-1

我正在尝试为我的项目做一些 R 编码。我必须从 R 中的一个目录读取一些 .csv 文件,并且必须将数据框分配为 df_subject1_activity1,我尝试过嵌套循环,但它不起作用。

前任:

我的目录名称是“Test”,我有六个 .csv 文件

subject1activity1.csv、subject1activity2.csv、subject1activity3.csv、subject2activity1.csv、subject2activity2.csv、subject2activity3.csv

现在我想编写代码以在 R 中加载此 .csv 文件并将数据框名称分配为

前任:

subject1activity1 = df_subject1_activity1
subject1activity2 = df_subject1_activity2

.... 等等使用 for 循环。

我的预期输出是:df_subject1_activity1 df_subject1_activity2 df_subject1_activity3 df_subject2_activity1 df_subject2_activity2 df_subject2_activity3

我尝试了以下代码: setwd(dirname(getActiveDocumentContext()$path)) new_path <- getwd() new_path

data_files <- list.files(pattern=".csv") # 识别文件名 data_files

for(i in 1:length(data_files)) {
    for(j in 1:4){
        assign(paste0("df_subj",i,"_activity",j)
        read.csv2(paste0(new_path,"/",data_files[i]),sep=",",header=FALSE))
  }
}

我没有得到欲望输出。R新手可以请任何人帮忙。谢谢

4

2 回答 2

0

你快到了。与往常一样,如果您不确定,使用更多行清楚地编码绝不是一个坏主意。


data_files <- list.files(pattern=".csv", full.names=TRUE) # Identify file names data_files

for( data_file in data_files) {

    ## check that the data file matches our expected pattern:
    if(!grepl( "subject[0-9]activity[0-9]", basename(data_file) )) {
        warning( "skiping file ", basename(data_file) )
        next
    }

    ## start creating the variable name from the filename

    ## remove the .csv extension
    var.name <- sub( "\\.csv", "", basename(data_file), ignore.case=TRUE )

    ## prepend 'df' and introduce underscores:
    var.name <- paste0(
        "df",
        gsub( "(subject|activity)", "_\\1", var.name ) ## this looks for literal 'subject' and 'acitivity' and if found, adds an underscore in front of it
    )

    ## now read the file
    data.from.file <- read.csv2( data_file )

    ## and assign it to our variable name
    assign( var.name, data.from.file )

}

我没有要测试的文件,但如果上述失败,您应该能够逐行运行代码并轻松查看它开始出错的地方。

于 2021-03-23T08:57:15.833 回答
0

一种解决方案是使用 vroom 包(https://www.tidyverse.org/blog/2019/05/vroom-1-0-0/),例如

library(tidyverse)
library(vroom)
library(fs)

files <- fs::dir_ls(glob = "subject_*.csv")
data <- purrr::map(files, ~vroom::vroom(.x))
list2env(data, envir = .GlobalEnv)

# You can also combine all the dataframes if they have the same columns, e.g.
library(data.table)
concat <- data.table::rbindlist(data, fill = TRUE)
于 2021-03-23T04:26:34.217 回答