This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Modifying dataframe in R based on Unique values in 2nd column

Hi,

I have a dataframe like this I want to keep unique values in second column and get rid of any repeating values. Problem is the first column has different names (below) The problem below is that both Fox and cat has sample ID Fam101. I just keep one of them any one of them.

Animals SampleID

Cat Fam101

Fox Fam100

Cat Fam30

Fox Fam101

Fox Fam60

Wolf Fam30

Both results below are acceptible

Animals SampleID

Fox Fam100

Cat Fam30

Fox Fam101

Fox Fam60

Wolf Fam30

Animals SampleID

Cat Fam101

Fox Fam100

Cat Fam30

Fox Fam60

Wolf Fam30

r dataframe

1 answer

I want to keep unique values in second column and get rid of any repeating values

But in both of your expected results the "Fam30" is not removed, which is a repeating value. Is that a mistake?

If so, I think the following code may help

animal_samples <- data.frame(a = c(rep(c("cat","fox"),2),"fox","wolf"),
                             b = paste0("Fam",c('101','100','30','101','60','30')))
animal_samples <- animal_samples[!duplicated(animal_samples$b),]

Thanka, yes I expected to drop fam30 too. That worked ....

Log in to answer this question.