简体   繁体   中英

ifelse works with mutate in R, why not in knitr?

I have this line of code working in "normal" R:

damage <- damage %>% mutate( EVTYPE = ifelse(grepl("winter", EVTYPE,
    ignore.case=T), "Winter Weather", EVTYPE))

However, the exact same line of code in knitr results in strange behavior. Instead of the value of EVTYPE being left alone if the grep fails, it is instead changed into a number. Perhaps an index.

The output in R of head(unique(damage$EVTYPE)) is:

[1] "TORNADO" "TSTM WIND" "HAIL" "ICE STORM/FLASH FLOOD"
[5] "Winter Weather" "HURRICANE OPAL/HIGH WINDS"

whereas in knitr it is:

[1] "407" "423" "134" "239"
[5] "Winter Weather" "223"

Is this a bug? Is there some reason this isn't expected to work in knitr?

It looks like in your R environment it's a character vector while in your knitr environment it's a factor. Have you forgotten to use stringsAsFactors = FALSE on read.table or data.frame?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM