Reputation: 95
Pretty silly question, but I could not find a good solution. Say I have a dataframe like this:
dframe <- cbind.data.frame(V1=rnorm(10,0,1), V2=rnorm(10,0,1))
dframe
With the following code I can paste new words into column names:
names(dframe) <- paste("r_", names(dframe), sep="")
But what if I want to change names to only one specific column? Why does the following code not work? (actually the right side of the code does work, but the new names do not get assigned)
names(dframe[1]) <- paste("r_", names(dframe[1]), sep="")
In my real data I have a bunch of columns and I need to do this operation on some of them - still too many to change names one by one.
Upvotes: 0
Views: 459
Reputation: 2556
The following works:
dframe <- data.frame(V1 = rnorm(10, 0, 1), V2 = rnorm(10, 0, 1))
names(dframe)[1] <- paste("r_", names(dframe[1]), sep = "")
head(dframe, 2)
r_V1 V2
1 -0.5518091 -0.05065393
2 1.5083914 1.24679703
(You can use data.frame()
instead of cbind.data.frame()
.)
Upvotes: 1