r - R - Apply function on multiple data frames
问题描述
I loaded several data sheets as data frames in R with:
temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i]))
Now I would like to apply a function on all data frames. I thought about something like:
kappa1_mean_h_stem <- lapply(df.list, mean_h_stem)
Where df.list
contains a list of all data frames.
mean_h_stem <- function(x) {
mean(x[1,3])
}
I want the function to return the mean for a specific column. But it tells me, I had the wrong number of dimensions.
解决方案
The reason for your error is I think that you passed x[1,3]
which would get the value from the first row of the third column only. I assume you want to calculate the mean of the same column across all the data.frames
, so I made a slight modification to your function so you can pass data and the name or position of the column:
mean_h_stem <- function(dat, col){ mean(dat[,col], na.rm=T)}
Column can be selected using an integer:
lapply(df.list, mean_h_stem, 2)
Or a column name, expressed as a string:
lapply(df.list, mean_h_stem, 'col_name')
Passing the second argument like this can feel a little unintuitive, so you can do it in a clearer way:
lapply(df.list, function(x) mean_h_stem(dat = x, col ='col_name'))
This will only work for single columns at a time per your question, but you could easily modify this to do multiple.
As an aside, to read in the csv files, you could also use an lapply
with read.csv
:
temp <- list.files(pattern='*.csv')
df.list <- lapply(temp, read.csv)
推荐阅读
- android - 使用 clear() 删除 EncryptedSharedPreferences 不起作用
- image - Instagram 降低图片质量
- java - 如何使用 Projection 接口和本机查询获取具有空列的记录
- oracle11g - 如何在 oracle 11g 中更改我的几何图形?
- javascript - java ScriptEngine 在数组方法上失败
- kotlin - 在 Kotlin 中使用 Java 枚举时重载解析模糊
- python - 在有向图中查找循环,返回语句
- apache-spark - Spark 执行器在 jdbc 源中没有增加
- jquery - 使用 :last-child 选择最后一个孩子
- ios - PHImageResultIsDegradedKey/PHImageFileURLKey 未找到