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,
dot character makes it still be considered as number. Also in
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
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.