与R中的多个数据帧相同的功能 [英] Same function over multiple data frames in R
问题描述
这是我的简化示例:
2数据框:df1,df2
df1
开始停止ID
0 10 x
10 20 y
20 30 z
df2
开始停止ID
0 10 a
10 20 b
20 30 c
我想要的是一个第四列,平均起始和停止两个dfs
df1
开始停止ID平均
0 10 x 5
10 20 y 15
20 30 z 25
我可以一次执行一个数据框:
df1 $ Avg < - rowMeans(subset(df1,select = c(start,stop)),na.rm = TRUE)
但是我想在所有数据框上运行它。
任何帮助将不胜感激!
创建数据框列表,然后使用lapply将功能应用于它们。
df.list< - list(df1,df2,...)
res< - lapply df.list,function(x)rowMeans(subset(x,select = c(start,stop)),na.rm = TRUE))
#保留原始data.frame也
res< ; - lapply(df.list,function(x)cbind(x,rowmean= rowMeans(subset(x,select = c(start,stop)),na.rm = TRUE)))
然后,lapply将按顺序将每个数据框中的数据输入x。
I am new to R, and this is a very simple question. I've found a lot of similar things to what I want but not exactly it. Basically I have multiple data frames and I simply want to run the same function across all of them. A for-loop could work but I'm not sure how to set it up properly to call data frames. It also seems most prefer the lapply approach with R. I've played with the get function as well to no avail. I apologize if this is a duplicated question. Any help would be greatly appreciated!
Here's my over simplified example: 2 data frames: df1, df2
df1
start stop ID
0 10 x
10 20 y
20 30 z
df2
start stop ID
0 10 a
10 20 b
20 30 c
what I want is a 4th column with the average of start and stop for both dfs
df1
start stop ID Avg
0 10 x 5
10 20 y 15
20 30 z 25
I can do this one data frame at a time with:
df1$Avg <- rowMeans(subset(df1, select = c(start, stop)), na.rm = TRUE)
but I want to run it on all of the dataframes.
Any help would be greatly appreciated!
Make a list of data frames then use lapply to apply the function to them all.
df.list <- list(df1,df2,...)
res <- lapply(df.list, function(x) rowMeans(subset(x, select = c(start, stop)), na.rm = TRUE))
# to keep the original data.frame also
res <- lapply(df.list, function(x) cbind(x,"rowmean"=rowMeans(subset(x, select = c(start, stop)), na.rm = TRUE)))
The lapply will then feed in each data frame as x sequentially.
这篇关于与R中的多个数据帧相同的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!