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!
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)
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)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With