This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Covert Many Lines In A Specific Line

0 down vote favorite

I would like to transform this data:

Sample  Genotype  Region
sample1    A      Region1
sample1    B      Region1
sample1    A      Region1
sample2    A      Region1
sample2    A      Region1
sample3    A      Region1
sample4    B      Region1

In that format:

Sample  Genotype  Region   
sample1    E      Region1
sample2    A      Region1
sample3    A      Region1
sample4    B      Region1

I wanna to tag excluded (E) in "Genotype" column in an unified line to samples with more than one genotype (sample1) and just unify lines to samples with genotype repeated in two lines (sample2). I have one list with many regions (Region1 - Regionx). It is possible to do in R software? Thanks a lot.

r

1 answer

Given the above in a data.frame called d:

d2 <- unique(d) #Collapse duplicates, e.g., "sample2"
d2$Genotype <- factor(d2$Genotype, levels=c(levels(d2$Genotype), "E")) #Add a level "E" to Genotype
d2[duplicated(d2$Sample),2] <- "E" #Label "E" lines
d2 <- d2[-duplicated(d2$Sample, fromLast=T)==F,] #Remove the non-labeled "E" lines that should still be excluded

Log in to answer this question.