[R] Can't rename a data frame column

Boris Steipe boris.steipe at utoronto.ca
Mon May 2 19:49:26 CEST 2016


You need to learn about proper subsetting, and the names() function. Consider the following:

R > d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
R > d
  alpha beta gamma
1     1    4     7
2     2    5     8
3     3    6     9
R > names(d)
[1] "alpha" "beta"  "gamma"
R > names(d) == "beta"
[1] FALSE  TRUE FALSE
R > names(d)[names(d) == "beta"] <- "two"
R > d
  alpha two gamma
1     1   4     7
2     2   5     8
3     3   6     9

R > names(d)[3] <- "three"
R > d
  alpha two three
1     1   4     7
2     2   5     8
3     3   6     9



B.
(I can't even ...)




On May 2, 2016, at 1:27 PM, jpm miao <miaojpm at gmail.com> wrote:

> Hi,
> 
>   Could someone suggest a way to rename a data frame column? For example,
> I want to rename the column beta, but I can't do it this way
> 
>> d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
>> mm<-"beta"
>> rename(d, c(mm="two", "gamma"="three"))
> The following `from` values were not present in `x`: mm
>  alpha beta three
> 1     1    4     7
> 2     2    5     8
> 3     3    6     9
> 
> ********
>     Of course this would work
> 
>> rename(d, c("beta"="two", "gamma"="three"))
>  alpha two three
> 1     1   4     7
> 2     2   5     8
> 3     3   6     9
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list