【发布时间】:2023-03-11 17:24:01
【问题描述】:
这是设置:
mydf<-structure(list(weight = c(1.34288799762726, 1.18884372711182,
1.15979790687561, 1.34288799762726, 1.08285343647003, 1.07932889461517,
1.28913342952728, 1.211909532547, 1.03438591957092, 1.22719633579254
), RespID = c(3182, 3183, 3184, 3185, 3186, 3187, 3188, 3189,
3190, 3191), b1 = structure(c(1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L,
2L, 2L), .Label = c("Mand", "Kvinde"), class = "factor")), .Names = c("weight",
"RespID", "b1"), row.names = c(NA, 10L), class = "data.frame")
现在对 summary 的调用将生成以下输出:
summary(mydf)
# weight RespID b1
# Min. :1.034 Min. :3182 Mand :4
# 1st Qu.:1.102 1st Qu.:3184 Kvinde:6
# Median :1.200 Median :3186
# Mean :1.196 Mean :3186
# 3rd Qu.:1.274 3rd Qu.:3189
# Max. :1.343 Max. :3191
同时 apply 给出另一个结果:
apply(mydf, 2, class)
# weight RespID b1
#"character" "character" "character"
因此,根据应用,我的 data.frame 中的每一列都属于“字符”类,我知道这是错误的。总结是正确的。
【问题讨论】:
-
?apply的第一句写着:“如果X不是一个数组而是一个具有非空dim值的类的对象(比如一个数据框),apply尝试强制它如果它是二维的(例如,数据框)或通过 as.array,则通过 as.matrix 到数组。”矩阵只能保存一种数据类型。 -
apply 适用于数组,而不是 data.frames - 这可能是burns-faq no。 89383772