0
votes

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.

1
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
@nrussell here they say that tribble stands for transpose tibble ... and I guess that's why there is an "r" for tibble transpose .. tribble ...Richard
@nrussell any other idea how to transpose a numerical tibble without making it a matrix first?Richard
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
Maybe just define t.tbl_df <- function(x) { as_data_frame(t.data.frame(x)) } as a work around.nrussell

1 Answers

1
votes

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