rbind列表中的所有给定列 [英] rbind all given columns within a list
问题描述
我正在将不固定数量的.csv文件(都包含在当前工作目录中)读入一个列表,并希望重新查找每个.csv文件的第二列.
I am reading a variable number of .csv files, all contained in the present working directory, into a list, and would like to rbind the 2nd column of each of these .csv files.
工作目录中的文件如下:
The files in the working directory look like this:
150601_0001.csv
150601_0002.csv
150601_0003.csv
etc.
我有以下代码将它们全部读取到目录中任意数量文件的列表中: (代码来自此处)
I have the following code to read them all into a list for any given number of files in the directory: (code comes from here)
myfiles <- dir(pattern = "\\.(csv|CSV)$", full.names = TRUE) # get filenames and paths
myfiles_data <- lapply(myfiles, data.table::fread) # read data from files, quickly
head(myfiles_data[[1]]) # validate file reading
names(myfiles_data) <- myfiles # assign names to list items
到目前为止,这一切都很好,我将所有数据收集到一个漂亮的列表中:
This works perfectly fine so far, and I get all the data into a pretty list:
> myfiles_data
$`./150601_0001.csv`
X Y Z
1: 1 67.81 1
2: 2 68.52 1
3: 3 69.66 1
---
250: 250 50.02 1
251: 251 50.58 1
252: 252 51.16 1
$`./150601_0002.csv`
X Y Z
1: 1 70.77 2
2: 2 70.54 2
3: 3 70.47 2
---
250: 250 51.00 2
251: 251 51.17 2
252: 252 51.43 2
$`./150601_0003.csv`
X Y Z
1: 1 68.32 3
2: 2 67.80 3
3: 3 67.33 3
---
250: 250 50.58 3
251: 251 50.68 3
252: 252 50.77 3
现在,我想在每个数据集的第二列上进行查找.以下代码为我提供了仅包含第二列的列表(出于视觉目的,数据被缩写):
Now I want to rbind on the second column of each data set. The following code gives me a list with only the second columns (data was abbreviated for visual purposes):
> lapply(myfiles_data, `[[`, 2)
$`./150601_0001.csv`
[1] 67.81 68.52 69.66 ...
...
[241] ... 52.85 51.85 50.90
$`./150601_0002.csv`
[1] 70.77 70.54 70.47 ...
...
[241] ... 51.00 51.17 51.43
$`./150601_0003.csv`
[1] 68.32 67.80 67.33 ...
...
[241] ... 50.58 50.68 50.77
我如何一次性将rbind()应用于所有这些对象?
How may I apply rbind() to all of these in one shot?
推荐答案
不具有添加评论的声誉,因此无法添加答案
Dont have reputation to add a comment so adding an answer
尝试将所有数据重新绑定到一个数据帧中
try this to rbind all data into one dataframe
Output<-do.call(rbind, list)
这篇关于rbind列表中的所有给定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!