r - 为什么keep from purrr在map中返回NULL?
问题描述
我需要比较嵌套tibble
s 中的字符串。这是我的数据的样子:
library(tidyverse)
mydata = data.frame(
Months = c(1 ,1 ,1 , 2, 2, 2),
Strings = rep(c('1', '2', '3', '1', '3', '4'))
)
我需要返回上个月没有出现在当月的那个字符串。
我使用此代码来执行此操作。
output = mydata %>%
nest(Nested = -Months) %>%
arrange(Months) %>%
mutate(Lost= map(Nested, lag(Nested), .f = function(.CurrentMonth, .PreviousMonth){
length(keep(.PreviousMonth$Strings ,!.PreviousMonth$Strings %in% .CurrentMonth$Strings))
}))
但output$Lost
即使该行包含零
length(keep(output$Nested[[1]]$Strings, !output$Nested[[1]]$Strings %in% output$Nested[[2]]$Strings ))
工作正常并返回 1。
解决方案
map
用inside 替换会产生正确map2
的mutate
输出。
推荐阅读
- haskell - 为预先存在的类型自动生成“PersistEntity”
- php - woocommerce 从页面底部移动侧边栏
- ssl - 使用 Apache Camel 的 netty4-http 组件发出 Https 请求
- python - 减少 CNN 模型中的验证损失
- assembly - 如何在正常的 DLL 中找到“奇怪的”ROP 小工具?编译器永远不会这样做。(面向回报的编程)
- javascript - 如何替换正则表达式的最后一个匹配项?
- c# - 如何以一定的数据包大小和速度对我的 udp 服务器进行压力测试
- css - 为什么这个 flexbox 不能滚动?
- javascript - TypeError:在 Google App 脚本的对象中找不到函数 forEach
- r - 使用 nls() 函数生成 von Bertalanffy 增长模型时出现“奇异梯度”错误