-1

I amtrying to do some R coding for my project. Where I have to read some .csv files from one directory in R and I have to assign data frame as df_subject1_activity1, i have tried nested loops but it is not working.

ex:

my dir name is "Test" and i have six .csv files

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

now i want to write code to load this .csv file in R and assign dataframe name as

ex:

subject1activity1 = df_subject1_activity1
subject1activity2 = df_subject1_activity2

.... so on using for loop.

my expected output is: df_subject1_activity1 df_subject1_activity2 df_subject1_activity3 df_subject2_activity1 df_subject2_activity2 df_subject2_activity3

I have trie dfollowing code: setwd(dirname(getActiveDocumentContext()$path)) new_path <- getwd() new_path

data_files <- list.files(pattern=".csv") # Identify file names 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))
  }
}

I am not getting desire output. new to R can anyone please help. Thanks

  • 1
    Most of the time in R you don't need to use a loop. Can you show some sample code you have tried and also what you want to end up with? Why do you think you need nesting? You may want to look at the dir() function. – Elin Mar 23 '21 at 03:58
  • Hi Elin, I am have writen below code: data_files <- dir(pattern=".csv") data_files for(i in 1:data_files) { assign(paste0("df_subj",i,"_activity",i), # Read and store data frames read.csv2(paste0(new_path,"/",data_files[i]),sep=",",header=FALSE,na.strings="")) } I am trying to rename my file names with df_subject1_activity1 and so on. i have 2 subjects and each subject has 3 activities. – user15457402 Mar 23 '21 at 04:02
  • Edit the post to include the code that you have tried. You can change the `assign` line to `assign(paste0("subject",i,"activity",i), read.csv2(....))`. Although using `assign` is discouraged and not recommended, store the data in a list instead. – Ronak Shah Mar 23 '21 at 04:15

2 Answers2

0

One solution is to use the vroom package (https://www.tidyverse.org/blog/2019/05/vroom-1-0-0/), e.g.

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)
jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • Hi, thank you for your reply. But I am not suppose to use tidyverse package. I gave 6 csv files with same colunm name but diffrent data. so I need to load 6 .csv files wich assign to 6 different dataframes. I can do it if I have to assign data frame with one name with 1 to6 numbers for example firstfile.csv to data.frame1, secondfile.csv to data.frame2. in this case i can use single for loop. but in my case my firstfile.csv is assign to subject1_activity1_df then secondfile.csv to subject1_activity2_df and so on. so I am not able to assign dataframe names in this situation with single for loop – user15457402 Mar 23 '21 at 05:23
0

You are almost there. As always, if you are unsure, is never a bad idea to code clearly using more lines.


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 )

}

I don't have your files to test with, but should the above fail, you should be able to run the code line by line and easily see where it starts to go wrong.

Sirius
  • 5,224
  • 2
  • 14
  • 21