将R中的csv文件合并到不同的列 [英] Combining csv files in R to different columns

查看:281
本文介绍了将R中的csv文件合并到不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有100多个csv文件,每个文件都包含一列日期和时间(每个文件的值完全相同),然后是第二列,每个文件都不同。我希望文件名是列名。

I have over 100 csv files, each containing one column of Date and Time (which are the exact same values for every file) and then a second column that is different for every file. I want the file name to be the column name.

因此,基本上我想为每个文件在数据框中添加一列。

So essentially I want to add a column to my data frame for each file.

最有效的方法是

到目前为止,我所要做的只是列出文件夹中所有csv文件的列表,因为到目前为止我已经找到了所有信息。似乎只是在告诉我如何将数据添加为更多的行,而不是更多的列。

So far all I've done is make a list of all of the csv files in my folder because all of the information I have found so far only seems to be telling me how to add the data as more rows, not more columns.

推荐答案

这是一个选择。它或多或少是手工制作的,因为我不知道有哪个命令可以完全按照您的指定执行此操作。

Here's an option. It's more or less handcrafted as I'm not aware of any single command to do this exactly as you've specified.

# the working directory contains d1.csv, d2.csv, d3.csv with
# matching first two columns. The third column is random data
read.csv("./d1.csv")
#>     a b         c
#> 1   1 A 0.5526777
#> 2   2 B 0.2161643
#> 3   3 C 0.3311132
#> 4   4 D 0.3577971
#> 5   5 E 0.2298579
#> 6   6 F 0.4014883
#> 7   7 G 0.2789038
#> 8   8 H 0.5729675
#> 9   9 I 0.3413949
#> 10 10 J 0.5807167

## identify the .csv files in the working directory
file_list <- dir(".", pattern = "*.csv")
file_list
#> [1] "d1.csv" "d2.csv" "d3.csv"

## for each of the .csv files, extract the base filename and 
## create a new object with that name containing the data.
## Additionally, name the third column by the basename
for ( file in file_list) {
  f <- sub("(.*)\\.csv", "\\1", file)
  assign(f, read.csv(file = file))
  assign(f, setNames(get(f), c(names(get(f))[1:2], file)))
}

## at this point, the objects d1, d2, and d3 have been created, 
## containing their respective data. The third column of each of 
## these is their originating filename.
d1
#>     a b    d1.csv
#> 1   1 A 0.5526777
#> 2   2 B 0.2161643
#> 3   3 C 0.3311132
#> 4   4 D 0.3577971
#> 5   5 E 0.2298579
#> 6   6 F 0.4014883
#> 7   7 G 0.2789038
#> 8   8 H 0.5729675
#> 9   9 I 0.3413949
#> 10 10 J 0.5807167

## specify the names of the date and time columns (common between files)
date_col <- "a"
time_col <- "b"

## use Reduce to go through the list of created objects and 
## merge them together
list_of_objects <- mget(sub("(.*)\\.csv", "\\1", file_list))
combined_files <- Reduce(function(x, y) merge(x, y, by = c(date_col, time_col)), list_of_objects)

combined_files
#>     a b    d1.csv    d2.csv    d3.csv
#> 1  10 J 0.5807167 0.8181820 0.7073864
#> 2   1 A 0.5526777 0.3225574 0.3758595
#> 3   2 B 0.2161643 0.6933108 0.5654979
#> 4   3 C 0.3311132 0.9309869 0.1727413
#> 5   4 D 0.3577971 0.8810876 0.7802144
#> 6   5 E 0.2298579 0.1023579 0.9925649
#> 7   6 F 0.4014883 0.1328283 0.7610007
#> 8   7 G 0.2789038 0.2926512 0.7469455
#> 9   8 H 0.5729675 0.8727978 0.3073394
#> 10  9 I 0.3413949 0.3107775 0.4778286

如果您不了解某些方面,请告诉我然后我将扩展评论。

If there's a particular aspect that you don't understand, let me know and I'll expand the comments.

这篇关于将R中的csv文件合并到不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆