[R] Alternative to lops

Ek Esawi e@@w|ek @end|ng |rom gm@||@com
Thu Apr 4 18:41:43 CEST 2019


Hi All--

Sorry i sent the one inadvertently

Her is a sample of my data. A data frame (MyDF) and a list (MyList).  My
own data frame has over 10,000 rows. I want to find out which elements of
MyDF$B contain any element(s) of MYList; then change MyDF$C to the name of
the vector of the list that has match.

I solved this via loops and if statements, using &in&  but I am hoping for
a better solution using the apply family functions. I tried something like
this but did not work.

lapply(strsplit(MyDF$B," "),function(x) lapply(MyList,function(y)  if(sum(y
%in% x)>0,x$Code==y[[1]]))

Thanks in advance--EK

My Sample data

> MyDF

    A     B        C
1 1 aa ab ac  0
2 2 bb bc bd  0
3 3    cc cf     0
4 4       dd     0
5 5       ee     0

> MyList

$X
[1] "a"  "ba" "cc"

$Y
[1] "abs" "aa"  "BA"  "BB"

$z
[1] "ab" "bb" "xy" "zy" "gh"



Desired results.



> MyDF

A        B   C
1 1 aa ab ac Y
2 2 bb bc bd Y
3 3    cc cf    X
4 4       dd     0
5 5       ee     0

	[[alternative HTML version deleted]]



More information about the R-help mailing list