Purrr安全地创建列表列表
我已经使用Purrr安全地创建列表列表,r,dplyr,tidyr,tibble,unnest,R,Dplyr,Tidyr,Tibble,Unnest,我已经使用安全地捕获了我发出呼噜声时代码中出现的错误。然而,安全地得到的结果比我预期的要复杂得多 首先,我们创建必要的函数和示例数据 #base functions. SI_tall <- function(topheight, age, si ){ paramasi <- 25 parambeta <- 7395.6 paramb2 <- -1.7829 refAge <- 100 d <- parambeta*(paramasi^p
安全地
捕获了我发出呼噜声时代码中出现的错误。然而,安全地得到的结果比我预期的要复杂得多
首先,我们创建必要的函数和示例数据
#base functions.
SI_tall <- function(topheight, age, si ){
paramasi <- 25
parambeta <- 7395.6
paramb2 <- -1.7829
refAge <- 100
d <- parambeta*(paramasi^paramb2)
r <- (((topheight-d)^2)+(4*parambeta*topheight*(age^paramb2)))^0.5
## height at reference age
h2 <- (topheight+d+r)/ (2+(4*parambeta*(refAge^paramb2)) / (topheight-d+r))
return(abs(h2 - si))
}
new.topheight <- function(my.si, my.age){
optim(par = list(topheight = 10), ## this topheight is just an initial value
method = 'L-BFGS-B', fn = SI_tall, si = my.si, age = my.age, lower= 0, upper=100)$par
}
#Creating the function which will display errors.
safe_new.topheight <- safely(new.topheight)
#Creating data
my.age <- seq(0,100, by=0.2)
my.si <- c(15)
si.crossing <- tidyr::crossing(my.si, my.age) %>% data.frame()
#Creating the column to be unnested.
si.crossing2<- si.crossing %>%
mutate(height=map2(my.si,my.age, safe_new.topheight))
有没有办法把它平铺到柱子上:
我的,我的,年龄,身高,错误
非常感谢 区分安全
和可能
可能会有帮助<代码>安全
很高兴看到发生了什么错误(以及在哪里)。它最好与转置
一起使用,而不是在TIBLE
中使用可能
用于运行映射
函数,即使它遇到错误。它允许您在抛出错误时选择一个可选值,否则
# use `transpose` on the height column to turn the list inside out
# which results in two lists `result` and `error`
# first lets have a look at the structure
si.crossing2$height %>%
transpose %>%
str
# then `pluck` the `error` list and remove all elements which are NULL
# with `compact` - here you can see the error that occurred
si.crossing2$height %>%
transpose %>%
pluck("error") %>%
compact
# `safely` is a great function to see what went wrong
# but its not very useful inside a tibbles list-column
# what you actually want to use is `possibly`
possib_new.topheight <- possibly(new.topheight, otherwise = NA)
# this will not tell you what went wrong, but instead yield `NA`
# when an error is thrown - important to use `otherwise = NA`,
# the default is NULL, which makes the output list shorter and
# won't fit to your tibble
si.crossing3 <- si.crossing %>%
mutate(height = map2_dbl(my.si,my.age, possib_new.topheight))
si.crossing3
#> # A tibble: 501 x 3
#> my.si my.age height
#> <dbl> <dbl> <dbl>
#> 1 15 0 NA
#> 2 15 0.2 0.000693
#> 3 15 0.4 0.00206
#> 4 15 0.6 0.00390
#> 5 15 0.8 0.00639
#> 6 15 1 0.00947
#> 7 15 1.2 0.0131
#> 8 15 1.4 0.0172
#> 9 15 1.6 0.0218
#> 10 15 1.8 0.0269
#> # … with 491 more rows
#使用高度列上的“transpose”将列表翻过来
#这将导致两个列表'result'和'error'`
#首先让我们看一下结构
si.交叉2$高度%>%
转置%>%
str
#然后'polck'删除'error'列表并删除所有为空的元素
#使用“compact”-您可以在这里看到发生的错误
si.交叉2$高度%>%
转置%>%
拔毛(“错误”)%>%
契约
#“安全”是一个很好的功能,可以查看出了什么问题
#但是它在tibbles列表列中不是很有用
#你真正想用的是“可能”`
possib_new.topheight区分安全
和可能
,可能会有所帮助<代码>安全
很高兴看到发生了什么错误(以及在哪里)。它最好与转置
一起使用,而不是在TIBLE
中使用可能
用于运行映射
函数,即使它遇到错误。它允许您在抛出错误时选择一个可选值,否则
# use `transpose` on the height column to turn the list inside out
# which results in two lists `result` and `error`
# first lets have a look at the structure
si.crossing2$height %>%
transpose %>%
str
# then `pluck` the `error` list and remove all elements which are NULL
# with `compact` - here you can see the error that occurred
si.crossing2$height %>%
transpose %>%
pluck("error") %>%
compact
# `safely` is a great function to see what went wrong
# but its not very useful inside a tibbles list-column
# what you actually want to use is `possibly`
possib_new.topheight <- possibly(new.topheight, otherwise = NA)
# this will not tell you what went wrong, but instead yield `NA`
# when an error is thrown - important to use `otherwise = NA`,
# the default is NULL, which makes the output list shorter and
# won't fit to your tibble
si.crossing3 <- si.crossing %>%
mutate(height = map2_dbl(my.si,my.age, possib_new.topheight))
si.crossing3
#> # A tibble: 501 x 3
#> my.si my.age height
#> <dbl> <dbl> <dbl>
#> 1 15 0 NA
#> 2 15 0.2 0.000693
#> 3 15 0.4 0.00206
#> 4 15 0.6 0.00390
#> 5 15 0.8 0.00639
#> 6 15 1 0.00947
#> 7 15 1.2 0.0131
#> 8 15 1.4 0.0172
#> 9 15 1.6 0.0218
#> 10 15 1.8 0.0269
#> # … with 491 more rows
#使用高度列上的“transpose”将列表翻过来
#这将导致两个列表'result'和'error'`
#首先让我们看一下结构
si.交叉2$高度%>%
转置%>%
str
#然后'polck'删除'error'列表并删除所有为空的元素
#使用“compact”-您可以在这里看到发生的错误
si.交叉2$高度%>%
转置%>%
拔毛(“错误”)%>%
契约
#“安全”是一个很好的功能,可以查看出了什么问题
#但是它在tibbles列表列中不是很有用
#你真正想用的是“可能”`
可能是新的。谢谢,我总是忘了用哪一个!谢谢,我总是忘了用哪一个!