Change Colnames

Hi R community,

I am an R beginner and I have a very minor question. Currently, my data have column names starting with V1 and ending with V30. In reality, from V1 to V9 indicate a measure called Efficacy and V10 to V23 with Satisfaction and V24 to V30 with Success. I would like to change the column names but having difficulty doing that. Could you help me find the solution?

Best

The basic way to do that for a three column data frame named DF is

colnames(DF) <- c("Name1", "Name2", "Name3")

In your case, you can type 30 names or, if you have a pattern, you can use paste() to construct a vector of names.

Here is one method:

#Demonstrate the output,
c(paste0("efficacy",1:9),paste0("satisfaction",1:14),paste0("success",1:7))
 [1] "efficacy1"      "efficacy2"      "efficacy3"      "efficacy4"      "efficacy5"     
 [6] "efficacy6"      "efficacy7"      "efficacy8"      "efficacy9"      "satisfaction1" 
[11] "satisfaction2"  "satisfaction3"  "satisfaction4"  "satisfaction5"  "satisfaction6" 
[16] "satisfaction7"  "satisfaction8"  "satisfaction9"  "satisfaction10" "satisfaction11"
[21] "satisfaction12" "satisfaction13" "satisfaction14" "success1"       "success2"      
[26] "success3"       "success4"       "success5"       "success6"       "success7"
#Assign the names
colnames(DF) <- c(paste0("efficacy",1:9),paste0("satisfaction",1:14),paste0("success",1:7))
1 Like

Thank you so much!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

Thank you so much!

Could you let me know how to use paste() in this case?
I want to have variable names like efficacy1, efficacy2, efficacy3...efficacy9.

Best