嗨我将一个函数应用于数据框的每一列,并为每列返回一个列表 . 现在我想将此列表转换为矩阵或数据框 .

已阅读this nice post但仍有一些问题 .

df = data.frame(name = c('Tom', 'Mark', 'Jane'),
            weight = c(150, 140, 110),
            sex = c('M', 'M', 'F'), 
            fulltime = c(T, T, F), stringsAsFactors = F)

df$sex = as.factor(df$sex)

# return a list
f1 = function(column){
        list( class = class(column),
              mean = mean(column)
        )
}

lapply do.call(rbind)的作品:

result = lapply(df[,], f1)
result

test1 = do.call(rbind.data.frame, result)
test1

# this seems to be same
test2 = as.data.frame(do.call(rbind, result), stringsAsFactors = T) 
test2
identical(test1, test2) # i don't know why not identical...

为什么sapply不起作用 - 它起初看起来很棒:

result = sapply(df, f1)
result # looks perfect:
#        name        weight    sex      fulltime 
# class "character" "numeric" "factor" "logical"
# mean  NA          133.3333  NA       0.6666667

# but each element is a list
result['class', 'name']
result[1 ,1]
str(result) 

# `unlist` loses row/col names
test = matrix( unlist(result), ncol = ncol(df), byrow = T)
test

# this makes 1D data frame
test1 = do.call(rbind.data.frame, result)
test

有人知道为什么矢量可以像上面那样排列成2D吗?它是属性的影响吗?

我们如何修复 sapply(df, f1) 的结果?它看起来非常接近正确的结果 .

谢谢!