0

Explaining by example:

I have two list objects, List A and List B.
Each list has same index range, from 1:100.

I want to apply a function to 'like' elements within each list - e.g.,

function of (a submatrix within A[[1]] and a submatrix within B[[1]]) gives
--> result1

function of (a submatrix within A[[2]] and a submatrix within B[[2]]) gives --> result2

...

function of (a submatrix within A[[100]] and a submatrix within B[[100]]) gives --> result100

As far as I can tell:
-- lapply isn't appropriate (it only applies a function to one list);
-- mapply isn't appropriate either (applies a function to multiple lists, but the function illustrated here takes in the 'like' elements of multiple lists);

I could pain you with other failed approaches, but instead I'll thank you in advance for any insights! :)

  • 1
    You need to give more details about the function you're using. Why doesn't `mapply` work? – C_Z_ Feb 12 '16 at 22:08
  • mapply applies a function to multiple lists, is my understanding – Fred Oswald Feb 13 '16 at 03:34
  • The function is one that I'll make up (it will operate on 2 matrices pulled out of 2 list elements, one from List A; the other from List B). My understanding is that mapply applies a function to multiple lists. But instead, I have two list objects, A and B, each with indices that range from 1 to N. I want to be able to apply a user function to 'like' indices N times, as sketched out above. Hope this clarifies, thanks for posting! – Fred Oswald Feb 13 '16 at 03:41
  • like this? `A <- B <- list(list(1, matrix(1:5)), list(2, matrix(5:1))); mapply(function(x, y) x[[2]] ** y[[2]], A, B, SIMPLIFY = FALSE)` it would be easier to help you if you also added an example of A and B with desired results – rawr Feb 13 '16 at 04:51
  • Thanks rawr, I think this might be it I don't have the functios written actually... :) but what it will do it take a submatrix from an indexed element from List A, a submatrix from the 'like' indexed element from list B, and then the function operates on the two submatrices to produce a vector. So given indices 1 to N, I want to apply the function N times and get N vectors. – Fred Oswald Feb 13 '16 at 16:20
  • hm, still not sure if this code is it - I'll noodle with it – Fred Oswald Feb 13 '16 at 16:27
  • @FredOswald Sorry, I still don't understand what you're trying to do. Maybe try to provide a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – C_Z_ Feb 13 '16 at 17:34
  • It's hard to provide a reproducible example, because the question concerns how to write the code. Let me try re-explaining by coming up with the simplest example: List A contains three 2x2 matrices. List B contains three 2x4 matrices. FUN is a function that matrix-multiplies a 2x2 matrix by a 2x4 matrix. Now I want to be able to return three things: A[[1]]*B[[1]], A[[2]]*B[[2]], and A[[3]]*B[[3]], which is done by applying FUN three times to the 'like' elements of List A and List B. Thanks! – Fred Oswald Feb 13 '16 at 21:14
  • 1
    Just provide us with the "input" data structure, what you tried and what is the expected result. However upon a cursory read of your question and comments it appears you will solve the problem with `mapply`, which takes corresponding elements from all object specified in `...` and applies a function from `FUN`. – Roman Luštrik Feb 14 '16 at 11:17

1 Answers1

0

If I got your question:

C1=1:10
C2=4:8
C=C1 %in% C2
C

# [1] FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE FALSE

Hossein Vatani
  • 1,381
  • 14
  • 26