【发布时间】:2020-08-19 05:40:39
【问题描述】:
我在管道中包含条件时遇到问题。在我的代码中,我将一个函数应用于数据,并根据是否有任何积极的结果来总结数据。如果没有阳性结果,我需要输出为“NA”。
library(tidyverse)
df<-tibble(
lab1=c(rep("cat", 5), rep("carrot", 5), rep("granite", 5)),
lab2=c(rep("animal", 5), rep("vegetable", 5), rep("mineral", 5)),
res=c(9.90, 10.90, 11.20, 8.70, 10.10, 9.66, 13.00, 8.88, 9.33, 8.77, 7, 7, 7, 7, 7)
)
TestSameVal<-function(d){
if (length(unique(d$res))==1){
return(TRUE)} else return(FALSE)
}
result<-
df%>%
group_by(lab1, lab2)%>%
nest()%>%
mutate(all_sameval=map(data, TestSameVal))%>% #Returns TRUE if all measurements for a given variable are the same value
unnest(all_sameval)%>%
filter(all_sameval==T)%>% #Filter on only those variables with the same value
unnest(data)%>%
select(-all_sameval)%>%
{if (length(.$res)>0) summarise(rep_val=mean(res)) else T=NA}
#If there are any results where all_sameval is TRUE, summarise the results. Otherwise, assign NA
代码通过 select(-all_sameval) 运行良好。我收到以下由summarise 内容引起的错误:
Error in mean(res) : object 'res' not found
另外,当我运行以下代码时,它工作得很好:
df%>%
group_by(lab1, lab2)%>%
nest()%>%
mutate(all_sameval=map(data, TestSameVal))%>%
unnest(all_sameval)%>%
filter(all_sameval==T)%>%
unnest(data)%>%
select(-all_sameval)%>%
summarise(rep_val=mean(res))
我意识到我可以使用管道外部的条件来处理这个问题,但如果可能的话,我宁愿保持精简。真正让我崩溃的是,这段代码昨天运行良好。我已经尝试更新我的所有软件包并重新启动 R。
非常感谢任何帮助!
【问题讨论】:
-
使用
{if (length(.$res)>0) {. %>% summarise(rep_val=mean(res))} else NA}。 IF打断了管道,需要再传一遍 -
接近了!它不太奏效,但
{if (length(select(.,lab1)%>%pull())>0) summarise(., rep_val=mean(res)) else T=NA}确实奏效了。谢谢!