0
我正在处理一些R代码来自动导入文件。我一直在使用sub
来更改path
字符串,更具体地说,我想通过Trial1
到Trial10
参与者1等等,并将其保存为data[i]
。而不是所有的试验手动这可以通过循环更有效地完成?函数本身添加的文件路径导入的数据,所以我可以使用此信息后循环更改字符串(用于文件导入功能)
path <- "C:/Users/Thomas/Desktop/tapping backup/Pilot141116/pilot_151116_pat1_250/realisations/participant_8/Trial1"
setwd(path)
files <- list.files(path = path, pattern = "midi.*\\.csv", full.names = T)
# set up a function to read a file and add a column for filename
import <- function(file) {
df <- read_csv(file, col_names = T)
df$file <- file
return(df)
}
# run that function across all files.
data1 <- ldply(.data = files, .fun = import)