I'm building a table from a CSV file. When the file is initially loaded I need to load as characters.
datset <- read.csv("outcome-of-care-measures.csv", colClasses = "character")
I have function to convert a factor containing number (from other stack q)
as.numeric.factor <- function(x) {as.numeric(levels(x))[x]}
I clean up the file with
i<-17
datset[datset=="Not Available"]<-NA
datset<-datset[complete.cases(datset[,i]),]
x<- as.numeric.factor(datset[, i])
The datset table contains lots of columns I don't need so I build a new table :
dat <- data.frame(cbind("HospitalName"= datset[,2], "State"= datset[,7],"Rating" = x))
My problem is that even though x is numeric, it gets turned into a factor when loaded to the dataframe. I can verify this from debug mode with :
class(x)
"Numeric"
class(dat[,3])
"Factor"
In later code I'm trying to sort the Rating column but it's failing due it being a factor - I guess.
I've even tried appending stringsAsFactors = FALSE to read.csv but this has no effect.
How can I prevent x from being converted into a factor when loading to a DF?
read.csvinstead? There is anna.stringsargument and you could import the column as numeric.x <- factor(1:10): after transformation withas.numeric.factorand putting it in a data frame, it remains anumeric. @Henrik's comment is, I think, right on where there is a problem: what happens when you removecbind?data.frame(cbindstep is problematic. Thecbindstep results in a matrix. A matrix can only hold type of values (see coercion hierarchy in the Value section of?matrix). When you then applydata.frameon a character matrix, values are converted tofactor(seestringsAsFactorsargument in?data.frame). See alsooptions;stringsAsFactors.?data.frame, which allow for constructs likedata.frame(x = ...,y = ...), which is how you should be using the function.data.frame(cbind())is a very bad habit.read.csvcan do this automatically), but you could:colclasses <- rep("character, 40); colclasses[7] <- "numeric"