Marie-Eve
Marie-Eve

Reputation: 585

How to reshape data from long to wide format so to achieve this output?

I'm having trouble rearranging the following data frame with tidyr package:

data <- data.frame(
    name = rep(c("John", "Mary", "Peter", "Sarah"), each=2),
    firm = c("a", "b", "c", "d", "a", "b", "c", "d"),
    rank = rep(1:2, 4),
    value = rnorm(8)
    )

I want to reshape it so that each unique "name" variable is a rowname, with the "values" as observations along that row and the "rank" as colnames followed by the "firm" name. Sort of like this:

  name          1      firm_1            2       firm_2
  John       0.3407997      a        -0.3795377      b
  Mary      -0.8981073      c       -0.5013782       d
  Peter     0.3407997       a        -0.3795377      b
  Sarah     -0.8981073      c       -0.5013782       d

Upvotes: 3

Views: 106

Answers (1)

kmacierzanka
kmacierzanka

Reputation: 825

We can use a combination of dplyr and tidyr, similarly to the posts in the comment by @aosmith.

library(dplyr) # [1] ‘1.0.0’
library(tidyr) # [1] ‘1.1.0’

data %>% pivot_wider(names_from = rank, values_from = c(firm, value)) %>%
        select(name, `1` = value_1, firm_1, `2` = value_2, firm_2)

In order to fully go from long to wide format we must take values_from not 1, but 2, columns, as the original data has 4, not 3, columns.

Upvotes: 1

Related Questions