SO上有一些与此主题类似的问题,但与我的用例不完全相同。我有一个数据集,其中列的布局如下所示
Id Description Value
10 Cat 19
10 Cat 20
10 Cat 5
10 Cat 13
11 Cat 17
11 Cat 23
11 Cat 7
11 Cat 14
10 Dog 19
10 Dog 20
10 Dog 5
10 Dog 13
11 Dog 17
11 Dog 23
11 Dog 7
11 Dog 14
我想要做的是通过 Id、Description 捕获 Value 列的平均值。最终的数据集将如下所示。
Id Cat Dog
10 14.25 28.5
11 15.25 15.25
我可以以非常粗略的方式做到这一点,但效率不是很高
tempdf1 <- df %>%
filter(str_detect(Description, "Cat")) %>%
group_by(Id, Description) %>%
summarize(Mean_Value = mean(Value) , na.rm = TRUE))
这不是很方便。非常感谢任何有关如何更有效地实现预期结果的建议。