Lapply with gregexpr and character vector

I am new to using lapply etc. My code and data follows ...

b  = c( "ZFW", "ZTL" )

      

I have a vector with a large character, dfNames (length = 147), which contains instances like ...

dfNames[ 113 ] "arr_to_KEWR_in_ZFW"
dfNames[ 147 ] "arr_to_KEWR_in_ZTL"

      

When I apply this code ...

indx = which( unlist( lapply( paste( '_in_', b, sep = '' ), function(v){gregexpr( v, dfNames )} ) ) != -1 )

      

I am getting a list of integers in indx that is at least 294 long ...

6  41  58  75 101 118 135 165 200 217 234 260 277 294

      

which creates on dfNames [indx] ...

"all_in_ZFW" "dep_from_KCLT_in_ZFW" "dep_from_KDFW_in_ZFW" "dep_from_KEWR_in_ZFW"
"arr_to_KCLT_in_ZFW"   "arr_to_KDFW_in_ZFW"   "arr_to_KEWR_in_ZFW"   NA                    
NA                     NA                     NA                     NA                    
NA                     NA   

      

So obviously my use of lapply caused R to wrap around and I am not returning indices from dfNames that contain pattern = 'ZTL'.

Sorry for the dumb question.

+3


source to share


1 answer


What I understand you are looking at



b = c("ZFW", "ZTL" )
lapply(b, function(x) grepl(x, list))

      

0


source







All Articles