tapply函数抱怨args长度不等但它们似乎匹配

时间:2014-04-25 20:27:01

标签: r tapply

这是失败的调用,错误消息和一些显示有问题的长度的显示:

it <- tapply(molten, c(molten$Activity, molten$Subject, molten$variable), mean)
# Error in tapply(molten, c(molten$Activity, molten$Subject, molten$variable),  : 
#  arguments must have same length

length(molten$Activity)
# [1] 679734

length(molten$Subject)
# [1] 679734

length(molten$variable)
# [1] 679734

dim(molten)
# [1] 679734      4

str(molten)
# 'data.frame': 679734 obs. of  4 variables:
#  $ Activity: Factor w/ 6 levels "WALKING","WALKING_UPSTAIRS",..: 5 5 5 5 5 5 5 5 5 5 ...
#  $ Subject : Factor w/ 30 levels "1","2","3","4",..: 2 2 2 2 2 2 2 2 2 2 ...
#  $ variable: Factor w/ 66 levels "tBodyAcc-mean()-X",..: 1 1 1 1 1 1 1 1 1 1 ...
#  $ value   : num  0.257 0.286 0.275 0.27 0.275 ...

2 个答案:

答案 0 :(得分:4)

如果你看一下?tapply,你会发现X应该是“一个原子对象,通常是一个向量”。您向tapply提供数据框(“熔化”),这不是原子对象。请参阅is.atomic,然后尝试is.atomic(molten)。此外,您的分组变量应以list形式提供(请参阅INDEX参数)。

这样的工作:

tapply(X = warpbreaks$breaks,  INDEX = list(warpbreaks$wool, warpbreaks$tension), mean)
#          L        M        H
# A 44.55556 24.00000 24.55556
# B 28.22222 28.77778 18.77778 

答案 1 :(得分:1)

你需要为INDEX设置一个对象,但是c( )会将它们全部串在一起,这是eror的来源,所以使用一个列表:

it <- tapply(molten$value, list(Act=molten$Activity, sub=molten$Subject, var=molten$variable), mean)

更好的是:

it <- with(molten , tapply(value, list(Act=Activity, Sub=Subject, var=variable), mean) )