Entering edit mode
5.4 years ago
xxxxxxxx
▴
20
My file is like this-
Pcol Mcol
P1 M1,M2,M5,M6
P2 M1,M2,M3,M5
P3 M4,M5,M7,M6
I want to find the combination of Mcol elements along with Pcol.
Expected output-
Pcol Mcol
P1 M1,M2
P2 M1,M2
P1 M1,M5
P2 M1,M5
P1 M1,M6
P1 M2,M5
P2 M2,M5
P1 M2,M6
P1 M5,M6
P3 M5,M6
P2 M1,M3
P2 M2,M3
P3 M4,M5
P3 M4,M7
P3 M4,M6
P3 M7,M6
I have tried this-
x <- read.csv("file.csv" ,header = TRUE, stringsAsFactors = FALSE)
xx <- do.call(rbind.data.frame,
lapply(x$Gcol, function(i){
n <- sort(unlist(strsplit(i, ",")))
t(combn(n, 2))
}))
But it only gives output of combination not the Pcol elements.
Related post:
xxxxxxxx
It is bad etiquette to ask the same question multiple times.