Search code examples
rlistnames

Rename names from a list with file names without the ".csv" in R


I have a list of files like this:

> list_files
[[1]]
[1] "25.csv"

[[2]]
[1] "21.csv"

[[3]]
[1] "23.csv"

[[4]]
[1] "24.csv"

[[5]]
[1] "27.csv"

and when I apply this:

  for (i in seq_along(list_files)) {
    df<-read.csv(list_files[[i]], sep="", stringsAsFactors=FALSE)
    time_series <- as.xts(df$SMS, order.by =as.POSIXct(df$TIME))
    final_list[[i]] <- time_series
  }
  final_list
}

My final_list dont have the names of files that is the user id like 25,21,23... How can I solve this problem?


Solution

  • If you want to assign names to what you add to the list, you should be doing assignment to a string name instead of i which is a numeric iterator. For file name:

    final_list[[list_files[[i]]]] <- time_series
    

    For just the iterator:

    final_list[[as.character(i)]] <- time_series
    

    For the number from the file name you have to split the string on a period and extract the first element:

    final_list[[ strsplit(list_files[[i]],'[.]')[[1]][1] ]] <- time_series
    

    Based on your comments, I think the full form you want is:

    final_list<-list()
    for (i in list_files) {
      df<-read.csv(i, sep="", stringsAsFactors=FALSE)
      time_series <- as.xts(df$SMS, order.by =as.POSIXct(df$TIME))
      final_list[ strsplit(i,'[.]')[[1]][1] ] <- time_series
    }
    final_list
    

    Note that instead of iterating through 1:length(list_files) which seq_along is doing, we are actually iterating through the list itself (i.e. '25.csv','24.csv'...)

    Final edit: As in the 2nd part of my answer, you should do for i in info$user to iterate over the user IDs. Your use of seq_along is generating numbers 1 through the number of users that do not correspond to the actual ids.

    I would amend both code blocks to one:

    time_series_list<-list()
    for(id in as.character(info$user)){
        if(file.exists(paste0(id,".csv"))){
          file <- dir(path = setwd(path_list), pattern=paste0(id))
          df<-read.csv(file, sep="", stringsAsFactors=FALSE)
          time_series <- as.xts(df$SMS, order.by =as.POSIXct(df$TIME))
          time_series_list[ id ] <- time_series
        }else{
          print(paste("File for id",id,"doesn't exist")
        }
      }