简体   繁体   中英

Coerce vectors in entire dataframe into appropriate class in R

I have a large data frame with around 3000 columns. Each column is a factor - but some are conceptually numeric.

I wrote this quick for loop to transform the appropriate columns but it doesn't seem to be working the way I intended. Essentially, I check and see if coercing a vector into numeric results in the mean of that vector being NaN , and if not, proceed to coerce the vector into numeric, otherwise, coerce it into character.

Here is the code:

 for (i in 1:length(data)) {
     ifelse(!is.nan(mean(as.numeric(as.character(data[,i])), na.rm=TRUE)), 
     as.numeric(as.character(data[,i])), as.character(data[,i])
      )  
 }

The problem is that it doesn't change my data.

I assume that you have a data.frame of character columns:

DF <- lapply(iris, as.character)
sapply(DF, class)
#Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
# "character"  "character"  "character"  "character"  "character"

You can then use type.convert :

DF <- lapply(DF, type.convert)
sapply(DF, class)
#Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#   "numeric"    "numeric"    "numeric"    "numeric"     "factor" 

This would also convert to logical, integer or complex values as appropriate, but I assume you won't mind that. Basically, this is what read.table uses.

However, I wonder why you have a data.frame of character columns to begin with ...

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