renaming subset of columns in r with paste0 -
i have data frame (my_df
) columns named after individual county numbers. melted/cast data larger set point. first column name year , list of years 1970-2011. next 3010 columns counties. however, i'd rename county columns "column_"+county number
.
this code executes in r whatever reason doesn't update column names. remain solely numbers... help?
new_col_names = paste0("county_",colnames(my_df[,2:ncol(my_df)])) colnames(my_df[,2:ncol(my_df)]) = new_col_names
the problem subsetting within colnames
call.
try names(my_df) <- c(names(my_df)[1], new_col_names)
instead.
note: names
, colnames
interchangeable data.frame
objects.
edit: alternate approach suggested flodel, subsetting outside function call:
names(my_df)[-1] <- new_col_names
Comments
Post a Comment