0

我有几个以日期命名的 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
4

2 回答 2

4

该对象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
于 2013-08-11T05:27:35.000 回答
1

你可以使用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
于 2013-08-10T23:05:17.370 回答