2

这是设置:

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中的每一列都是我知道是错误的“字符”类。总结虽然是正确的。

4

3 回答 3

5

发生这种情况的原因是apply需要一个矩阵

> as.matrix(mydf)
   weight     RespID b1      
1  "1.342888" "3182" "Mand"  
2  "1.188844" "3183" "Kvinde"
3  "1.159798" "3184" "Mand"  
4  "1.342888" "3185" "Mand"  
5  "1.082853" "3186" "Kvinde"
6  "1.079329" "3187" "Kvinde"
7  "1.289133" "3188" "Mand"  
8  "1.211910" "3189" "Kvinde"
9  "1.034386" "3190" "Kvinde"
10 "1.227196" "3191" "Kvinde"

您要使用的是sapply

> sapply(mydf,class)
   weight    RespID        b1 
"numeric" "numeric"  "factor" 
于 2013-08-02T13:20:05.597 回答
1

apply强制转换matrix不能包含 a 的 a factor,因此将其强制转换为字符变量:

df <- data.frame( x = as.factor(letters[1:3]) , y = as.factor(LETTERS[1:3]) )
str(df)
'data.frame':   3 obs. of  2 variables:
 $ x: Factor w/ 3 levels "a","b","c": 1 2 3
 $ y: Factor w/ 3 levels "A","B","C": 1 2 3

apply(df,2,class)
          x           y 
"character" "character" 

 sapply(df,class)
       x        y 
"factor" "factor" 
于 2013-08-02T13:21:20.100 回答
0

我找到了原因。

似乎 apply 会将 data.frame 强制转换为矩阵,从而导致每一列都存储为字符类型。apply 方法当然会在转换后报告类。诀窍是要意识到 data.frame 是一个美化列表,因此

lapply(mydf, class)
# $weight
# [1] "numeric"
# 
# $RespID 
# [1] "numeric"
# 
# $b1
# [1] "factor"

给出正确答案。

于 2013-08-02T13:20:29.410 回答