r - from for loop to apply -


I'm new to using R. So I'm sure how to implement it. I would like to do with the speed of my function:

 for  (i in 1: ncol (exp)) {for in j: length (fe)) {tmp = True id = Strsplit [i], "\\.") If (id == fe [j]) {tmp = FALSE} if (tmp == true) {only = cbind (only, c ( Name ( 

Edit:

P>

Thanks a lot for very good details and sorry for my bad description All of you feel right, but when you wanted to delete matches in FA Cup

  exp & lt; - data.frame (x = 1: 10, by = 10: 1, CG = 11: 20, Ez = 20 : 11) fe & lt; -LETTERS [1: 2]   

Then the result should be colnames with only 'C' else everything else should be removed.

  1 cz 2 11 3 12 4 13 5 14 6 15 7 16 8 17 9 18 10 19 11 20    

Edit: If you want to delete only the column whose name appears in the FE, then you can simply:

  XP> - Data .frame (x = 1: 10, by = 10: 1, cs = 11: 20, as = 20: 11) FAF & lt; -LETTERS [1: 2] id & lt; - sapply (strsplit (name (exposure), "\\."), Function (i)! I [1]% in% fu) Ex Update) as well as loop, only very efficient you do not need a loop through the face, the % in%  function is vectorated.  

If the name appears anywhere between the dots,

  id & lt; - sapply (name (Exposure), "\\."), Function (i) Yoga (% F in i%) == 0)   

It does very strange things, and I have no indication that you are actually trying to do. For one, returns strsplit , so id == fe [j] will always change false, unless fe [j] Do not have a list yourself And I doubt ... so I can correct your code as

  id = strsplit (colnames (exp) [i], "\\.") [[1 ]] [1]   

If you have all the things related to the dot or

  id = unlist (strsplit (colnames (exp) [] If you want to compare everything in the string, in that case, you want to % in%  

/ Code> instead of == .

Second, what you get is a character matrix, which essentially multiply the rows if all the elements [j] are unique, then you can also:

  only & lt; - rbind (name (exp), exp) only & lt; - do.call (cbind, loop (mat, function (x) matrix (rep (x (x, ncol (exp) -1), nrow = nrow (exp) +1)))   

Assume that your logic code means (as you have not applied some sample data, it is impossible to know), optimization runs:

  mat> - rbid (Name (expose), XP call (cbind, lapply (mat, function (x) {n & lt; - sum (% fe %% strsplit (x [1], "\\.") [[1]] [1]) Matrix (Representative (x, n), nrow = nrow (mat))})   

Note - If you are interested then call [fe] on fe Also appears - you can change the code from:

  do.call (cbind, lapply (mat, function (x) {n  - sum (! %% Unlist (Strsplit (x [1], "\\.))) Matrix (representative (x, n), nro = no (mat))})   

If this is what you want , Then your code 't either do I have checked with the following sample data, and all give the same result:

  exp & lt; - data .frame (ax = 1: 10, by = 10: 1, cz = 11: 20, edge = 20: 11) FAF & lt; - Characters [1: 4]    

Comments