当R中只有数字时,删除空格并将值转换为数字

我一直在网上寻找这个问题

与通常的发现不同的是,我有一些列,其中有数字和其他不同于纯数字的值。

例如说:

df <- data.frame('Col1' = c('421', ' 0.52', '-0.88 ', '1.2 (ref)', ' 97  '),
                 'Col2' = c('0.0', '0.27,0.91', '3.0', ' 10242.3', '  94.5'))

I would like to remove spaces from the cells only composed by numbers. Not sure if, for example, 0.52, that dot character makes it still be considered as number. Also in -0.88 the - character.

到目前为止,我会使用

library(stringr)
# Remove spaces
df$Col1 <- str_replace_all(df$Col1, "\\s+", "")

library(dplyr)
# Convert to as.numeric
df %>%
mutate_all(funs(as.numeric(as.character(.)))   

But I would not like to just replace every single space, for example in the value 1.2 (ref), I would like to keep that space. Also, not to change every value to as.numeric, only where pure numbers, or \d+\.\d+, or \-\d+\.\d+ (regex)

Also if I attempt to convert to as.numeric, the numeric values somehow change drastically, I understand this is because of the spaces present in the values.

提前致谢

评论
nsunt
nsunt

We could use parse_number

library(dplyr)
df %>% 
    mutate_all(~readr::parse_number(as.character(.)))
#    Col1     Col2
#1 421.00     0.00
#2   0.52     0.27
#3  -0.88     3.00
#4   1.20 10242.30
#5  97.00    94.50
点赞
评论