如果您的唯一列 ID 是字符,您如何使用聚合?
aggregate(data, list(data$colID), sum)
Error in Summary.factor(c(1L, 1L), na.rm = FALSE) :
sum not meaningful for factors
换个性格..
data$colID<-as.character(data$colID)
aggregate(data, list(data$colID), sum)
Error in FUN(X[[1L]], ...) : invalid 'type' (character) of argument
ddply I get a similar error.
Error in FUN(X[[1L]], ...) :
only defined on a data frame with all numeric variables
我只想按 colID 聚合,我不想总结它。我希望所有其他列求和。
dput(data)
structure(list(colID = structure(c(1L, 1L, 1L, 2L, 2L), .Label = c("a",
"b"), class = "factor"), col1 = c(1, 0, 0, 0, 2), col2 = c(0,
1, 0, 2, 0), col3 = c(0, 0, 1, 0, 0), col4 = c(5, 5, 5, 7, 7)), .Names = c("colID",
"col1", "col2", "col3", "col4"), row.names = c(NA, -5L), class = "data.frame")