0
我有一個奇怪的情況,當我在控制檯中運行它時,一大塊代碼正在爲我工作,但是當我試圖編織它時R Markdown文件,我收到錯誤。下面是代碼:R中的錯誤:找不到對象,代碼不工作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:
任何幫助只是讓這個代碼編織將不勝感激。我相信我只是錯過了一些非常明顯的東西,但我無法弄清楚。
在您的第一個代碼塊中,您可能需要引用peak_wk作爲neflu_agg $ peak_wk。我的理解是'dplyr'返回一個data.frame。您引用peak_wk的代碼似乎正在提供一個名爲neflu_agg的數據框。 – lmo
你的意思是我應該在代碼的總結部分這樣做?所以neflu_agg $ peak_wk = wk_new [which.max(weighted_ili)])? 如果是這樣,我收到一條新的錯誤消息,說cum_ili = sum(weighted_ili,na.rm = TRUE)中有一個意外的'=', – Nathan
不,我指的是非'dplyr'代碼,如'plot'功能。注意錯誤信息:'plot execution halted'。 – lmo