Given the following data frame:
z1 z2
1 A X
2 A Y
3 B X
4 B Y
5 C X
6 C Z
7 D X
8 D Z
9 E X
10 E Y
11 F X
12 G Z
13 H X
14 I Y
15 J X
16 K Z
I am trying to find a more efficient (than what I've come up with) way to eliminate the duplicated values in the first column, z1, given the value in the second column, z2, is not a specified value "X". This is the output I'm after:
z1 z2
1 A X
3 B X
5 C X
7 D X
9 E X
11 F X
12 G Z
13 H X
14 I Y
15 J X
16 K Z
There are several posts here (and elsewhere) about eliminating duplicates based on multiple columns and I HAVE tried various forms of duplicated() and unique() but can not seem to hit on the right coding that does this. This problem is a little different from other posts I've seen in that the rows to eliminate are based on a duplicated value existing in z1 and is conditioned on the value in z2 but the conditional on z2 does not apply when no duplicate exists in z1. I have come up with the following solution using subset() but the problem is that I need to input the values from z1 that are duplicated to make it work. My current solution is inefficient because I need to find the duplicated values through another procedure first and then hard code them in to the subset command.
This is the data frame and code I've been working with:
z1=c(rep(c("A","B","C","D","E"),each=2),"F","G","H","I","J","K")
z2=c(rep(c("X","Y"),2),rep(c("X","Z"),2),rep(c("X","Y","X","Z"),2))
z=data.frame(cbind(z1,z2))
t1=subset(z,
(z$z1!="A" | z$z2=="X")&
(z$z1!="B" | z$z2=="X")&
(z$z1!="C" | z$z2=="X")&
(z$z1!="D" | z$z2=="X")&
(z$z1!="E" | z$z2=="X"))
t1
Any thoughts?
You can use duplicated, with fromLast=F and fromLast=T to determine whether a z1 value is repeated:
duplicated(z$z1) | duplicated(z$z1, fromLast=T)
# [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE
# [14] FALSE FALSE FALSE
All that remains is to limit to solutions that are either non-duplicated or have X2 value of "X":
subset(z, !(duplicated(z1) | duplicated(z$z1, fromLast=T)) | z2 == "X")
# z1 z2
# 1 A X
# 3 B X
# 5 C X
# 7 D X
# 9 E X
# 11 F X
# 12 G Z
# 13 H X
# 14 I Y
# 15 J X
# 16 K Z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With