我的代码坏了,这似乎是由于NAs和总结。我在两个数据帧上做了一个连接,由于时间的不同,会出现NAs。
我的加入:
data <- dplyr::right_join(ny.t, c.p, by=c("Date", "State"))我的代码:
top.5 <- data %>% group_by(State) %>% summarize(Infected = max(Deaths) + max(Positive)) %>%
arrange(desc(Infected)) %>% top_n(5) 怎么修?
发布于 2020-04-17 23:26:55
我们可以创建一个条件,以便if all (值在deaths中为NA )返回0,否则返回max值。
library(dplyr)
data %>%
group_by(state) %>%
summarise(max_deaths = if(all(is.na(deaths))) 0 else max(deaths, na.rm = TRUE),
max_positive = if(all(is.na(positive))) 0 else max(positive, na.rm = TRUE),
max_negative = if(all(is.na(negative))) 0 else max(positive, na.rm = TRUE))或者使用summarise_at
data %>%
group_by(state) %>%
summarise_at(vars(deaths, positive, negative),
~ if(all(is.na(.))) 0 else max(., na.rm = TRUE))https://stackoverflow.com/questions/61282273
复制相似问题