Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Find variables that occur only once across a split data.frame in R

$
0
0

Using BASE R, I wonder how to answer the following question:

Are there any value on X or Y (i.e., variables of interest names) that occurs only in one element in m but not others? If yes, produce my desired output below.

For example: Here we see X == 4 only occurs in element m[[2]] but not m[[1]] and m[[3]]. Here we also see Y == 99 & 8 only occur in m[[1]] but not others. Here we also see Y == 6 only occurs in m[[2]] but not others.

Note: the following is a toy example, a functional answer is appreciated. AND X& Y may or may not be numeric (e.g., be string).

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))

m <- split(f, f$id) # Here is `m`

mods <- names(f)[-1] # variables of interest names

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

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>