Irakli Irakli - 1 year ago 303
R Question

R: How to transpose a dataframe in tidyverse?

Using the basic R, I can transpose a dataframe, say mtcars:

Or with pipes:

mtcars %>% t %>%

How to accomplish the same within tidyr or tidyverse packages?

My attempt below gives "Error: Duplicate identifiers for rows"

mtcars %>% gather(var, value, everything()) %>% spread(var, value)

Answer Source

Try with add_rownames

add_rownames(mtcars) %>% 
         gather(var, value, -rowname) %>% 
         spread(rowname, value) 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download