dataframe - Reshape data in R: merge by rows and rows to columns -


this question has answer here:

so - have data.frame looks this:

id   snpindex   a1  a2 id1  1      b id1  2   b    b id1  3      b id2  1      b id2  2   b    b id2  3      id3  1   b    b .... 

and this:

id 1_a1 1_a2 2_a1 2_a2 3_a1 3_a2 id1    b    b    b      b id2    b    b    b      id3 ... 

i.e. 1 row each id , 2 columns each snpindex - each column 1 a1/a2 value.

i appreciate help!

i'm sure a) duplicate , b) code can simplified appears after

dat <- data.frame( id = c("id1" , "id2" , "id3") ,                    snpindex = c(1,2,3) ,                     a1 = c("a", "b" , "a") ,                    a2 = c("b" , "b" , "b") , stringsasfactors = f)  library(tidyr) library(dplyr)   dat %>%      gather( key, value , a1, a2) %>%      mutate( key = paste0(snpindex , "_", key)  ) %>%      select( -snpindex , - id) %>%      spread( key , value ) 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -