简体   繁体   English

如何在rstudio中读取多个文件并将其合并为一个dataframe?

[英]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.

相关问题 如何在Rstudio中读取多个.txt文件并制作一个数据框? - How to read multiple .txt files in Rstudio and make a dataframe? Read Multiple txt files in an order and combine them into one dataframe but label the origin of each row in the new generated dataframe in r - Read Multiple txt files in an order and combine them into one dataframe but label the origin of each row in the new generated dataframe in r R中如何一次读取多个lidar文件(.las)合并成一个dataframe - How to read many lidar files (.las) at once and combine them into one dataframe in R 如何读取和合并多个nts文件 - How to read and combine multiple nts files 如何在 R 中将多个.txt 文件中的数据合并为一个 dataframe(需要转置) - How to combine data from multiple .txt files into one dataframe (transposing required) in R 读取多个ENVI文件并将它们组合在一个csv中 - read multiple ENVI files and combine them in one csv 如何一次读取多个 txt 文件并使用 R 合并到一个 df? - How to read in multiple txt files at once and combine to one df using R? 如何使用一个读取选项读取多个文件 - How to read multiple files with one read option 如何将几个类似的.csv文件组合成一个给定结构的数据帧 - How to combine several similar .csv files into one dataframe with given structure 如何在 R 中将具有两个制表符分隔列的 multiple.txt 文件读取到一个 dataframe 中? - How can I read multiple .txt files with two tab separated columns into one dataframe in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM