0

Hi I'm trying to simplify my code by using a loop. I tried to modify a column name(fips to area_fips) in multiple data frames and later I will add a leading 0 to the column "area_fips". I don't know why my loop didn't do anything. My data looks like:

fips   state  county
1234   AL     xx county
2345   AL     xx county
...

Here is what I have so far.

list_2014 = read.csv('C:/cfpb_list_2014.csv')
list_2015 = read.csv('C:/cfpb_list_2015.csv')
list_2016 = read.csv('C:/cfpb_list_2016.csv')
list_2017 = read.csv('C:/cfpb_list_2017.csv')
list_2018 = read.csv('C:/cfpb_list_2018.csv')
list_2019 = read.csv('C:/cfpb_list_2019.csv')

filelist = c('list_2014','list_2015','list_2016','list_2017','list_2018','list_2019')

for (i in filelist){
   d = get(i)
   names(d)[1] = 'area_fips'

   }

I tried to use i$area_fips in the loop but it didn't work.

list_2014$area_FIPS = str_pad(list_2014$FIPS, 5, pad = "0")
list_2015$area_FIPS = str_pad(list_2015$FIPS, 5, pad = "0")
list_2016$area_FIPS = str_pad(list_2016$FIPS, 5, pad = "0")
list_2017$area_FIPS = str_pad(list_2017$FIPS, 5, pad = "0")
list_2018$area_FIPS = str_pad(list_2018$FIPS, 5, pad = "0")
list_2019$area_FIPS = str_pad(list_2019$FIPS, 5, pad = "0")

Thanks!

serena su
  • 3
  • 1
  • you're renaming a copy of the data, `d`, not the original. you would need to replace the original object with the updated one, so adding `assign(i, d)` to your `for` loop should work – rawr Jan 26 '20 at 05:09
  • 1
    Once you `get` the data and store it into `d`, you update the first column name in `d` *but never store it back into the variable pointed to by `i`. While I truly dislike the `get`/`assign` methodology (much preferring Ronak's use of `lapply`), you would need to use `assign(i, d)` within the loop to put the updated value back into the original frame. But again, this method is ill-advised for many many reasons. – r2evans Jan 26 '20 at 05:58
  • And you could improve on your file reading method. `filelist <- list.files(pattern = "cfpb_list_201\\d\\.csv"); list_df <- lapply(filelist, read.csv)`. – Rui Barradas Jan 26 '20 at 07:14

1 Answers1

1

You could use mget to get all the dataframes in one list, change the column name of first column and apply str_pad on it. We can then return the changed dataframe.

total_list <- lapply(mget(filelist), function(x) {
   names(x)[1] = 'area_fips'
   x[[1]] <- stringr::str_pad(x[[1]], 5, pad = "0")
   return(x)
})

If you need them as seprate dataframes we can use list2env

list2env(total_list, .GlobalEnv)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213