Home > Blockchain >  How to do paste0 with a specified value with only selected columns in R
How to do paste0 with a specified value with only selected columns in R

Time:11-30

I have a data frame that has three columns

  Col1|Col2|Col3

I want to merge the text "Table1" into only Col2 and Col3

names(df)<-paste0("Table1_",colnames(df[,c(2:3)]))

but I end up getting

 Table1_Col1|Table1_Col2|

The third column I get blank and no column name. I am trying to get

Col1| Table1_Col2| Table1_Col3

CodePudding user response:

You almost had it: names(df)[2:3] <- paste0("Table1_", names(df)[2:3])

Because a data.frame is a list of vectors, you can just use the simpler names() rather than colnames() (but both are completely fine imo).

  •  Tags:  
  • r
  • Related