在循环中读取多个csv并在主csv中写为列

时间:2018-03-29 01:14:54

标签: r loops csv

我们说我有两个.csv表(实际上有几百个):

Table 1
x     mean_snowcover    useless_data 
1     80                6546156
2     50                6285465
3     60                2859525

Table 2
x    mean_snowcover    useless_data
1    91                87178
2    89                987189
3    88                879278927

我想要一个看起来像这样的新表:

Mean Snowcover
x    Table_1    Table_2
1    80         91
2    50         89
3    60         88

这是我目前的代码:

setwd("C:/Users/evan/Desktop/Finished Data SGMA/test")

master1=read.csv("New folder/AllSGMA.csv")

temp = list.files(pattern="*.csv$",recursive=FALSE)

###READ CSVS IN LOOP###
for(x in 1:length(temp)){
    mycsv = read.csv(temp[x])
    mean_snowcover=mycsv$mean_snowcover
    master2=cbind(master1,mean_snowcover)
}

write.csv(master2,"Mean Snowcover.csv")

但输出是一张空白表。我已经看过关于堆栈溢出的类似问题,但我无法弄清楚我需要改变什么。我是R的新手。

1 个答案:

答案 0 :(得分:0)

您可以使用Reducedplyr::left_join

df1 <- read.table(text =
    "x     mean_snowcover    useless_data
1     80                6546156
2     50                6285465
3     60                2859525", header = T)

df2 <- read.table(text =
    "x    mean_snowcover    useless_data
1    91                87178
2    89                987189
3    88                879278927", header = T)

library(dplyr);
library(magrittr);
Reduce(function(x,y)
    left_join(x, y, by = "x") %>% select(x, contains("snowcover")), list(df1, df2))
#    x mean_snowcover.x mean_snowcover.y
#  1 1               80               91
#  2 2               50               89
#  3 3               60               88

这将适用于任意数量的data.frame,只要它们共享一个共同的x列,并将它们全部放在list中,即

lst <- list(df1, df2, df3, ....)
Reduce(function(x,y)
    left_join(x, y, by = "x") %>% select(x, contains("snowcover")), lst)