Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为了在多个csv文件中找到特定行的平均值,我应该做什么?_R - Fatal编程技术网

R 为了在多个csv文件中找到特定行的平均值,我应该做什么?

R 为了在多个csv文件中找到特定行的平均值,我应该做什么?,r,R,我试图根据多个CSV文件中的行找到平均值。我已从目录中提取了该文件,并对其进行了修改,使其仅包含2个适用的列。问题是,我想根据目录中所有66个CSV文件的值来查找特定行的平均值。我的代码基本上卡在这里: # Set path to folder folder.path <- getwd() # Get list of csv files in folder filenames <- list.files("Path", pattern = "*.csv

我试图根据多个CSV文件中的行找到平均值。我已从目录中提取了该文件,并对其进行了修改,使其仅包含2个适用的列。问题是,我想根据目录中所有66个CSV文件的值来查找特定行的平均值。我的代码基本上卡在这里:

# Set path to folder
folder.path <- getwd()

# Get list of csv files in folder
filenames <- list.files("Path", pattern = "*.csv", full.names = TRUE)

# Read all CSV files in the folder and create a list of data frames
ldf <- lapply(filenames, read.csv)

# Select hr and stimulus columns in each dataframe in the list
ldf <- lapply(ldf, "[", c("Variable1", "Variable2"))

#See the variable left in each CSV files
lapply(ldf, names)
#设置文件夹的路径

folder.path您可以使用
lappy
ldf
中的每个数据帧进行迭代,并对它们进行
aggregate
以获得
Variable1
中每个唯一值的
Variable2
平均值

result <- lapply(ldf, function(x) aggregate(Variable1~Variable2, x, mean, na.rm = TRUE))

我们可以使用
data.table
方法

library(data.table)
out <- lapply(ldf, function(x) setDT(x)[, 
        .(Variable1 = mean(Variable1, na.rm = TRUE)),
       by = Variable])
库(data.table)
出来
library(data.table)
out <- lapply(ldf, function(x) setDT(x)[, 
        .(Variable1 = mean(Variable1, na.rm = TRUE)),
       by = Variable])