-2

So I have the following dataframe:

> data.frame(id=c(1,2),a=c(10,11),b=c(20,21))  
id  a  b
1 10 20
2 11 21

I want to create a denormalized dataframe that takes the values of two or more columns and creates two columns, one for the column that the value comes from, and other with the value itself, so the final dataframe has more rows than columns.

The result should look like this:

> data.frame(id=c(1,1,2,2),x=c(10,20,11,21),type=c("a","b","a","b"))
    id  x type
    1 10    a
    1 20    b
    2 11    a
    2 21    b

Where the order of the values does not matter, of course.

Thanks everyone!

Daniel Díez
  • 113
  • 12

1 Answers1

1

Using tidyverse approach:

library("tidyverse")

your_df <- data.frame(id=c(1,2),a=c(10,11),b=c(20,21))

your_df_long_form <- your_df %>% 
    gather(key = type, value = x, a:b) %>% 
    arrange(id)

and this the output:

  id type  x
1  1    a 10
2  1    b 20
3  2    a 11
4  2    b 21
Scipione Sarlo
  • 1,470
  • 1
  • 17
  • 31