如何在Rstudio中读取多个.txt文件并制作一个数据框?

时间:2019-09-16 12:37:39

标签: r dataframe text-files

我想建立一个推荐系统。我有17770个txt文件,每个txt文件都是包含用户ID和等级的电影元数据。

dataset_capture 我无法将数据导入RStudio。

我搜索了很多方法来导入多个数据,但最终所有方法都行不通。

至少我尝试了3个代码:

folderPath <- "D:/3rd Term/DataAnalysis/finalProject/dataSet/trainData/"
file_list <- list.files(path=folderPath, pattern="*.txt")    
dataSet <- 
  do.call("cbind", 
          lapply(file_list, 
                 function(x) 
                   read.table(paste(folderPath, x, sep=''), 
                              header = TRUE, 
                              stringsAsFactors = FALSE)))

========================================================================================
setwd("D:/3rd Term/DataAnalysis/finalProject/dataSet/trainData/")
files <-list.files()
data <- 0
for (f in files) {

  tempData = scan( f, what="character", sep = "")

  dataSet <- cbind(data,tempData)

} 

=========================================================================================

list_of_files <- list.files(path = "D:/3rd Term/DataAnalysis/finalProject/dataSet/trainData/", recursive = TRUE,
                            pattern = "\\.txt$", 
                            full.names = TRUE)

DT <- rbindlist(sapply(list_of_files, fread, simplify = FALSE),
                use.names = TRUE, idcol = "FileName", fill = TRUE)

我希望文件将作为数据框导入。我想使用cbind,以便可以合并所有txt,然后创建一个矩阵。

编辑: 我忘了提到每个txt都包含具有逗号分隔符/分隔符的userID,等级和日期(这并不重要),

1488844,3,2005-09-06
822109,5,2005-05-13
885013,4,2005-10-19
30878,4,2005-12-26

1 个答案:

答案 0 :(得分:0)

假设read.table命令可分别对每个文件起作用:

folderPath <- "D:/3rd Term/DataAnalysis/finalProject/dataSet/trainData/"
file_list <- list.files(path=folderPath, pattern="*.txt", full.names = TRUE) 
library(dplyr)
df <- lapply(file_list, function(file) {
  read.table(file, 
             header = TRUE, 
             stringsAsFactors = FALSE))
}) %>% 
  bind_rows()

根据更新后的问题,这里是一个最小的可重现示例

该示例表明,初始read.table命令也可以得到改进:

# create sample file to reproduce problem
writeLines("1488844,3,2005-09-06
822109,5,2005-05-13
885013,4,2005-10-19
30878,4,2005-12-26", "mv_00001.txt", useBytes = TRUE)

file_list <- list.files(path = ".", pattern="*.txt") 

# use the same file a couple of times to make setup more realistic
file_list <- c(file_list, file_list, file_list)

# initial answer with improved read-in command
library(dplyr)
df <- lapply(file_list, function(file) {
  read.csv(file, 
           header = FALSE, 
           col.names = c("userID", "rating", "date" ),
           stringsAsFactors = FALSE)
}) %>% 
  bind_rows()

# result
df
#>     userID rating       date
#> 1  1488844      3 2005-09-06
#> 2   822109      5 2005-05-13
#> 3   885013      4 2005-10-19
#> 4    30878      4 2005-12-26
#> 5  1488844      3 2005-09-06
#> 6   822109      5 2005-05-13
#> 7   885013      4 2005-10-19
#> 8    30878      4 2005-12-26
#> 9  1488844      3 2005-09-06
#> 10  822109      5 2005-05-13
#> 11  885013      4 2005-10-19
#> 12   30878      4 2005-12-26

reprex package(v0.3.0)于2019-09-17创建