Rename a single column in dataframe using R

0 votes

How to rename a vector/data frame with a single column?

I know with various columns, I can use

colnames(x) <- c("col1","col2")


#Consider this example
 Samp <- data.frame(sample(trainer$index, 10000))
head(Samp )
#   sample.trainer.index..10000.
# 1                      5907862
# 2                      2181266
# 3                      7368504
# 4                      1949790
# 5                      3475174
# 6                      6062879

ncol(Samp)
# [1] 1
class(Samp)
# [1] "data.frame"
class(Samp[1])
# [1] "data.frame"
class(Samp[,1])
# [1] "numeric"
colnames(Samp)[2] <- "newname2"
# Error in names(x) <- value : 
#   'names' attribute [2] must be the same length as the vector [1]
Apr 13, 2018 in Data Analytics by BHARANI
• 410 points
353 views

1 answer to this question.

0 votes
colnames(Samp)[2] <- "newname2"

This sets the name of second column. But since you have only one column you can use:

colnames(trSamp) <- "newname2"
answered Apr 13, 2018 by kappa3010
• 2,020 points

Related Questions In Data Analytics

+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,170 points
179 views
0 votes
3 answers

Code snippet to find number of null values in each column in a dataframe in R

colSums(is.na(data_frame_name)) will return you number of nulls ...READ MORE

answered Jul 5 in Data Analytics by sindhu
116 views
0 votes
0 answers

Create a dual axis column/bar chart using ggplot in R

Hi, i need to create a dual ...READ MORE

Jul 22 in Data Analytics by riya
70 views
0 votes
1 answer

How to save a DataFrame in R??

Suppose your DataFrame is named as df: write.csv(df,file="exmp.csv") Then ...READ MORE

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

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
50 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
72 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
154 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,550 points
116 views
0 votes
1 answer
0 votes
1 answer

How to limit output of a dataframe in R?

For randomly sampling a row/cell where a ...READ MORE

answered Apr 18, 2018 in Data Analytics by kappa3010
• 2,020 points
62 views