简体   繁体   English

ifelse在R中使用mutate,为什么不在knitr中使用?

[英]ifelse works with mutate in R, why not in knitr?

I have this line of code working in "normal" R: 我有这行代码在“普通” 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. 但是,knitr中完全相同的代码行会导致奇怪的行为。 Instead of the value of EVTYPE being left alone if the grep fails, it is instead changed into a number. 如果grep失败,则不会单独保留EVTYPE的值,而是改为将其更改为数字。 Perhaps an index. 也许是一个索引。

The output in R of head(unique(damage$EVTYPE)) is: head(unique(damage $ EVTYPE))在R中的输出为:

[1] "TORNADO" "TSTM WIND" "HAIL" "ICE STORM/FLASH FLOOD" [1]“ TORNADO”“ TSTM WIND”“ HAIL”“ ICE STORM / FLASH FLOOD”
[5] "Winter Weather" "HURRICANE OPAL/HIGH WINDS" [5]“冬季天气”“飓风蛋白石/高风”

whereas in knitr it is: 而在针织衫中是:

[1] "407" "423" "134" "239" [1]“ 407”“ 423”“ 134”“ 239”
[5] "Winter Weather" "223" [5]“冬季天气”“ 223”

Is this a bug? 这是错误吗? Is there some reason this isn't expected to work in knitr? 是否有某些原因不能预期此功能在knitr中起作用?

It looks like in your R environment it's a character vector while in your knitr environment it's a factor. 看起来在您的R环境中,它是一个字符向量,而在针织机环境中,这是一个因素。 Have you forgotten to use stringsAsFactors = FALSE on read.table or data.frame? 您是否忘记在read.table或data.frame上使用stringsAsFactors = FALSE?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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