Search code examples
rtidyrspread

tidyr spread subset of key-value pairs


Given the example data, I'd like to spread a subset of the key-value pairs. In this case it is just one pair. However there are other cases where the subset to be spread is more than one pair.

library(tidyr)
# dummy data
> df1 <- data.frame(e = c(1, 1, 1, 1),
              n = c("a", "b", "c", "d") ,
              s = c(1, 2, 5, 7))
> df1
  e n s
1 1 a 1
2 1 b 2
3 1 c 5
4 1 d 7

Classical spread of all key-value pairs:

> df1 %>% spread(n,s)

  e a b c d
1 1 1 2 5 7

Desired output, spread only n=c

  e c n s
1 1 5 a 1
2 1 5 b 2
3 1 5 d 7

Solution

  • We can do a gather after the spread

    df1 %>%
      spread(n, s) %>% 
      gather(n, s, -c, -e)
    #  e c n s
    #1 1 5 a 1
    #2 1 5 b 2
    #3 1 5 d 7
    

    Or instead of spread/gather, we filter without the 'c' row and then mutate to create the 'c' column while subsetting the 's' that corresponds to 'c'

    df1 %>% 
       filter(n != "c") %>% 
       mutate(c = df1$s[df1$n=="c"])