Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

I have a data frame called "newprice" (see below) and I want to change the column names in my program in R.

> newprice

   Chang.  Chang.   Chang.

1     100       36      136

2     120      -33       87

3     150       14      164

In fact, this is what am doing:

names(newprice)[1]<-paste("premium")

names(newprice)[2]<-paste("change")

names(newprice)[3]<-paste("newprice") 

I have not put this in a loop because I want each column name to be different as you see.

When I paste my program into R console this is the output it gives me:

> names(newprice)[1]<-paste(“premium”)

Error: unexpected input in "names(newprice)[1]<-paste(“"

> names(newprice)[2]<-paste(“change”)

Error: unexpected input in "names(newprice)[2]<-paste(“"

> names(newprice)[3]<-paste(“newpremium”)

Error: unexpected input in "names(newprice)[3]<-paste(“"

I have equally tried using the c() function for example c("premium"), instead of the paste() function, but to no avail.

Could someone help me to figure this out?

1 Answer

0 votes
by

To change the column names of a data frame, you can use the colnames() function as follows:

colnames(dataframe) <- c('premium','change','newprice')

Output:

      premium      change       newprice

1         100         36            136

2        120         -33            87

3       150          14            164

To change the name of a particular column:

colnames(dataframe)[2] <- "change"

Browse Categories

...