I want to find the common characters from the list of character vectors. Below is the example through which problem can be solved. But i have list which has like 100 character vectors and i want to use for loop to iterate over the loop and get the common character items

ab <- c ("a", "b", "c", "g")
cd <- c("a" ,"d", "c", "e", "f")
ef <- c("a", "c", "i", "r", "t")
dat <- list(ab,cd,ef)

Reduce(intersect, dat)
[1] "a" "c"

Edit: My problem is i am having a large list with the character vectors nested in it. that is the reason i want to use for loop to iterate over the list and find the common items from the character vectors.

I tried something like this shown below but i am not getting any output

for (i in 1: seq_along(dat))
{
   op <- Reduce(intersect, dat[[i]])
}

1 Answers

-1
Abhijeet Patil On

The following code can be used to solve the problem using the for loop:

ab <- c ("a", "b", "c", "g")
cd <- c("a" ,"d", "c", "e", "f")
ef <- c("a", "c", "i", "r", "t")
dat <- list(ab,cd,ef)

op <- vector()
for (i in seq_along(dat)) 
{
  op[i] <- list(dat[[i]])  
}
op.final <- Reduce(intersect, op)