Napier
Napier

Reputation: 227

change column value and change its column type

I have this kind of dataset called df

this is part of the dataset that i want to change

SaleCondition    SalePrice
<chr>            <chr>
Normal           $208500
Normal           $181500
Normal           $223500
Abnorml          $140000
Normal           $250000
Normal           $143000

I know how to change column SalePrice to numeric type with this code

df$SalePrice = as.numeric(df$SalePrice)

But I dont know how delete all of the $ character.

Upvotes: 0

Views: 49

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61214

We can use sub for 'find and replace' and then convert to numeric

df$SalePrice <- as.numeric(sub("\\$", "", df$SalePrice))

Another alternative is parse_number from readr package

library(readr)
df$SalePrice <- parse_number(df$SalePrice)

Upvotes: 1

Related Questions