我有一个奇怪的情况,当我在控制台中运行时,一大堆代码对我有用,但是当我尝试在R Markdown文件中编织它时,我收到错误。这是代码:
{r,echo=FALSE, message=FALSE, fig.width=3, fig.height=3}
library(cdcfluview)
library(mosaic)
library(ggplot2)
library(dplyr)
neflu <- get_flu_data("census", "1", "ilinet", years=1997:2016)
neflu[neflu=="X"]<-NA
wk_new <- rep(NA, nrow(neflu))
season_new <- rep('0', nrow(neflu))
for(i in 1:nrow(neflu)){
if(neflu$WEEK[i] == 40){
wk_num <- 1
}
wk_new[i] <- wk_num
wk_num <- wk_num+1
if(neflu$WEEK[i] < 40) {
season_new[i] <- paste(neflu$YEAR[i]-1, '-', neflu$YEAR[i])
} else {
season_new[i] <- paste(neflu$YEAR[i], '-', neflu$YEAR[i]+1)
}
}
neflu_new <- cbind(neflu, wk_new, season_new)
neflu_agg <- neflu_new %>%
mutate(weighted_ili = as.numeric(X..WEIGHTED.ILI))%>%
group_by(REGION.TYPE, season_new) %>%
summarize(peak_ili = max(weighted_ili, na.rm=TRUE),
cum_ili = sum(weighted_ili,na.rm=TRUE),
peak_wk = wk_new[which.max(weighted_ili)])
neflu_agg2 <- subset(neflu_agg, peak_wk != 5, select=c(REGION.TYPE,season_new,peak_ili,cum_ili, peak_wk))
plot(peak_wk, peak_ili, main= "Peak ILI vs Peak Week", xlab= "Peak Week", ylab= "Peak ILI")
nelm1 <- lm(peak_ili~1+peak_wk)
abline(nelm1, col='red')
plot(peak_ili, cum_ili, main= "Cumulative ILI vs Peak ILI", xlab= "Peak ILI", ylab= "Cumulative ILI")
nelm2 <- lm(cum_ili~1+peak_ili)
abline(nelm2, col='blue')
以下是我收到的错误消息:
The object peak_wk not found Calls:<Anonymous> . . .
WithCallingHandlers -> withVisible -> eval -> eval -> plot execution halted".
我已尝试更改此代码,但仍然出现错误:
{r,echo=FALSE, message=FALSE, fig.width=3, fig.height=3}
library(cdcfluview)
library(mosaic)
library(ggplot2)
library(dplyr)
neflu <- get_flu_data("census", "1", "ilinet", years=1997:2016)
neflu[neflu=="X"]<-NA
wk_new <- rep(NA, nrow(neflu))
season_new <- rep('0', nrow(neflu))
for(i in 1:nrow(neflu)){
if(neflu$WEEK[i] == 40){
wk_num <- 1
}
wk_new[i] <- wk_num
wk_num <- wk_num+1
if(neflu$WEEK[i] < 40) {
season_new[i] <- paste(neflu$YEAR[i]-1, '-', neflu$YEAR[i])
} else {
season_new[i] <- paste(neflu$YEAR[i], '-', neflu$YEAR[i]+1)
}
}
neflu_new <- cbind(neflu, wk_new, season_new)
weighted_ili <- mutate(neflu_new, weighted_ili = as.numeric(X..WEIGHTED.ILI))
peak_ili <- max(weighted_ili, na.rm=TRUE)
cum_ili <- sum(weighted_ili,na.rm=TRUE)
peak_wk <- wk_new[which.max(weighted_ili)]
neflu_agg <- neflu_new %>%
mutate(weighted_ili = as.numeric(X..WEIGHTED.ILI))%>%
group_by(REGION.TYPE, season_new) %>%
summarize(peak_ili = max(weighted_ili, na.rm=TRUE),
cum_ili = sum(weighted_ili,na.rm=TRUE),
peak_wk = wk_new[which.max(weighted_ili)])
neflu_agg2 <- subset(neflu_agg, peak_wk != 5, select=c(REGION.TYPE,season_new,peak_ili,cum_ili, peak_wk))
plot(peak_wk, peak_ili, main= "Peak ILI vs Peak Week", xlab= "Peak Week", ylab= "Peak ILI")
nelm1 <- lm(peak_ili~1+peak_wk)
abline(nelm1, col='red')
plot(peak_ili, cum_ili, main= "Cumulative ILI vs Peak ILI", xlab= "Peak ILI", ylab= "Cumulative ILI")
nelm2 <- lm(cum_ili~1+peak_ili)
abline(nelm2, col='blue')
以下是我现在得到的错误:
Error in FUN(X[[i]],...): only defined on data frame with all
numeric variables Calls:
任何帮助我们编写这些代码都会非常感激。我确信我只是遗漏了一些非常明显的东西,但我无法弄清楚。
答案 0 :(得分:1)
我现在意识到为什么我在lmo的帮助下得到了错误:
peak_wk,peak_ili和cum_ili未在全球环境中定义。我必须指定plot()函数从中提取数据的位置:
attach(neflu_agg2)
这很好用。