我的桌面上有三個檔案夾,每個檔案夾包含 2,000 個 .txt 檔案,檔案的編號從 1.txt 到 2,000.txt。我想使用 R 使用 .txt 檔案的內容創建資料框,其中檔案中的每一行都是資料框中的一行。我希望資料框的列是
例如:資料框 1:
| Folder 1 | Folder 2 | Folder 3 |
| -------- | -------- | --------
| contents | contents | contents of
| of 1.txt | of 1.txt | 1.txt
from folder from folder from folder
1. 2. 3.
Data Frame 2:
| Folder 1 | Folder 2 | Folder 3 |
| -------- | -------- | --------
| contents | contents | contents of
| of 2.txt | of 2.txt | 2.txt
from folder from folder from folder
1. 2. 3.
我能夠使用以下方法從一個檔案夾中為一個 .txt 創建一個資料框:
setwd('/Users/name/Desktop/foldername')
txt_1 = readLines("1.txt")
df1 = data.frame(txt_1)
如何遍歷檔案夾并為 2,000 個 txt 檔案中的每一個創建單獨的資料框?
此外,如何在每個資料框中添加來自檔案夾 2 和檔案夾 3 以及第 2 列和第 3 列的 2,000 個相應的 txt 檔案?
感謝您的幫助!
uj5u.com熱心網友回復:
這就是我個人的處理方式。事實上,我使用類似的方法來處理我在作業場所運行的一些自動化任務的日志檔案。
其中大部分可以在不使用dplyrand的情況下完成tidyr,但是,tibble物件(a 的變體data.frame)具有列印方法,如果您正在處理大量資料 [1],該方法將被證明是有用的。
我在每個步驟上方包含了代碼在做什么的簡短描述。我看到您對 Stack Overflow 很陌生,但不確定您對 R 的總體經驗水平,所以請不要害怕要求澄清。
library(dplyr)
library(tidyr)
library(parallel)
parent_dir <- "path_to_your_desktop"
folder <- c("Folder1", "Folder2", "Folder3")
cl <- makeCluster(detectCores() - 1)
AllData <-
# Make a data frame with the path to the folders
tibble(folder_path = file.path(parent_dir, folder),
folder = folder) %>%
# Return a name of all of the files in the folders.
# Consider using the `pattern` argument in ?list.files
# if the files you want to read all have a common naming convention
mutate(files = lapply(folder_path,
list.files)) %>%
unnest(cols = 'files') %>%
# Read the data from each of the files.
# Whatever function you are using to process your data can
# replace read.csv. I'm using it just for illustration.
# Note: you mention a lot of file, I'm using the parallel version
# of lapply here as it might cut down on the time it takes to read
# all of your files.
mutate(data = parLapply(cl,
file.path(folder_path, files),
read.csv,
stringsAsFactors = FALSE),
# You mentioned you would want to remove data frames that
# have a different number of rows. Here's a quick way
# to get the number of rows in each data frame.
nrows = vapply(data,
nrow,
numeric(1))) %>%
select(-folder_path)
AllData
#> # A tibble: 5 x 4
#> folder files data nrows
#> <chr> <chr> <list> <dbl>
#> 1 Folder1 PointlessData.txt <df[,3] [3 x 3]> 3
#> 2 Folder2 PointlessData.txt <df[,3] [3 x 3]> 3
#> 3 Folder3 PointlessData - Copy (2).txt <df[,3] [3 x 3]> 3
#> 4 Folder3 PointlessData - Copy.txt <df[,3] [3 x 3]> 3
#> 5 Folder3 PointlessData.txt <df[,3] [3 x 3]> 3
Created on 2022-10-27 by the reprex package (v2.0.1)
[1] 在 adata.frame中,由串列組成的列被完整列印。對于大量資料,這可能會使您的 R 會話或計算機停止。在 atibble中,列印的所有內容都是串列元素的指示。
uj5u.com熱心網友回復:
你可能想要這樣的東西。全球環境中的 2000 個單獨的資料框似乎太多了。也許將它們放在串列中會更易于管理。PS,我無法對此進行測驗,因為我不知道您的檔案是什么樣的。另外,由于您想跳過具有不同行號的檔案,因此我將其添加到其中。
library(tidyverse)
loc_1 <- '/Users/name/Desktop/foldername1/'
loc_2 <- '/Users/name/Desktop/foldername2/'
loc_3 <- '/Users/name/Desktop/foldername3/'
df_list <- map(1:20,
\(i){
df1 <- read_lines(glue::glue("{loc_1}{i}.txt")) |>
data.frame()
df2 <- read_lines(glue::glue("{loc_2}{i}.txt")) |>
data.frame()
df3 <- read_lines(glue::glue("{loc_3}{i}.txt")) |>
data.frame()
if(all(c(nrow(df1), nrow(df2), nrow(df3)) == nrow(df1))){
bind_cols(df1, df2, df3)
} else NA
}) |>
`names<-`(paste0(1:2000, ".txt"))
uj5u.com熱心網友回復:
這是一個基本的 R 解決方案。它創建一個資料框串列,其中檔案編號對應于串列索引;每個檔案的行數不匹配的情況是NULL.
dfs <- list()
folders <- c("Folder 1", "Folder 2", "Folder 3")
for (i in 1:2000) {
paths <- file.path(folders, paste0(i, ".txt"))
names(paths) <- folders
files <- lapply(paths, readLines)
if (length(unique(sapply(files, length))) == 1) {
dfs[[i]] <- data.frame(files)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521630.html
標籤:r数据框文本
