Replace string with NA in R dataframe column

2767 views r
1

I am having trouble finding the syntax for replacing a string value in a data frame column with NA in r language. The whole column is made up of 1s and 0s, but because of these 2 string outlier values, the whole column is registering as character rather than numeric. ??I want to convert these outlier strings to NAs, and then the whole column to numeric.

Here is the output from the frequency table column

I have downloaded the recode() tool from the package car, but I can’t make it work with the character value.

Let me know if anyone can help!

answered question

1 Answer

1

Given a vector x

x <- c(1, 0, "45 mins", "10 mins")
#[1] "1"       "0"       "45 mins" "10 mins"

you can do

as.numeric(x)
#[1]  1  0 NA NA
# Warning message:
# NAs introduced by coercion 

So you should do

dataframe$column <- as.numeric(dataframe$column)

posted this

Have an answer?

JD

Please login first before posting an answer.