Creating a summary statistical table from a data frame

前端 未结 5 967
醉酒成梦
醉酒成梦 2020-11-27 15:47

I have the following data frame (df) of 29 observations of 5 variables:

    age   height_seca1 height_chad1 height_DL weight_alog1
1   19         1800                


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 15:57

    So far I had the same problem and I wrote ...

    h <- function(x, flist){
      f <- function(f,...)f(...)
      g <- function(x, flist){vapply(flist, f , x, FUN.VALUE = numeric(1))}
      df <- as.data.frame(lapply(x, g , flist))
      row.names(df) <- names(flist)
      df
    
    }
    
    h(cars, flist = list(mean = mean, median = median, std_dev =  sd))
    

    it should work with any function as specified in flist as long as the function returns a single value; i.e. it wont work with range

    Note that elements of flist should be named otherwise, you'll get strange row.names for the resulting data.frame

提交回复
热议问题