R中聚合后的排序
我首先使用R中聚合后的排序,r,sorting,R,Sorting,我首先使用aggregate获得数据帧中每列的平均值: meanDemVoteHouseState <- aggregate(congress$X2012.House.Dem.vote, by = list(state = congress$state), FUN = mean) 得到 'data.frame': 50 obs. of 2 variables:
aggregate
获得数据帧中每列的平均值:
meanDemVoteHouseState <- aggregate(congress$X2012.House.Dem.vote,
by = list(state = congress$state),
FUN = mean)
得到
'data.frame': 50 obs. of 2 variables:
$ state: chr "AK" "AL" "AR" "AZ" ...
$ x : num 0.29 0.34 0.29 0.462 0.566 ...
显然,新变量现在被称为“x”
但当我试着分类时:
meanDemVoteHouseState[order(x),]
我收到一个错误“找不到对象“x”
我尝试了很多其他的方法,但都没有效果
我错过了什么?你想要什么
meanDemVoteHouseState[order(meanDemVoteHouseState[,"x"]),]
如果你分两步来做的话,你会变得更清楚
myind <- order(meanDemVoteHouseState[,"x"]) # need 'x' fully qualified
meanDemVoteHouseState[myind, ]
myind你想要什么
meanDemVoteHouseState[order(meanDemVoteHouseState[,"x"]),]
如果你分两步来做的话,你会变得更清楚
myind <- order(meanDemVoteHouseState[,"x"]) # need 'x' fully qualified
meanDemVoteHouseState[myind, ]
myind这样做可能更容易
meanDemVoteHouseState <- aggregate(X2012.House.Dem.vote ~ state,
data = congress, FUN = mean)
意味着只做一件事可能会更容易些
meanDemVoteHouseState <- aggregate(X2012.House.Dem.vote ~ state,
data = congress, FUN = mean)
meanDemVoteHouseState你能给出一个国会数据样本吗?meanDemVoteHouseState[带(meanDemVoteHouseState,order(x)),]
或meanDemVoteHouseState[带(meanDemVoteHouseState,order(x)),]你能给出一个国会数据样本吗?meanDemVoteHouseState[带(meanDemVoteHouseState,order(x)),]
或meanDemVoteHouseState[订单(meanDemVoteHouseState$x),]
谢谢。那也行。我可能会把变量名改成更合理的名称。谢谢。那也行。我可能最终会将变量名更改为更合理的名称。