R 在子集上使用lappy
我想生成一个均值向量,它是从R中现有向量的子集中导出的 我的数据如下所示:R 在子集上使用lappy,r,R,我想生成一个均值向量,它是从R中现有向量的子集中导出的 我的数据如下所示: date plant_ID treatment stalk_count flower_count 195 1 control 0 0 196 1 control 0 0 197 1 control 0 0 198 1
date plant_ID treatment stalk_count flower_count
195 1 control 0 0
196 1 control 0 0
197 1 control 0 0
198 1 control 0 0
.........................................................
237 98 treatment 0 0
239 98 treatment 0 0
226 98 treatment 2 9
我想我需要使用split()按plant_ID将数据分成子集,但不知道如何告诉lappy()获取这些子集,并将mean()函数应用于每个子集中包含的flower_count数据
我的问题是:
1-这是一种有效的方法吗?2-如何编写代码来完成此操作?我们不需要
拆分
,可以通过使用聚合
从基R
通过一组操作来获得“花卉计数”的平均值
aggregate(flower_count~plant_ID, df1, FUN = mean)
或者使用dplyr
library(dplyr)
df1 %>%
group_by(plant_ID) %>%
summarise(flowercountMean = mean(flower_count))
如果我们想将lappy
与split
lapply(split(df1$flower_count, df1$plant_ID), mean)
谢谢你,阿克伦,这很有帮助。然而,我还不能让它运行。“df1”这个词是什么意思?@JKO假设你读了数据集df1啊,太好了,非常感谢!