我试图使用第三列对两个变量的TRUE值求和,但是我收到了一个错误。这是一个代码示例:
framea<-data.frame(v1=c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, NA, TRUE, TRUE, TRUE, FALSE),
v2=c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, NA, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE),
v3=c("Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue"),
v4=c(1:4, 1:4, 1:5))
framea
attach(framea)
aggdata<-aggregate(formula=cbind(v1, v2)~v4, data=framea, by=list(v3), FUN=sum, na.rm = TRUE, na.action = NULL)
detach(framea)
aggdata
在aggregate()步骤之后,我收到以下错误:
Error in FUN(X[[i]], ...) : invalid 'type' (list) of argument
我理想地想要像aggdata的以下输出:
v1 v2
1 Blue 1 0
2 Blue 1 1
3 Blue 0 2
4 Blue 0 0
1 Red 2 2
2 Red 1 1
3 Red 1 1
我该如何做到这一点?
答案 0 :(得分:2)
感谢上面akrun的帖子,我想通了。我不应该使用公式=能力。这是答案:
framea<-data.frame(v1=c(TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, NA, TRUE, TRUE, TRUE, FALSE),
v2=c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, NA, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE))
v3<-c("Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue", "Red","Blue")
v4<-c(1:3, 1:3, 1:3, 1:4)
framea
attach(framea)
aggdata<-aggregate(x=framea, by=list(v3, v4),FUN=sum, na.rm = TRUE, na.action = NULL)
detach(framea)
aggdata
(请告诉我是否应删除此内容。谢谢。)