How can I convert a factor variable with missing values to a numeric variable?
How can I solve my problem?
The crucial information here is how missing values are encoded in your data file. The corresponding argument in read.csv()
is called na.strings
. So if dots are used:
original <- read.csv("original.csv", na.strings = ".")
I'm not 100% sure what your problem is but maybe this will help....
original<-read.csv("original.csv",header = TRUE,stringsAsFactors = FALSE)
original$Tumor_Size<-as.numeric(original$Tumor_Size)
This will introduce NA's because it cannot convert your dot(.) to a numeric value. If you try to replace the NA's with a dot again it will return the field as a character, to do this you can use,
original$Tumor_Size[is.na(original$Tumor_Size)]<-"."
Hope this helps.
链接地址: http://www.djcxy.com/p/24952.html上一篇: 将11个级别的因子转换为字符或数字