1

I have a problem, I want apply a function for several data frame, I call these like follow:

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) assign(temp[i], read.csv(temp[i], sep = ";"))

after the function called ajuste_tiempos, created by me, like follow:

ajuste_tiempos <- function(datos) {

tiempos <- datos$HOUR
timestamps <- as.POSIXlt(as.POSIXct('1900-1-1', tz='UTC') 
                     + as.difftime(as.character(tiempos)))
timestamps$min <- (timestamps$min + 5/2) %/% 5 * 5
time <- format(timestamps, format='%H:%M:%S')
date<-paste(datos[,3], time, sep=" ")
date
}

basically it's the function that I want to apply, but after applying this function to the data frames I want to do other manipulations with the columns. I'm trying it this way:

ajuste_tiempos <- list()
ff <- list(temp[i])
a <- lapply(ff, function(i) {ajuste_tiempos(i)})

Does anyone have any idea how I can do it?

  • `ajuste_tiempos <- list()` overwrites your function. Is `temp` already a list of data frames? If it is then removing `ajuste_tiempos <- list()` and running `lapply(temp, ajuste_tiempos)` should work. Assuming your files follow the same format, then you would `rbind` to combine them into a single data frame. – Mako212 Mar 06 '18 at 17:30
  • I'm skeptical that your `assign` works, `temp` starts as a character vector of file names, then you try to `assign` data frames to it. Start a new list for the data frames, and don't use `assign`, just use `<-` or `=`. Like this `data_list = list(); for(...) {temp[[i]] <- read.csv(...)}`. – Gregor Thomas Mar 06 '18 at 17:33
  • @Mako212, **temp** is a list of files into a directory, but the idea is not bind the files, I want to leave them separately, I tried to apply your recommendations and I get this error: Error in datos$HOUR : $ operator is invalid for atomic vectors – Antonio Bonilla Mar 06 '18 at 18:39
  • @Grego 1 I do not understand, can you be more specific please, **temp** is a list of files into directory, how I have four CSV files, *temp* list and show the files – Antonio Bonilla Mar 06 '18 at 18:42
  • Despite the word "list" in the command, `temp = list.files(pattern="*.csv")` returns a vector of class `character`, not an object class `list`. Look at `class(temp)` or `str(temp)` to see that it is not a `list`. And you can't assign data frames to a vector, which is why you should create a different object that is actually a `list` to hold your data frames as you read them int. – Gregor Thomas Mar 06 '18 at 18:57
  • @Gregor Well I understand what you say with the way I use list.files, so you recommend creating a list object and put the CSV files of my directory within that list? – Antonio Bonilla Mar 06 '18 at 19:17
  • A `character` vector is the right object to store file names. When you read in CSV files, you should put them in a `list`. I would recommend using descriptive names rather than something like `temp`. For example `csv_names = list.files(pattern="*.csv")`, then `data_list = lapply(csv_names, read.csv, sep = ";")`. I've written a [long answer here about using lists of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207), you should read that. – Gregor Thomas Mar 06 '18 at 19:21
  • ok @Gregor I do what you say and my 4 CSV files in my directory are in a list, but they remain as a big list, that is to say that I already called them and I can work with them? – Antonio Bonilla Mar 06 '18 at 19:28
  • I can't understand what you mean. Please read this answer: https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames/24376207#24376207 – Gregor Thomas Mar 06 '18 at 19:32
  • @Gregor basically I want to call n CSV files from my working directory, apply the function **ajuste_tiempos** that I described above and finally manipulate these CSV files such as emilinate or paste columns – Antonio Bonilla Mar 06 '18 at 19:48

0 Answers0