Changing column names of a data frame
Solution 1:
Use the colnames()
function:
R> X <- data.frame(bad=1:3, worse=rnorm(3))
R> X
bad worse
1 1 -2.440467
2 2 1.320113
3 3 -0.306639
R> colnames(X) <- c("good", "better")
R> X
good better
1 1 -2.440467
2 2 1.320113
3 3 -0.306639
You can also subset:
R> colnames(X)[2] <- "superduper"
Solution 2:
I use this:
colnames(dataframe)[which(names(dataframe) == "columnName")] <- "newColumnName"
Solution 3:
The error is caused by the "smart-quotes" (or whatever they're called). The lesson here is, "don't write your code in an 'editor' that converts quotes to smart-quotes".
names(newprice)[1]<-paste(“premium”) # error
names(newprice)[1]<-paste("premium") # works
Also, you don't need paste("premium")
(the call to paste
is redundant) and it's a good idea to put spaces around <-
to avoid confusion (e.g. x <- -10; if(x<-3) "hi" else "bye"; x
).
Solution 4:
Try:
names(newprice)[1] <- "premium"