Search code examples
rfunctionxts

Creating a function to iterate over tibble elements within a large list in R


I am trying to create a function to automatize some basic formatting I need to do before I combine multiple datasets into an xts. I can do a bulk read of the files and create a large list of tibbles. But I'm having a hard time creating a function to iterate over that list. When I read individual files into a df, I have been running the following:

df<-df[!(duplicated(df$DateTime)),] dfx<-xts(df[,-1], order.by = as.POSIXct(df$DateTime, format="%d-%b-%y %H:%M:%S"))

Then I do an merge.xts of all of the 'dfx' objects. One issue I have with the data is that the DateTime does not always match up between files and the above method gives me a large xts with NAs, which is what I prefer to another type of merge/rbind. I would like to create a function to do this over and over, especially because reading each file into separate data frames and then merging is grueling now that I have to combine 10+. All of my attempts have not been successful and now I am just stuck. :/ Any help would be appreciated!


Solution

  • If it is a list, we can use lapply to loop over the list and use an anonymous function call to apply the function

    lst2 <- lapply(lst1, function(df) {
         df<-df[!(duplicated(df$DateTime)),]
         xts(df[,-1], order.by = as.POSIXct(df$DateTime, format="%d-%b-%y %H:%M:%S")) 
      })
    

    and then use Reduce to do the merge

    Reduce(merge, lst2)