Lyam
Lyam

Reputation: 135

Can not convert character to numeric in R

I'm struggling to convert a character vector to numeric in R. I import a dataframe from csv with:

a = read.csv('myData.csv', header=T, stringsAsFactors=F)

One of my factors, fac1, is a vector of numbers but contains some instances of "na" and "nr". Hence, typeof(a$fac1) returns "character"

I create a new dataframe without "na" and "nr" entries

k = a[a$fac1 != "na" & a$fac1 != "nr", ]

I then try to convert fac1 to numeric with:

k$fac1_num = as.numeric(k$fac1)

The problem is that this doesn't work, as typeof(k$fac1_num) now returns "double" instead of "numeric"

Can anyone suggest a fix / point out what I'm doing wrong? Thanks in advance!

Upvotes: 0

Views: 2839

Answers (2)

null
null

Reputation: 367

Try to use sapply with mode :

sapply(your_df, mode)

Upvotes: 0

IRTFM
IRTFM

Reputation: 263471

Try just coercing to numeric:

a = read.csv('myData.csv', header=T, stringsAsFactors=F)
a$fac1_num = as.numeric(a$fac1)

If you need to subset (which is generally not needed and I would advise against doing routinely since there might be value in knowing what the other column value tell you about the "reality" behind the data), then just:

k <- a[ !is.na(a$fac1_num) , ]

That way you will still have the original character value in the a data-object and can examine its values if needed. The proper test for "numericy" is is.numeric

Upvotes: 1

Related Questions