3
votes

Consider a data frame that captures values associated with a given Cluster / Feature pair:

library(tidyverse)

set.seed(100)
X <- data_frame(Cluster = rep(1L:3L,2),
                Feature = rep(c("A","B"), each=3),
                Values  = map(rep(11:13,2), rnorm) )
# # A tibble: 6 x 4
#    Cluster Feature Values
#      <int> <chr>   <list>
#  1       1 A       <dbl [11]>
#  2       2 A       <dbl [12]>
#  3       3 A       <dbl [13]>
#  4       1 B       <dbl [11]>
#  5       2 B       <dbl [12]>
#  6       3 B       <dbl [13]>

I'm interested in creating a new column that, for any given Cluster / Feature pair, consolidates all values of this feature that are in other clusters. For example, the first entry in such a Not In Cluster (NIC) column should contain the 25 values that are associated with Feature A in Clusters 2 and 3.

The following loop over rows will produce the correct answer:

X$NIC <- map( 1:nrow(X), ~c() )
for(i in 1:nrow(X) ) {
  cl <- X$Cluster[i]
  f  <- X$Feature[i]
  X$NIC[[i]] <- filter( X, Cluster != cl, Feature == f ) %>%
                  pull(Values) %>% unlist
}
# # A tibble: 6 x 4
#   Cluster Feature Values     NIC
#     <int> <chr>   <list>     <list>
# 1       1 A       <dbl [11]> <dbl [25]>
# 2       2 A       <dbl [12]> <dbl [24]>
# 3       3 A       <dbl [13]> <dbl [23]>
# 4       1 B       <dbl [11]> <dbl [25]>
# 5       2 B       <dbl [12]> <dbl [24]>
# 6       3 B       <dbl [13]> <dbl [23]>

## Spot-checking
with( X, identical(NIC[[1]], unlist(Values[2:3])) )      # TRUE
with( X, identical(NIC[[5]], unlist(Values[c(4,6)])) )   # TRUE

I was wondering if there's a cleaner way of doing this with dplyr tools. I feel like this is a perfect setup for a group_by solution, but it seems that there needs to be some "cross-talk" between groups for it to work.

1

1 Answers

3
votes

The key is to not group by Cluster, since you want to iterate over clusters within Features.

library(dplyr)
library(purrr)

mutate(group_by(X, Feature),
       NIC = map(1:n(), ~ flatten_dbl(Values[-.])))
# # A tibble: 6 x 4
# # Groups:   Feature [2]
#   Cluster Feature Values     NIC       
#     <int> <chr>   <list>     <list>    
# 1       1 A       <dbl [11]> <dbl [25]>
# 2       2 A       <dbl [12]> <dbl [24]>
# 3       3 A       <dbl [13]> <dbl [23]>
# 4       1 B       <dbl [11]> <dbl [25]>
# 5       2 B       <dbl [12]> <dbl [24]>
# 6       3 B       <dbl [13]> <dbl [23]>