微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在具有相同列表的 R 中使用嵌套的 for 循环?

如何解决如何在具有相同列表的 R 中使用嵌套的 for 循环?

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

例如:

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

subject1activity1.csv, 主题1活动2.csv, subject1activity3.csv, 主题2活动1.csv, 主题2活动2.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

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 新手可以请任何人帮忙。 谢谢

解决方法

一种解决方案是使用 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)
,

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


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 )

}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。