2

I am trying to avoid looping by using an documented apply function, but have not been able to find any examples to suit my purpose. I have two vectors, x which is (1 x p) and y which is (1 x q) and would like to feed the Cartesian product of their parameters into a function, here is a parsimonious example:

    require(kernlab)
    x = c("cranapple", "pear", "orange-aid", "mango", "kiwi", 
           "strawberry-kiwi", "fruit-punch", "pomegranate")
    y = c("apple", "cranberry", "orange", "peach")

    sk <- stringdot(type="boundrange", length = l, normalized=TRUE)
    sk_map = function(x, y){return(sk(x, y))}

I realize I could use an apply function over one dimension and loop for the other, but I feel like there has to be a way to do it in one step... any ideas?

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
mlegge
  • 6,763
  • 3
  • 40
  • 67

2 Answers2

1

Is this what you had in mind:

sk <- stringdot(type="boundrange", length = 2, normalized=TRUE)

# Create data frame with every combination of x and y
dat = expand.grid(x=x,y=y)

# Apply sk by row
sk_map = apply(dat, 1, function(dat_row) sk(dat_row[1],dat_row[2]))
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • `apply` will only work when the data types of `x` and `y` are identical (or if it's ok to coerce them to be the same). The more general case is `mapply(fn, dat[[1]], dat[[2]]))` – MrFlick May 19 '14 at 23:03
  • In this case, the function is comparing strings for matching substrings, so I don't think that will a problem here (for example, the function works fine if one column is a factor or character and the other is numeric), but that's good to know for future reference. – eipi10 May 19 '14 at 23:09
  • Right, in this case it will work. But as the title of the question suggests a more general solution, i wanted to make sure this was included as well. – MrFlick May 19 '14 at 23:15
  • I benchmarked these two answers and this method seems to have an advantage for the magnitude of data I need to use it with. Thank you! – mlegge May 20 '14 at 13:37
1

You can use the outer function for this if your function is vectorized, and you can use the Vectorize function to create a vectorized function if it is not.

outer(x,y,FUN=sk)

or

outer(x,y, FUN=Vectorize(sk))
Greg Snow
  • 48,497
  • 6
  • 83
  • 110