Skip to content
Prev 342837 / 398506 Next

Compare data in two rows and replace objects in data frame

here is another way of doing it using 'tidyr' and 'dplyr'
+ 2471250    1    1    1
+ 2471250    0    0    0
+ 2433062    0    0    0
+ 2433062    1    1    1
+ 100021605    1    1    0
+ 100021605    1    0    1
+ 100005599    1    1    0
+ 100005599    1    1    1
+ 100002798    1    1    0
+ 100002798    1    1    1", header = TRUE, as.is = TRUE)
+       , `10` = "A"
+       , `01` = "B"
+       , `1-` = "Aht"
+       , `-1` = "Bht"
+       )
+     gather(key, val, -CloneID) %>%  # 'melt' the data
+     group_by(CloneID, key) %>%  # group by CloneID
+     summarise(newKey = paste0(val, collapse = '')) %>%  # add concat
to two rows
+     mutate(newVal = keyTrans[newKey]) %>%  # add the new value
+     select(-newKey) %>%  # remove newKey for output
+     spread(key, newVal)
Source: local data frame [5 x 4]

    CloneID genotype2001 genotype2002 genotype2003
1   2433062            B            B            B
2   2471250            A            A            A
3 100002798           HT           HT            B
4 100005599           HT           HT            B
5 100021605           HT            A            B

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Mon, Aug 4, 2014 at 2:21 PM, John McKown
<john.archie.mckown at gmail.com> wrote: