![](/img/trans.png)
[英]import a folder with multiple .csv files and manipulate all dataframe at once in R
[英]how can I import and convert multiple excel files in a folder into dataframe in R
我在一個文件夾中有 1,000 個文件,並希望將它們作為 R 中的數據框處理。我可以轉換單個文件,但需要一種一次性轉換文件的方法。 需要任何幫助,請。 到目前為止我所擁有的:
我的代碼:
my_files <- list.files()
my_files <- as.data.frame(my_files)
my_files
class(my_files)顯示了一個數據框,但並不是真正作為數據框工作
這是我在這種情況下使用的解決方案。 在需要的地方進行更改。 您需要先安裝rio
和data.table
包。
my_files <- list.files(path = "~", # change this to the path where your files are
pattern = ".xlsx$", # use the file ending of your files
full.names = TRUE,
recursive = TRUE)
my_df_list <- lapply(my_files, rio::import) # imports files and produces list of data.frames
my_df <- data.table::rbindlist(my_df_list) # attempts to bind data.frames into one
我們可以使用list.files
來獲取文件的路徑,使用lapply
我們可以遍歷文件名、讀取文件並刪除第一列。
my_files <- list.files(full.names = TRUE)
all_files <- lapply(my_files, function(x) read.csv(x)[-1])
all_files
是每個文件中沒有第一列的數據幀列表,可以通過all_files[[1]]
、 all_files[[2]]
等訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.