在以下数据框中,我需要取a, b , c
values <- data.frame(value = c("a", "a", "a", "a", "a",
"b", "b", "b",
"c", "c", "c", "c"), i = c(1,2,3,4,5,6,7,8,9,10,11,12))
为实现这一目标,我尝试使用aggregate
函数,如下所示:
agg <- aggregate(values, by = list(values$value), FUN = mean)
输出确实产生i
的平均值,但我不认为这是正确的方法。输出也会引发一些警告。
Warning messages:
1: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
> agg
Group.1 value i
1 a NA 3.0
2 b NA 7.0
3 c NA 10.5
答案 0 :(得分:1)
删除警告消息:
aggregate(values, by = list(values$value), FUN = function(x) mean(as.numeric(x)))
返回:
Group.1 value i 1 a 1 3.0 2 b 2 7.0 3 c 3 10.5
或者,我们可以使用点表示法:
aggregate(. ~ value, values, mean)
或使用公式:
aggregate(i ~ value, values, mean)
两者都返回:
value i 1 a 3.0 2 b 7.0 3 c 10.5
答案 1 :(得分:0)
以下是使用dplyr
的另一个简单解决方案:
library(dplyr)
values %>%
group_by(value) %>%
summarise(i = mean(i))
# A tibble: 3 x 2
value i
<fctr> <dbl>
1 a 3.0
2 b 7.0
3 c 10.5