I have data in a character column that includes " characters. For example:
258
"6,962.30"
96
I am trying to clean this column with to make it numeric.
The command:
df$Column <-sub(' .*"','',df$Column)
Is not working. How do I fix this?
CodePudding user response:
You can use readr::parse_number
.
df$Column <- readr::parse_number(df$Column)
For example,
x <- c(258, '"6,962.30"', 96)
readr::parse_number(x)
#[1] 258.0 6962.3 96.0
CodePudding user response:
I would use sub
with a character class:
x <- c(258, '"6,962.30"', 96)
output <- as.numeric(gsub("[^0-9.] ", "", x))
output
[1] 258.0 6962.3 96.0
CodePudding user response:
Another base R approach:
as.numeric(gsub('\\"|,','',v))
[1] 258.0 6962.3 96.0
CodePudding user response:
You'd just need to substitute comma.
as.numeric(gsub(",", "", df$Column))
# [1] 258.0 6962.3 96.0