将处理后的多个文件合并到一个数据框中,并为每个colname分配文件名

时间:2015-11-02 07:07:31

标签: r

我有一个文件列表,我写了一个函数来处理每个文件并返回两列(" name"" value")。

file_list <- list.files(pattern=".txt")
sample_name <- sub (".*?lvl.(.*?).txt","\\1",file_list)

for (i in 1:length(file_list)){
 x<- cleanMyData(file_list[i]) # this function returns a two column data 
 #then I want to merge all these processed data into one dataframe. Merge all "value" column based on the "name" column 
 # at the same time I want to put the file name in the corresponding column name. I already process the file name and put them into sample_name
}

为了更清楚,这里是我处理过的数据,例如:

file: apple.txt 
name value
A     12
B     13
C     14

file: pear.txt
name value
A     15
B     14
C     20
D     21

期待输出:

  Apple Pear
A   12   15
B   13   14
C   14   20

1 个答案:

答案 0 :(得分:0)

你可以尝试

fns <- c("apple.txt", "pear.txt")
(df <- 
Reduce(function(...) merge(..., all=F), 
       lapply(
         seq(fns), function(x) {
           read.table(fns[x], 
                      header=TRUE, 
                      col.names = c("name", 
                                    tools::file_path_sans_ext(fns)[x]))
         })
)
)
#   name     apple     pear
# 1    A        12       15
# 2    B        13       14
# 3    C        14       20

要将第一个字符大写,您可以使用类似

的内容
sub("\\b(\\w)", "\\U\\1", fns, perl=TRUE)

(见?sub

要删除name列,您可以使用subset(df, select = -name)