1

I have a list of column aliases I am creating based on a dataframe.

column_aliases <- c("colA" = "Column A", "colB" = "Column B", "colC" = "Column C")

I know that the names() function can be used to access "colA", "colB", and "colC"; however, is there a good function to access "Column A", "Column B", and "Column C"? I am having trouble finding one.

Thank you!

325
  • 594
  • 8
  • 21
  • Simply print `column_aliases`? Like `column_aliases[2]` returns `"Column B"`. – Martin Gal Sep 01 '23 at 12:02
  • I'd like to have a list of all the column aliases, like how the `names()` function returns a list of all the original names @MartinGal – 325 Sep 01 '23 at 12:03

2 Answers2

2

After reading your response to Martin Gal's comment maybe you mean something like unname(column_aliases)?

unname(column_aliases)
[1] "Column A" "Column B" "Column C"
pookpash
  • 738
  • 4
  • 18
1

Some more approaches -

  1. as.vector
as.vector(column_aliases)
#[1] "Column A" "Column B" "Column C"
  1. c()
c(column_aliases, use.names = FALSE)
#[1] "Column A" "Column B" "Column C"
  1. c(t())
c(t(column_aliases))
#[1] "Column A" "Column B" "Column C"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Great answer. I really like 2. because it is quite clear what you are doing and why. 1. is ok but if glanced over it I would probably think there is some "bigger" transformation happening. 3. is probably gonna confuse me quite a bit when I read the code. – pookpash Sep 01 '23 at 13:15