3

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!

4
  • 2
    Here's a nice answer about why you should put them in a list. Commented Aug 10, 2017 at 3:32
  • @alistaire Thanks for the link. A good read. Commented Aug 10, 2017 at 4:48
  • Thanks @alistaire, I'll go throw and read this and adjust accordingly. There are a lot of other actions I'll be taking with these dataframes so this may may sense to combine the dfs and then split them back out towards the end when I write to xls files. Commented Aug 10, 2017 at 5:00
  • Did you find a solution to your problem? Commented Nov 9, 2022 at 4:29

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response @jameselmore, I am very familiar with mutate but hadn't thought of it in this application. I couldn't get this to work quite right yet but will keep trying tomorrow and let you know if I do. Thanks again!
1

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)
})

3 Comments

Thanks for your response. I wasn't able to get this to work due to the '$' sign which I converted to [] but still couldn't get it to work quite right.
I think my first answer should have worked. Can you try again with my update?
Thanks Tim. I just tried but am receiving a "Error in x[["ARTICLE_NUMBER"]] : subscript out of bounds error"
1

I see some few errors here and there: What is a$ARTICLE_NUMBER that need to be passed as an argument into the function str_pad?? is it already existing while running the for loop/ lapply function?? If yes then you have to be able to write the lapply/for loop function. Since I do not know how your data looks like, I would you a simpler version here

Define your variables as:

    a=b=c=list()# Just ensure they are lists

   lapply(list(a=a,b=b,c=c),function(x) {x$ARTICLE_NUMBER= "TYPE FUNCTION HERE";x})

From the above code I get the results:

$a
$a$ARTICLE_NUMBER
[1] "TYPE FUNCTION HERE"


$b
$b$ARTICLE_NUMBER
[1] "TYPE FUNCTION HERE"


$c
$c$ARTICLE_NUMBER
[1] "TYPE FUNCTION HERE"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.