简体   繁体   中英

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!

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

尝试将sapplymode一起使用:

sapply(your_df, mode)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM