【问题标题】:Read data from data files into R data frames将数据文件中的数据读入 R 数据帧
【发布时间】:2021-07-06 08:31:13
【问题描述】:

我想将多个文件中的数据读取到单独的数据帧中。这些文件与脚本位于不同的文件夹中。 我使用了带有文件名的列表。

users_list <- list.files(path = "Data_Eye/Jazz/data/first",
                         pattern = "*.cal", full.names = F)

我尝试使用函数 mapread_delim 但没有成功。将每个文件读取到不同的数据帧对我来说很重要。最好有数据框列表。

【问题讨论】:

标签: r dataframe


【解决方案1】:

你可以做这样的事情,虽然我没有任何 .cal 文件来测试它。因此,您可能需要不同的函数来读取这些文件。

library(devtools)
devtools::install_github("https://github.com/KirtOnthank/OTools")
library(OTools)

# Give full path to your files (if different than working directory).
temp = list.files(path="../Data/original_data", pattern="*.cal", full.names = TRUE)

# Then, apply the read.cal function to the list of files.
myfiles = lapply(temp, OTools::read.cal)

# Then, set the name of each list element (each dataframe) to its respective file name.
names(myfiles) <- gsub(".cal","",
                       list.files("../Data/original_data",full.names = FALSE),
                       fixed = TRUE)

# Now, put all of those individual dataframes from your list into the global environment as separate dataframes.
list2env(myfiles,envir=.GlobalEnv)

【讨论】:

    【解决方案2】:

    在base R中,只需使用lapply生成数据框列表:

    list_of_dfs <- lapply(users_list, read_delim)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 2013-06-24
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 2012-02-29
      • 1970-01-01
      • 2015-09-18
      相关资源
      最近更新 更多