Find row numbers by looping through a list

Hello,
I made a list, it look like the one below.
Besides I have a textfile read with read.table.

#the list looks like this:
print(koppeling.id)
"LPI" "LQI" "LRI" "LSI"

#the data looks like this:
print(Lateral.dat)
1 FLBR id LPI sc 0 47150 flbr
2 FLBR id STI sc 0 26940 flbr
3 FLBR id LQI sc 0 88920 flbr
4 FLBR id CSO_63A sc 0 lt 0 28220

I would like to get all the row numbers from the text file which have a string as found in the list. I have tried to make a loop but I get the error as below.

> for (i in koppeling.id) {
+   which(grepl(i, Lateral.dat$V1))}
Warning message:
In grepl(i, Lateral.dat$V1) :
  argument 'pattern' has length > 1 and only the first element will be used
> 

Hopefuly anyone knows how to fix this problem?

You don't need a for loop since these functions are vectorized.

koppeling.id <- c("LPI", "LQI", "LRI", "LSI")

Lateral.dat <- data.frame(col = c("LPI", "STI", "LQI", "CSO_63A"))

which(grepl(paste0(koppeling.id, collapse = "|"), Lateral.dat$col))
#> [1] 1 3

Created on 2020-07-15 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.