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