Levels in R Dataframe -
i imported data .csv file, , attached dataset.
problem: 1 variable in integer form , has 295 levels. need use variable create others, don't know how deal levels.
what these, , how deal them?
when read in data read.table (or read.csv? - didn't specify), add argument stringsasfactors = false. character data instead.
if expecting integers column must have data not interpretable integers, convert numeric after you've read it.
txt <- c("x,y,z", "1,2,3", "a,b,c") d <- read.csv(textconnection(txt)) sapply(d, class) x y z ##"factor" "factor" "factor" ## don't want factors, characters d <- read.csv(textconnection(txt), stringsasfactors = false) sapply(d, class) # x y z #"character" "character" "character" ## convert x numeric, , wear nas non numeric data as.numeric(d$x) #[1] 1 na #warning message: #nas introduced coercion
finally, if want ignore these input details , extract integer levels factor use e.g. as.numeric(levels(d$x))[d$x], per "warning" in ?factor.
Comments
Post a Comment