2016-11-29 51 views
0

我正在处理一些R代码来自动导入文件。我一直在使用sub来更改path字符串,更具体地说,我想通过Trial1Trial10参与者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) 

回答

0

我就从pilot_151116_pat1_250/realisations/recursive设置为TRUEfull.names设置为TRUE建立文件列表。然后用import函数运行ldply循环。稍后,您可以从file列中推断出哪些参与者和试用数据是其中的一部分。这可以通过使用strsplitsep等于/或通过使用来自dplyr包的separate来完成。