0

I try to follow the tidyverse approach and I extracted numerical data as tibble. As a matrix I would just transpose the data. There seems to be the function tribble in tibble for this but I can not make it work.

How can I set the column names in the call? I can't see it from the help.

library(tidyverse)
iris = as_data_frame(iris)
iris = select(iris,-Species)

tribble(iris)

gives the error

Error: no column names detected in 'tribble()' call

PS: I think there should be an easier way than doing this.

Community
  • 1
  • 1
Richi W
  • 3,534
  • 4
  • 20
  • 39
  • Judging by the examples in the help file `?tribble`, I don't think this function has anything to do with transposing `tibble` objects (or any other type of existing object). – nrussell Jan 18 '17 at 12:49
  • @nrussell [here](https://blog.rstudio.org/2016/08/29/tibble-1-2-0/) they say that tribble stands for transpose tibble ... and I guess that's why there is an "r" for tibble transpose .. tribble ... – Richi W Jan 18 '17 at 12:53
  • @nrussell any other idea how to transpose a numerical tibble without making it a matrix first? – Richi W Jan 18 '17 at 12:53
  • Regardless of the origins of the name, if the function were intended to be used as `transposed_tibble_object <- tribble(tibble_object)` I'm pretty sure there would be at least one example of that in the help file. As for your question, calling `t(tibble_object)` will dispatch `t.data.frame`. Does that not produce the output you are looking for? – nrussell Jan 18 '17 at 12:57
  • @nrussell yes I can just use `t()` ... I just thought that there were something more convenient. I think I have to tall `as_data_frame` again to be back at a tibble. – Richi W Jan 18 '17 at 13:03
  • 1
    Maybe just define `t.tbl_df <- function(x) { as_data_frame(t.data.frame(x)) }` as a work around. – nrussell Jan 18 '17 at 13:14

1 Answers1

2

tribble is used to create a tibble, but enter the information row-wise (thus, tribble) instead of column-wise. These two are identical:

a <- tribble(
  ~colA, ~colB, 
  "a",   1,
  "b",   2, 
  "c",   3
)

b <- tibble(colA = c("a", "b", "c"), colB = c(1:3))

is.tibble(a) and is.tibble(b) both produce TRUE.

As suggested in the comments by nrussell, you can transpose your tibble with t()

library(tidyverse)    
iris_t <- iris %>% t %>% as_tibble
s-heins
  • 679
  • 1
  • 8
  • 20