r - Simple issue with as.numeric -


i have matrix:

catergory  reason          species [1,] "decline"  "genuine"       "24"    [2,] "improved" "genuine"       "16"     [3,] "improved" "misclassified" "24"    [4,] "decline"  "misclassified" "16"    [5,] "decline"  "taxonomic"     "24"    [6,] "improved" "taxonomic"     "16"    [7,] "decline"  "unclear"       "24"    [8,] "improved" "unclear"       "16"    

i want make third column numeric try:

reasonstats[,3]<-as.numeric(reasonstats[,3]) 

it doesn't give me errors, values remain not numerical. if do

as.numeric(reasonstats[,3]) 

it gives me list of them numeric , if

reasonstats[,3]<-2 

it subs in 2 fine.

can tell me i'm going wrong?

thank you

to understand "what's going wrong", need start reading page ?matrix, lead page ?as.vector. reading ?as.vector, find:

as.vector, generic, attempts coerce argument vector of mode mode (the default coerce whichever vector mode convenient)

a few examples understand this:

a <- c(true, true, false) # [1]  true  true false mode(a) # [1] "logical" b <- c(true, true, 0) b # [1] 1 1 0 as.logical(b) # [1]  true  true false mode(b) # [1] "numeric" c <- c(true, false, "zero") c # [1] "true"  "false" "zero"  mode(c) # [1] "character" 

so, understanding as.vector cannot contain mixed atomic modes, can conclude matrix (which vector dim attribute specifying number of rows , columns) cannot contain mixed atomic modes.

reading more storage structures in r, encounter data.frame. page ?data.frame describes as:

... matrix-like structure columns may of differing types (numeric, logical, factor , character , on).

so, if want rectangular table have shown, columns of different types, should use data.frame, not matrix.

assuming matrix "m", defined as:

m <- structure(   c("decline", "improved", "improved", "decline", "decline",      "improved", "decline", "improved", "genuine", "genuine",      "misclassified", "misclassified", "taxonomic", "taxonomic",     "unclear", "unclear", "24", "16", "24", "16", "24", "16",      "24", "16"),    .dim = c(8l, 3l),    .dimnames = list(null, c("catergory", "reason", "species"))) 

first, convert data.frame:

m2 <- data.frame(m, stringsasfactors=false) 

then convert "species" column numeric:

m2$species <- as.numeric(m2$species) 

here's result, , structure.

m2 #   catergory        reason species # 1   decline       genuine      24 # 2  improved       genuine      16 # 3  improved misclassified      24 # 4   decline misclassified      16 # 5   decline     taxonomic      24 # 6  improved     taxonomic      16 # 7   decline       unclear      24 # 8  improved       unclear      16 str(m2) # 'data.frame':  8 obs. of  3 variables: #  $ catergory: chr  "decline" "improved" "improved" "decline" ... #  $ reason   : chr  "genuine" "genuine" "misclassified" "misclassified" ... #  $ species  : num  24 16 24 16 24 16 24 16 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -