如何应用“复杂”用户定义的函数在一个tibble的每个元素上 [英] How to apply a "complicated" user defined function on each element of a tibble

查看:141
本文介绍了如何应用“复杂”用户定义的函数在一个tibble的每个元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个(看似简单的)问题的答案进行了高低调查,但是空洞了,所以我希望有人能够帮助我或者指引我朝着正确的方向前进。

我有一个相当复杂的子模型,我想要应用于数据集,但是如果我只是使用use mutate,我会得到一个错误变量必须是长度为1或21。添加rowwise()似乎不会影响它。



让我用下面这个愚蠢的例子来说明这个问题: (x = 10:20,y = c(a,b,a,b), b,a,b,a,b,a,b,a))

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

ComplicatedFunction< - 函数(mystaticData,myx,myy){
#创建基表
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
myCalculatedThi ng< - sum(myBaseTable $ zCumulative)

return(myCalculatedThing)
}

#这是我想要做的事
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工程(但将是愚蠢的,因为我想为每行评估函数)
myData%>%mutate(newcol = ComplicatedFunction(mystaticData = staticData,
myx = 15,
myy =a))

#这不再有效,但我不明白我在做什么错误
myData%>% mutate(newcol = ComplicatedFunction(mystaticData = staticData,
myx = x,
myy =a))

#我尝试了rowwise(),但这似乎不起作用
myData%>%rowwise()%>%mutate(newcol = ComplicatedFunction(mystaticData = staticData,
myx = x,
myy = y))

我希望有人能向我解释我在这里做错了什么。



Sylvain

解决方案

您可以通过使用 partial 创建一个新函数来实现它:

  library(purrr )
newCF< - partial(ComplicatedFunction,mystaticData = staticData)
myData%>%rowwise()%>%mutate(newcol = newCF(myx = x,
my y = y))


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

解决方案

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))

这篇关于如何应用“复杂”用户定义的函数在一个tibble的每个元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆