【发布时间】:2016-03-06 11:54:36
【问题描述】:
这是我的数据框:
x1 <- as.numeric(c(1.5, 3.2, 5.1, 6.6, NA))
x2 <- as.numeric(c(3.4, 5.6, NA, NA, 0.9))
x3 <- as.character(c("abc", NA, "cde", NA, NA))
x4 <- as.logical(c(1, 1, 0, 1, 0))
x5 <- as.integer(c(NA, 2, 4, 7, 9))
df <- data.frame(x1, x2, x3, x4, x5, stringsAsFactors = F)
缺失值的汇总统计(百分比):
summary( round(apply(is.na(df), 2, sum)/(nrow(df)), 4) )
我可以按类别提取列并应用汇总统计信息 4 次以获得这些独特类别的数据:
unique(sapply(df, class))
我想知道是否有一种减少代码行数的好方法将汇总统计信息放在一个按类拆分的表中?
感谢您的帮助。
非常感谢
拉米
【问题讨论】:
-
你的意思是
lapply(split(seq_along(df), sapply(df, class)), function(x) summary(round(sum(is.na(df[x])/nrow(df),4)))) -
好的,谢谢。但是您的代码中有一个错字;-)
lapply(split(seq_along(df), sapply(df, class)), function(x) summary(round(apply(is.na(df[x]), 2, sum)/(nrow(df)), 4))) -
如果这是您想要的输出,我们可以使用
colMeans(作为答案发布)