Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply function multiple dataframes in R

Tags:

r

I have multiple data frames and would like to take the same action across an identically named column in each data frame.

I was able to do a for loop to read the multiple CSVs and create the dataframes but couldn't get a for loop to work to use str_pad across the same column in dataframes.

For example, I have:

a$ARTICLE_NUMBER <- str_pad(a$ARTICLE_NUMBER, 11, pad = 0)
b$ARTICLE_NUMBER <- str_pad(b$ARTICLE_NUMBER, 11, pad = 0)
c$ARTICLE_NUMBER <- str_pad(c$ARTICLE_NUMBER, 11, pad = 0)

I've tried:

vendor_list <- c("a", "b", "c")

for(i in vendor_list){
  i[ARTICLE_NUMBER] <- str_pad(i[ARTICLE_NUMBER], width = 11, pad = 0)
}

As well as:

lapply(vendor_list, function(x){
  x[ARTICLE_NUMBER] <- str_pad(x[ARTICLE_NUMBER], width = 11, pad = 0)
  return(x)
})

Also:

string_pad <- function(x){
  x[ARTICLE_NUMBER] <- str_pad(x[ARTICLE_NUMBER], width = 11, pad = 0)
}

vendor_list <- lapply(vendor_list, string_pad(x) x[, 1])

Not sure what I'm missing. Any help is much appreciated!

like image 332
user2299914 Avatar asked Apr 15 '26 17:04

user2299914


2 Answers

I think the primary issue was the manor in which you were addressing the column in the data.frame, your first attempt would work for something like this:

i[['ARTICLE_NUMBER']] <- str_pad(i[['ARTICLE_NUMBER']], width = 11, pad = 0)

In either case, I recommend a different approach. Operations like this on data.frames are much easier in the dplyr package

library(dplyr)

vendor_list <- list(a, b, c)
pad_article_num <- 
    function(df) {
         mutate(df, ARTICLE_NUMBER = str_pad(ARTICLE_NUMBER, width = 11, pad = 0)
    }
vendor_list <- lapply(vendor_list, pad_article_num)
like image 66
jameselmore Avatar answered Apr 18 '26 09:04

jameselmore


You could add the three data frames to a list and then use lapply():

df_list <- list(a, b, c)
lapply(df_list, function(x) {
    x[["ARTICLE_NUMBER"]] <- str_pad(x[["ARTICLE_NUMBER"]], 11, pad = 0)
})
like image 44
Tim Biegeleisen Avatar answered Apr 18 '26 07:04

Tim Biegeleisen