How to change column names of a Data frame

+1 vote

Here is the data frame called prices:

prices
  pr1 pr2 pr3
1 100 125 110
2 132 189 140
3 255 270 260

change the column names to old,new and best?

Thank you in Advance!!

Apr 11, 2018 in Data Analytics by DataKing99
• 8,250 points
1,715 views

3 answers to this question.

0 votes

Easiest way:

names(prices)[1]<-paste0("old")
names(prices)[2]<-paste0("new")
names(prices)[3]<-paste0("best")
#or by using colnames
colnames(prices)<-c("old","new","best")
prices
  old new best
1 100 125  110
2 132 189  140
3 255 270  260

answered Apr 11, 2018 by DeepCoder786
• 1,720 points
0 votes

Try this way -

names(data frame) <-  [list of new names]
names(iris) <- letters(1:5)
names(iris) <= c("ab","avd","yet","fwagr","xs")
answered Aug 26, 2019 by anonymous
• 33,030 points
0 votes

Hi,

To change the name of a column in a Dataframe, just use a combination of the names() function, indexing, and reassignment as shown below.

# Change name of 1st column of df to "a"
names(df)[1] <- "a"
answered Oct 30, 2020 by MD
• 95,460 points

Related Questions In Data Analytics

0 votes
2 answers

How to subset rows containing NA in a chosen column of a data frame?

You can give this a try. subset(dataframe, is.na(dataframe$col2)) ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
10,467 views
+1 vote
3 answers

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$sym ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
35,825 views
0 votes
1 answer

How to replace all occurrences of a character in a character column in a data frame in R

If you used sub() to replace the ...READ MORE

answered Jun 29, 2019 in Data Analytics by anonymous
• 33,030 points
17,936 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,921 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,342 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,813 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,062 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,776 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,661 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP