recursive program in r

193 views Asked by At

I am doing some computations, but am having a hard time wrining a program in r that accomplishes what I need.

x1<-c('a','b','c','d','a')
x2<-c('b','e','g')
x3<-c('c','a','h','j')
x4<-c('d','l','m','o','p','x','y','z')
x5<-c('f','q','a')

I am looking of a way to compute

y1<-length(intersect(x1,x2))
y2<-length(intersect(x3, union(x1,x2)))
y3<-length(intersect(x4, union(x3,union(x1,x2))))
y4<-length(intersect(x5, union(x4, union(x3,union(x1,x2)))))
1

There are 1 answers

0
akrun On BEST ANSWER

Using the modified code from @Frank

 nx <- 5L
 xx <- mget(paste0('x', 1:nx))
 cux <- Reduce(union, xx, accumulate=TRUE)
 lengths(Map(intersect, xx[-1], cux[-nx] ))