[英]How to read and combine multiple files into one dataframe in rstudio?
for (i in 1:length(all_files)) {file <- read_tsv(all_files[i], col_names = FALSE) %>% bind_rows()}
This code saves only the last file from the folder to dataframe file.. Also, this files doesn't have headers, so I used col_names = FALSE, but then I also need to skip some of the columns and rename others, so how do I do that?此代码仅将文件夹中的最后一个文件保存到 dataframe 文件中。此外,此文件没有标题,因此我使用 col_names = FALSE,但是我还需要跳过某些列并重命名其他列,那怎么办我这样做?
If the goal is to read in and stack/append many files into one data.frame, I recommend using a list.如果目标是读取并将许多文件堆叠/附加到一个 data.frame 中,我建议使用列表。 Read-in files one at a time, saving each data.frame as a list element (ie, the third file you read in is a data.frame stored in the third element of a list).一次读入一个文件,将每个 data.frame 保存为一个列表元素(即,您读入的第三个文件是存储在列表的第三个元素中的 data.frame)。 The package data.table then has a handy function that will stack them all for you. package data.table 然后有一个方便的 function 可以为您堆叠它们。
Some template code:一些模板代码:
# empty list to store each file as a data.frame when read-in
file_list <- vector(model="list", length=length(all_files))
# loop over files, reading in one at a time
for(i in 1:length(all_files)) {
file_list[[i]] <- read_tsv(all_files[i])
}
# stack all of the files using rbindlist() from package data.table
big_df <- rbindlist(file_list)
To anyone that stumbles with this post in Google, there's a typo in DanY's answer:对于在 Google 中偶然发现这篇文章的任何人,DanY 的回答中有一个错字:
file_list <- vector( model ="list", length=length(all_files))文件列表<-向量( model =“列表”,长度=长度(所有文件))
The correct argument is mode = "list"正确的参数是mode = "list"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.