I have a dataframe such as ;
Comparison Distance
A,B 2
A,C 30
A,D 32
A,E 34
B,C 31
B,D 40
B,E 5
C,E 2
C,D 6
D,E 7
and I have two list such as ;
Group_1 <- c("A","B")
Group_2 <- c("C","D","E")
and I would like to generate three new lists where I put all the Comparison
between the element in Group_1
and Group_2
in a List1_G1vsG2, one List2_G1vsG1 with comparison within element of Group_1
and on in List3_G2vsG2 for comparison within elements of Group_1
.
I shoul then get the following lists:
List1_G1vsG2
c(30,32,34,31,40)
List2_G1vsG1
c(2)
List3_G2vsG2
c(5,2,6,7)
Here is the dataframe in dput()
format if it can help
structure(list(Comparison = c("A,B", "A,C", "A,D", "A,E", "B,C",
"B,D", "B,E", "C,E", "C,D", "D,E"), Distance = c(2, 30, 32, 34,
31, 40, 5, 2, 6, 7)), class = "data.frame", row.names = c(NA,
-10L))
CodePudding user response:
A possible solution:
library(tidyverse)
df %>%
separate(Comparison, into = c("C1", "C2")) %>%
mutate(G = case_when(C1 %in% Group_1 & C2 %in% Group_1 ~ 1,
C1 %in% Group_2 & C2 %in% Group_2 ~ 2,
C1 %in% Group_1 & C2 %in% Group_2 ~ 3)) %>%
group_by(G) %>% group_split %>%
map(pull, Distance) %>% set_names(c("List2_G1vsG1", "List3", "List1_G1vsG2"))
#> $List2_G1vsG1
#> [1] 2
#>
#> $List3
#> [1] 2 6 7
#>
#> $List1_G1vsG2
#> [1] 30 32 34 31 40 5
CodePudding user response:
Maybe this is beyond the scope of your request. You can customize a distance matrix computation function. By this way, you can determine whether or not the distance between A and B is equal to that of B and A.
f <- function(x, y, dist.df){
pair <- outer(x, y, paste, sep = ",")
dist <- dist.df[[2]][match(pair, dist.df[[1]])]
matrix(dist, length(x), dimnames = list(x, y))
}
f(Group_1, Group_2, df)
# C D E
# A 30 32 34
# B 31 40 5
f(Group_1, Group_1, df)
# A B
# A NA 2
# B NA NA
f(Group_2, Group_2, df)
# C D E
# C NA 6 2
# D NA NA 7
# E NA NA NA