[英]R using special characters
I have a column of strings (and NAs) in a dataset whose values look like: 我在数据集中的一列字符串(和NA)看起来像这样:
"85min" "1hr 19min" "98min" NA "119min" "105min" NA "1hr 30min"
I'm trying to change all of the values to numerics (in minutes), so that the values look like: 我正在尝试将所有值更改为数字(以分钟为单位),以使这些值看起来像:
85 79 98 NA 119 105 NA 90
I tried to do it by pieces, first removing the "min," then removing the "hr," then changing "1" to "60," and then changing the middle spaces to plus signs, giving me: 我尝试分步进行,首先删除“ min”,然后删除“ hr”,然后将“ 1”更改为“ 60”,然后将中间空格更改为加号,从而得到:
"85" "60+19" "98" NA "119" "105" NA "60+30"
I was hoping that by applying as.numeric, I would get the ones with "+" signs to turn into formulas, but instead they just resulted in NAs. 我希望通过使用as.numeric,我可以将带有“ +”号的数字转换为公式,但相反,它们只会导致NA。
Is there any shorthand way of doing this kind of conversion? 有什么捷径可以进行这种转换吗? Many thanks in advance! 提前谢谢了!
Yes, you need parse
and eval
是的,您需要parse
和eval
strings = c("85", "60+19", "98", NA, "119", "105", NA, "60+30")
sapply(parse(text=strings), eval)
[1] 85 79 98 NA 119 105 NA 90
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.