Modifying dataframe in R based on Unique values in 2nd column
1
0
Entering edit mode
3.6 years ago
salman_96 ▴ 70

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 • 806 views
ADD COMMENT
1
Entering edit mode
3.6 years ago
gglim ▴ 210

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),]
ADD COMMENT
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1809 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6