0

I have searched high and low for the answer to this (seemingly simple) problem, but came up empty so I hope someone can help me or point me in the right direction.

I have a fairly complicated submodel that I want to apply to a dataset, but if I just use use mutate I get an error Variables must be length 1 or 21. adding rowwise() doesnt seem to impact it.

Let me use the following silly illustration of the problem:

myData <- tibble(x=10:20, y=c("a", "b","a", "b","a", "b","a", "b","a", "b","a"))

staticData <- tibble(x=0:100, y=c("a"),f=x/100) %>% union (tibble(x=0:100, y=c("b"),f=x/1000))

ComplicatedFunction <- function(mystaticData, myx, myy) {
  #make the base table 
  myBaseTable <- tibble(
    y = myy,
    x = c(myx:(myx + 20)) 
  )
  #add  f rates
  myBaseTable <- left_join(myBaseTable,mystaticData)
  #add stuff
  myBaseTable <- myBaseTable %>% 
    mutate(z = 1 - (f * 0.8)) %>% 
    mutate(zCumulative = cumprod(z)) 
  #Calculate the thing
  myCalculatedThing <- sum(myBaseTable$zCumulative)

  return(myCalculatedThing)
}

#This is what I want to do 
myData %>% mutate(newcol = ComplicatedFunction(mystaticData = staticData, 
                                               myx = x, 
                                               myy = y))
#this works
ComplicatedFunction(mystaticData = staticData, 
                    myx = 19, 
                    myy = "b")
ComplicatedFunction(mystaticData = staticData, 
                    myx = 20, 
                    myy = "a")

#This works (but would be silly as I want the function to be evaluated for each line)
myData %>% mutate(newcol = ComplicatedFunction(mystaticData = staticData, 
                                               myx = 15, 
                                               myy = "a"))

#This no longer works, but I dont understand what I am doing wrong
myData %>% mutate(newcol = ComplicatedFunction(mystaticData = staticData, 
                                               myx = x, 
                                               myy = "a"))

#I tried rowwise(), but this doesnt seem to work either 
myData %>% rowwise() %>% mutate(newcol = ComplicatedFunction(mystaticData = staticData, 
                                               myx = x, 
                                               myy = y))

I hope someone can explain to me what I am doing wrong here.

Many thanks in advance!

Sylvain

Sylvain
  • 47
  • 5

1 Answers1

2

You can do it by creating a new function using partial:

library(purrr)
newCF <- partial(ComplicatedFunction, mystaticData = staticData)
myData %>% rowwise() %>% mutate(newcol = newCF(myx = x, 
                                           myy = y))
student
  • 1,001
  • 2
  • 12
  • 24
  • Many thanks @Student. that resolves the issue. I dont understand why it is necessary to add partial though (after checking help on it). If it is not too much trouble, could you point me to documentation explaining why this works. – Sylvain May 10 '17 at 07:27
  • `df %>% mutate` did not work because `ComplicatedFunction` is not a vectorized function. This why you want to use `rowwise` for example. The reason I proposed using `partial` is because `staticData` is fixed and should not be treated row-wise. So partial created a new function where staticData already filled in and now the new function only have the two "dynamical" variables `x` and `y`. (note that now when I re-run your code the last line of code works so the problem has disappeared and I'm not quite sure why so I can't comment more on the it.) – student May 11 '17 at 17:45