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!

user2299914
  • 111
  • 2
  • 7
  • 2
    [Here's a nice answer about why you should put them in a list.](https://stackoverflow.com/a/24376207/4497050) – alistaire Aug 10 '17 at 03:32
  • @alistaire Thanks for the link. A good read. – tushaR Aug 10 '17 at 04: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. – user2299914 Aug 10 '17 at 05:00
  • Did you find a solution to your problem? – René Martínez Nov 09 '22 at 04:29

3 Answers3

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)
jameselmore
  • 442
  • 9
  • 20
  • 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! – user2299914 Aug 10 '17 at 05:03
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)
})
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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"
Onyambu
  • 67,392
  • 3
  • 24
  • 53