问题描述:

I would ask a question. It will be very helpfull if you mind to try.. Thankyou

I have a vector here....

[1] "I1,I2" "I1,I3" "I1,I4" "I1,I5" "I2,I3" "I2,I4"

[7] "I2,I5" "I3,I4" "I3,I5" "I4,I5"

Then, I wanna match this by the vector below...

[1] "I1,I2,I5" "I2,I4" "I2,I3"

[4] "I1,I2,I4" "I1,I3" "I2,I3"

[7] "I1,I3" "I1,I2,I3,I5" "I1,I2,I3"

hits <- sapply(1:length(a.new.list), function(j) pmatch(result,a.new.list[j]))

colnames(hits) <- a.new.list

rownames(hits) <- result

apply(hits,1, sum,na.rm=TRUE)

I1,I2 I1,I3 I1,I4 I1,I5 I2,I3 I2,I4 I2,I5 I3,I4 I3,I5 I4,I5

4 2 0 0 2 1 0 0 0 0

But what i expected is not in the result.

I1,I2 I1,I3 I1,I4 I1,I5 I2,I3 I2,I4 I2,I5 I3,I4 I3,I5 I4,I5

4 4 1 2 4 1 2 0 1 0

If The combination is not next to the one, the code said that it is not match...

But this is not what i need.

Thanks for your help. Sincerely

网友答案:

This approach uses the used melt.list method from reshape2. After creating two data frames from the string split we merge on the string and check for the number of matching groups. The code is tailored for search pairs. If the lengths change they must be changed at len:

library(reshape2)
len <- 2
dfs <- lapply(list(result, a.new.list), 
               function(x) melt(strsplit(x, ",")))
m <- merge(dfs[[2]], dfs[[1]], by=1)
f <- function(n) sum(aggregate(value~L1.y, m[m$L1.x == n,], 
               function(x) length(unique(x)) == len )$value)
setNames(sapply(1:length(a.new.list), f), a.new.list)
#I1,I2 I1,I3 I1,I4 I1,I5 I2,I3 I2,I4 I2,I5 I3,I4 I3,I5 I4,I5 
#    4     4     1     2     4     2     2     0     1     0

Data

a.new.list <- scan(what="character", text='"I1,I2" "I1,I3" "I1,I4" "I1,I5" "I2,I3" "I2,I4" "I2,I5" "I3,I4" "I3,I5" "I4,I5"')
result <- scan(what="character", text=' "I1,I2,I5"    "I2,I4"       "I2,I3"      
 "I1,I2,I4"    "I1,I3"       "I2,I3"      
                "I1,I3"       "I1,I2,I3,I5" "I1,I2,I3"  ')
相关阅读:
Top