如何绑定csv文件中的行?

时间:2016-12-07 03:19:25

标签: r dplyr

我在一个文件夹中有数百个csv文件。

temp <- list.files(pattern = "\\.csv$")

for (i in 1:length(temp)) 
  #assign(temp[i], read.csv(temp[i]))
  rbind(temp[i], read.csv(temp[i]))

相同数量的列。到处都有不同的行名。

我想按行排列它们。

我该怎么做?

欢迎任何解决方案。

示例1:

structure(list(X1.000000 = 2, X2116.000000 = 2120, X9.000000 = 19, 
    X0.624003 = 0.966611, X0.514702 = 1.003793, X0.376078 = 0.03738, 
    X.0.235598 = -0.033804, X0.000490 = 0.007754, X9.632849 = 236.345147, 
    X239017.279109 = 8732111.450485, X31533.351601 = 31533.351601, 
    X1190693206.447250 = 1190693206.44725), .Names = c("X1.000000", 
"X2116.000000", "X9.000000", "X0.624003", "X0.514702", "X0.376078", 
"X.0.235598", "X0.000490", "X9.632849", "X239017.279109", "X31533.351601", 
"X1190693206.447250"), class = "data.frame", row.names = c(NA, 
-1L))

示例2:

structure(list(X1.000000 = c(2, 3), X2076.000000 = c(2087, 2088
), X19.000000 = c(18, 1), X0.995502 = c(1.023801, 1.399615), 
    X0.951066 = c(0.99377, 0.111436), X0.020733 = c(0.026184, 
    0.622115), X.0.017602 = c(0.015609, 0.582765), X0.040000 = c(0.064003, 
    2e-06), X1179.116439 = c(1940.301862, 0.06376), X40779688.793149 = c(68889876.093994, 
    2669.598384), X29610.941343 = c(29610.941343, 29610.941343
    ), X1035102354.289360 = c(1035102354.28936, 1035102354.28936
    )), .Names = c("X1.000000", "X2076.000000", "X19.000000", 
"X0.995502", "X0.951066", "X0.020733", "X.0.017602", "X0.040000", 
"X1179.116439", "X40779688.793149", "X29610.941343", "X1035102354.289360"
), class = "data.frame", row.names = c(NA, -2L))

TRIAL

mymergeddata <- 
  do.call(rbind,
          lapply(temp,read.csv)

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

试用#2

lst <- lapply(lst, "names<-", paste0("V", 1:(ncol(lst[[1]])-1)))
lst <- lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]])))
lst2<-do.call(rbind, lst)

这是错误消息:

Error in lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]]))) : 
  'names' attribute [12] must be the same length as the vector [11]

这里:

length(lst[[1]])
[1] 12

1 个答案:

答案 0 :(得分:0)

可能统一名称,以便实际名称与以前的名称相匹配:

# ex1 <- structure(list(X1.000000 = 2, X2116.000000 ...
# ex2 <- structure(list(X1.000000 = c(2, 3),...
lst <- list(ex1, ex2) # <=> lst <- lapply(temp,read.csv)
lst <- lapply(lst, "names<-", paste0("V", 1:ncol(lst[[1]])))
do.call(rbind, lst)
#   V1   V2 V3       V4       V5       V6        V7       V8         V9          V10      V11        V12
# 1  2 2120 19 0.966611 1.003793 0.037380 -0.033804 0.007754  236.34515  8732111.450 31533.35 1190693206
# 2  2 2087 18 1.023801 0.993770 0.026184  0.015609 0.064003 1940.30186 68889876.094 29610.94 1035102354
# 3  3 2088  1 1.399615 0.111436 0.622115  0.582765 0.000002    0.06376     2669.598 29610.94 1035102354

这假定每个文件具有相同的列数。