【问题标题】:How do you combine two columns into a new column in a dataframe made of two or more different csv files?如何在由两个或多个不同 csv 文件组成的数据框中将两列组合成一个新列?
【发布时间】:2013-08-10 22:59:22
【问题描述】:

我有几个以日期命名的 csv 文件,对于所有这些文件,我想在每个文件中创建一个新列,其中包含来自放置在一起的其他两个列的数据。然后,我想将它们组合成一个大数据框,并只选择其中两列来保留。这是一个例子:

假设我有两个数据框:

  a b c        a b c
x 1 2 3      x 3 2 1
y 2 3 1      y 2 1 3

然后我想在它们每个中创建一个新列 d:

  a b c  d        a b c  d
x 1 2 3 13      x 3 2 1 31
y 2 3 1 21      y 2 1 3 23

然后我想像这样组合它们:

  a b c  d
x 1 2 3 13
y 2 3 1 21
x 3 2 1 31
y 2 1 3 23

然后保留 a 和 d 列中的两列,并删除另外两列 b 和 c:

  a  d
x 1 13
y 2 21
x 3 31
y 2 23

这是我当前的代码(当我尝试合并两列或尝试只保留两列时它不起作用):

    f <- list.files(pattern="201\\d{5}\\.csv")        # reading in all the files
    mydata <- sapply(f, read.csv, simplify=FALSE)     # assigning them to a dataframe
    do.call(rbind,mydata)                             # combining all of those dataframes into one
    mydata$Data <- paste(mydata$LAST_UPDATE_DT,mydata$px_last)   # combining two of the columns into a new column named "Data"
    c('X','Data') %in% names(mydata)               # keeping two of the columns while deleting the rest

【问题讨论】:

    标签: r csv dataframe multiple-columns


    【解决方案1】:

    对象mydata 是一个数据框列表。您可以使用lapply 更改列表中的数据框:

    lapply(mydata, function(x) "[<-"(x, "c", value = paste0(x$a, x$b)))
    

    file1 <- "a b             
    x 2 3"    
    file2 <- "a b
    x 3 1"
    mydata <- lapply(c(file1, file2), function(x) read.table(text = x, header =TRUE))
    lapply(mydata, function(x) "[<-"(x, "c", value = paste0(x$a, x$b)))
    
    # [[1]]
    #   a b  c
    # x 2 3 23
    # 
    # [[2]]
    #   a b  c
    # x 3 1 31
    

    【讨论】:

      【解决方案2】:

      您可以为此使用rbind (data1,data2)[,c(1,3)]。我假设您可以在每个数据框中创建col d,这是一个基本的东西。

       data1<-structure(list(a = 1:2, b = 2:3, c = c(3L, 1L), d = c(13L, 21L
          )), .Names = c("a", "b", "c", "d"), row.names = c("x", "y"), class = "data.frame")
      
       > data1
            a b c  d
          x 1 2 3 13
          y 2 3 1 21   
      
      data2<-structure(list(a = c(3L, 2L), b = c(2L, 1L), c = c(1L, 3L), d = c(31L, 
      23L)), .Names = c("a", "b", "c", "d"), row.names = c("x", "y"
      ), class = "data.frame")
      
      > data2
        a b c  d
      x 3 2 1 31
      y 2 1 3 23
      
      data3<-rbind(data1,data2)
      
          > data3
         a b c  d
      x  1 2 3 13
      y  2 3 1 21
      x1 3 2 1 31
      y1 2 1 3 23
      
      finaldata<-data3[,c("a","d")]
          > finaldata
         a  d
      x  1 13
      y  2 21
      x1 3 31
      y1 2 23
      

      【讨论】:

      • 文件其实很多,所以我不想在每个数据帧中手动输入d列。
      • 我认为他们最终会寻找do.call(rbind, ...),因为他们在文件中读取的方式,他们最终会得到listdata.frames。
      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      • 2016-07-20
      • 2022-01-20
      • 2018-08-12
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多