[英]Calculate sd and mean for many data frames in R
I have data frames called bmw_1,bmw_2,....bmw_9 and I want to calculate standard deviation and mean for each data frame but I don't want to write我有名为 bmw_1,bmw_2,....bmw_9 的数据帧,我想计算每个数据帧的标准差和平均值,但我不想写
mean(bmw_1) mean(bmw_2) mean(bmw_3)... mean(bmw_9)平均值(bmw_1) 平均值(bmw_2) 平均值(bmw_3)...平均值(bmw_9)
many times, so any help please很多次,所以请帮助
as mentioned in the comment, best way is to get the data frames into a list so you can apply a function over each.如评论中所述,最好的方法是将数据框放入列表中,以便您可以在每个列表上应用 function。
Get all dfs into a list by name pattern:按名称模式将所有 dfs 放入列表中:
ls_bmw <- mget(ls(pattern = "bmw_"))
Then apply the mean.然后应用平均值。
result <- lapply(ls_bmw, mean)
Difficult to go much further without a data example, but to get the results alongside the data frame name use:如果没有数据示例,很难进一步了解 go,但要获得数据框名称旁边的结果,请使用:
names(ls_bmw)
... to get a vector of the df names and: ...获取 df 名称的向量,并且:
unlist(result)
... to get a vector of the results. ...获得结果的向量。 The order of names and results elements will match and you convert that into a single result dataframe.
名称和结果元素的顺序将匹配,您将其转换为单个结果 dataframe。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.