我有这样的数据:
sample_data <- data.frame(
txtnumbers = c("text stuff +300.5","other stuff 40+ more stuff","text here -30 here too","30- text here","50+","stuff here 500+","400.5-" ),
stringsAsFactors = F
)
我想提取后面跟着+符号的数字,并将值插入到一个新列中,忽略文本的其余部分,在没有数字后面跟着+的地方返回NA:
desired_data <- data.frame(
txtnumbers = c("text stuff +300.5","other stuff 40+ more stuff","text here -30 here too","30- text here","50+","stuff here 500+","400.5-" ),
desired_col = c(NA,40,NA,NA,50,500,NA),
stringsAsFactors = F
)
有人能帮我做一个有效的功能吗?我可以使用parse_numeric解析数字,但只返回后面跟着+的数字会给我带来问题。谢谢