[R] Can't rename a data frame column
Thierry Onkelinx
thierry.onkelinx at inbo.be
Mon May 2 20:49:31 CEST 2016
Or you dplyr
library(dplyr)
d <- data.frame(alpha=1:3, beta=4:6, gamma=7:9)
mm <- "beta"
rename_(d, "two" = mm, "three" = "gamma")
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
2016-05-02 19:49 GMT+02:00 Boris Steipe <boris.steipe op utoronto.ca>:
> 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 op 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 op 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.
>
> ______________________________________________
> R-help op 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list