0

In R I have several dataframes with unique names. These names are also available in a column named as names. I need to run a loop like this

for(i in 1:length(names)) {

}

in which I will select the dataframes and do following things

  1. rename the first column to series
  2. remove NAs from series

filter(!is.na(series))

  1. use the following command to name the similar names unique

group_by(series) %>% mutate(series1 = if(n( ) > 1) {paste0(series, row_number( ))} else {paste0(series)}) %>% ungroup(series)

  1. then use gather command to convert columns into rows and some rows into columns 5 and add a column in the dataframe which has repeated values of unique dataframe name with header label.

The problem is that my try like following in a loop is not working

colnames(names[i])[1] <- "series"

  • 2
    names[i] is a character string. It is not the object being named. To get that object use get(names[i]) or you might want to put all the series into a list L <- mget(names) – G. Grothendieck Aug 04 '21 at 18:05
  • If you keep related data in a list, that will make it much easier to apply transformation to many elements at once. Having a bunch of different variables not in a list but in the global workspace makes things much more difficult to work with in R. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Aug 04 '21 at 18:58

0 Answers0