Search code examples
rloopsdataframeassign

How to change column names in dataframe in the loop?


I have 10 (for example) dataframes with similar names like df1,df2,df3,... with 10 columns I'd like to give names for 10th column in each dataframe like dataframe name(10th column in df1 must has "df1" name, in df2 -- "df2" and etc)

i tried this

for (i in paste0("df",1:10)){
     assign(names(get(i))[10],
            value=i
            )
     }

but nothing changed How can i solve this problem?


Solution

  • You can make it in three steps :

    --get

    --change colnames

    -- assign

    for (i in paste0("df",1:3)){
      d=get(i)
      colnames(d)[10]=i
      assign(i,d)
    }