Search code examples
rdplyrdata-cleaning

Coerce Matrix Character Values to Numeric Values


I have a matrix of historical home values that I'm analyzing and after I create a matrix to by transposing the object, I cannot convert the matrix's values to numeric rather than characters. I also want to preserve the row and column indices. Ultimately, this object should be an .xts

colnames(master_zillow2) <- names1

master_zillow3 <- t(master_zillow2)
master_zillow3 <- master_zillow3 %>% row_to_names(row_number = 1)

A sample of the data:

structure(c(" 112,055.39 ", " 112,074.39 ", " 111,940.63 ", " 111,935.54 ", 
" 111,989.44 ", " 221,254.91 ", " 221,443.20 ", " 221,951.01 ", 
" 222,971.84 ", " 222,991.41 ", " 92,921.09 ", " 92,903.53 ", 
" 92,922.18 ", " 92,896.29 ", " 92,921.62 ", " 272,971.91 ", 
" 275,503.86 ", " 277,680.63 ", " 281,073.80 ", " 283,666.72 ", 
" 247,208.49 ", " 248,297.55 ", " 249,632.64 ", " 251,537.16 ", 
" 252,856.62 "), dim = c(5L, 5L), dimnames = list(c("1-31-2000", 
"2-29-2000", "3-31-2000", "4-30-2000", "5-31-2000"), c("77449", 
"77494", "79936", "11368", "11385")))

storage.mode(master_zillow3) <- "numeric"

The final line of code - "storage.mode" <- "numeric" replaces the entire object's character values with NA's. Help resolving is appreciated!


Solution

  • You can replace the comma and convert to numeric:

    df[] <- gsub("\\,","", df) # thanks @thelatemail
    storage.mode(df) <- "numeric"
    
    #              77449    77494    79936    11368    11385
    # 1-31-2000 112055.4 221254.9 92921.09 272971.9 247208.5
    # 2-29-2000 112074.4 221443.2 92903.53 275503.9 248297.5
    # 3-31-2000 111940.6 221951.0 92922.18 277680.6 249632.6
    # 4-30-2000 111935.5 222971.8 92896.29 281073.8 251537.2
    # 5-31-2000 111989.4 222991.4 92921.62 283666.7 252856.6