Using BASE R, I wonder how to answer the following question:
Are there any value on X
or Y
that occurs only in one row but not others? If yes, produce my desired output below.
f <- data.frame(id = c(rep("AA",4), rep("BB",2), rep("CC",2)), X = c(1,2,2,3,1,4,3,3),
Y = c(99,7,8,7,6,7,7,7))
Desired output:
list(BB = c(X = 4, Y = 6), AA = c(Y = c(99, 8)))
# $BB
# X Y
# 4 6
# $AA
# Y1 Y2 # Would be a plus if shows `Y Y` instead of `Y1 Y2`
# 99 8
There are two big ideas with this base approach:
data.frame
.Created on 2019-11-10 by the reprex package (v0.3.0)
Here's another base approach that may be less complicated:
OP brings up using
table()
in a hint.duplicated()
is very performant:And similar idea in data.table:
And similar idea in dplyr:
It's not pure functional programming but it is base R:
Data: