Dear R user, I am trying to convert the contents of a date.frame to a matrix. Since there are negative values in the date.frame, when I use data.matrix(x, rownames.force = NA), the resulting matrix is not the same as the original one. Basically I think R treats the numbers in the date.frame as character and converts it to corresponding numerics. Any idea on this issue? Many Thanks, Hongyuan x[1:3,] ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 ALL.11 2 -214 -139 -76 -135 -106 -138 -72 -413 5 -88 -165 -67 3 -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 -155 -93 4 -58 -1 -307 265 -76 215 238 7 106 42 -71 84 ALL.12 ALL.13 ALL.14 ALL.15 ALL.16 ALL.17 ALL.18 ALL.19 ALL.20 ALL.21 ALL.22 2 -92 -113 -107 -117 -476 -81 -44 17 -144 -247 -74 3 -119 -147 -72 -219 -213 -150 -51 -229 -199 -90 -321 4 -31 -118 -126 -50 -18 -119 100 79 -157 -168 -11 ALL.23 ALL.24 ALL.25 ALL.26 AML AML.1 AML.2 AML.3 AML.4 AML.5 AML.6 AML.7 2 -120 -81 -112 -273 -20 7 -213 -25 -72 -4 15 -318 3 -263 -150 -233 -327 -207 -100 -252 -20 -139 -116 -114 -192 4 -114 -85 -78 -76 -50 -57 136 124 -1 -125 2 -95 AML.8 AML.9 AML.10 ALL.27 ALL.28 ALL.29 ALL.30 ALL.31 ALL.32 ALL.33 ALL.34 2 -32 -124 -135 -342 -87 22 -243 -130 -256 -62 86 3 -49 -79 -186 -200 -248 -153 -218 -177 -249 -23 -36 4 49 -37 -70 41 262 17 -163 -28 -410 -7 -141 ALL.35 ALL.36 ALL.37 ALL.38 ALL.39 ALL.40 ALL.41 ALL.42 ALL.43 ALL.44 ALL.45 2 -146 -187 -56 -55 -59 -131 -154 -79 -76 -34 -95 3 -74 -187 -43 -44 -114 -126 -136 -118 -98 -144 -118 4 170 312 43 12 23 -50 49 -30 -153 -17 59 ALL.46 AML.11 AML.12 AML.13 AML.14 AML.15 AML.16 AML.17 AML.18 AML.19 AML.20 2 -12 -21 -202 -112 -118 -90 -137 -157 -172 -47 -62 3 -172 -13 -274 -185 -142 -87 -51 -370 -122 -442 -198 4 12 8 59 24 212 102 -82 -77 38 -21 -5 AML.21 AML.22 AML.23 AML.24 2 -58 -161 -48 -176 3 -217 -215 -531 -284 4 63 -46 -124 -81> > help(as.matrix) > y = data.matrix(x, rownames.force = NA) > y[1:3,]ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 ALL.11 2 195 75 620 76 13 58 466 399 1967 472 97 377 3 106 533 467 37 39 433 89 278 68 12 83 432 4 499 1 315 1458 407 891 1096 1819 718 1320 476 1436 ALL.12 ALL.13 ALL.14 ALL.15 ALL.16 ALL.17 ALL.18 ALL.19 ALL.20 ALL.21 ALL.22 2 446 25 11 30 489 428 308 1156 52 178 381 3 30 87 438 159 244 61 346 188 103 482 196 4 219 33 37 383 186 28 474 2449 64 101 23 ALL.23 ALL.24 ALL.25 ALL.26 AML AML.1 AML.2 AML.3 AML.4 AML.5 AML.6 AML.7 2 35 485 23 278 173 1178 186 254 545 362 602 318 3 203 81 167 330 181 1 228 190 75 39 20 181 4 26 496 467 596 443 276 869 867 1 55 775 676 AML.8 AML.9 AML.10 ALL.27 ALL.28 ALL.29 ALL.30 ALL.31 ALL.32 ALL.33 ALL.34 2 273 67 102 169 544 1043 87 63 272 294 1226 3 386 615 190 94 196 86 74 130 267 109 170 4 1569 360 611 1004 1162 897 43 241 404 308 48 ALL.35 ALL.36 ALL.37 ALL.38 ALL.39 ALL.40 ALL.41 ALL.42 ALL.43 ALL.44 ALL.45 2 55 130 253 281 376 38 89 466 361 133 329 3 372 130 213 238 24 33 66 32 397 36 25 4 725 1231 1041 488 1028 316 1748 222 60 50 1020 ALL.46 AML.11 AML.12 AML.13 AML.14 AML.15 AML.16 AML.17 AML.18 AML.19 AML.20 2 22 131 170 21 47 381 36 67 77 279 403 3 52 42 245 100 97 374 236 166 28 265 130 4 397 1714 1932 915 1289 409 316 293 1054 136 351 AML.21 AML.22 AML.23 AML.24 2 218 132 324 76 3 74 209 344 140 4 1015 420 65 342 [[alternative HTML version deleted]]
David Winsemius
2009-May-31 14:39 UTC
[R] convert the contents of a date.frame to a matrix
Unable to reproduce: > ?data.matrix > x <- read.table(textConnection(" ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 ALL.11 + 2 -214 -139 -76 -135 -106 -138 -72 -413 5 -88 -165 -67 + 3 -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 -155 -93 + 4 -58 -1 -307 265 -76 215 238 7 106 42 -71 84"), header=TRUE) > x ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 ALL.11 2 -214 -139 -76 -135 -106 -138 -72 -413 5 -88 -165 -67 3 -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 -155 -93 4 -58 -1 -307 265 -76 215 238 7 106 42 -71 84 > data.matrix(x) ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 ALL.11 2 -214 -139 -76 -135 -106 -138 -72 -413 5 -88 -165 -67 3 -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 -155 -93 4 -58 -1 -307 265 -76 215 238 7 106 42 -71 84 You may have something else in that dataframe that is not apparent on a simple print display. Can you instead provide the results of dput(x)? -- David Winsemius On May 31, 2009, at 9:29 AM, Hongyuan Cao wrote:> Dear R user, > > I am trying to convert the contents of a date.frame to a matrix. > Since there > are negative values in the date.frame, when I use data.matrix(x, > rownames.force = NA), the resulting matrix is not the same as the > original > one. Basically I think R treats the numbers in the date.frame as > character > and converts it to corresponding numerics. > > Any idea on this issue? > > Many Thanks, > > Hongyuan > > x[1:3,] > ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 > ALL.11 > 2 -214 -139 -76 -135 -106 -138 -72 -413 5 -88 > -165 -67 > 3 -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 > -155 -93 > 4 -58 -1 -307 265 -76 215 238 7 106 42 > -71 84 > ALL.12 ALL.13 ALL.14 ALL.15 ALL.16 ALL.17 ALL.18 ALL.19 ALL.20 ALL.21 > ALL.22 > 2 -92 -113 -107 -117 -476 -81 -44 17 -144 > -247 > -74 > 3 -119 -147 -72 -219 -213 -150 -51 -229 -199 > -90 > -321 > 4 -31 -118 -126 -50 -18 -119 100 79 -157 > -168 > -11 > ALL.23 ALL.24 ALL.25 ALL.26 AML AML.1 AML.2 AML.3 AML.4 AML.5 AML. > 6 AML.7 > 2 -120 -81 -112 -273 -20 7 -213 -25 -72 -4 > 15 -318 > 3 -263 -150 -233 -327 -207 -100 -252 -20 -139 -116 > -114 -192 > 4 -114 -85 -78 -76 -50 -57 136 124 -1 -125 > 2 -95 > AML.8 AML.9 AML.10 ALL.27 ALL.28 ALL.29 ALL.30 ALL.31 ALL.32 ALL.33 > ALL.34 > 2 -32 -124 -135 -342 -87 22 -243 -130 -256 > -62 86 > 3 -49 -79 -186 -200 -248 -153 -218 -177 -249 > -23 -36 > 4 49 -37 -70 41 262 17 -163 -28 -410 > -7 -141 > ALL.35 ALL.36 ALL.37 ALL.38 ALL.39 ALL.40 ALL.41 ALL.42 ALL.43 ALL.44 > ALL.45 > 2 -146 -187 -56 -55 -59 -131 -154 -79 -76 > -34 > -95 > 3 -74 -187 -43 -44 -114 -126 -136 -118 -98 > -144 > -118 > 4 170 312 43 12 23 -50 49 -30 -153 > -17 > 59 > ALL.46 AML.11 AML.12 AML.13 AML.14 AML.15 AML.16 AML.17 AML.18 AML.19 > AML.20 > 2 -12 -21 -202 -112 -118 -90 -137 -157 -172 > -47 > -62 > 3 -172 -13 -274 -185 -142 -87 -51 -370 -122 > -442 > -198 > 4 12 8 59 24 212 102 -82 -77 38 > -21 > -5 > AML.21 AML.22 AML.23 AML.24 > 2 -58 -161 -48 -176 > 3 -217 -215 -531 -284 > 4 63 -46 -124 -81 >> >> help(as.matrix) >> y = data.matrix(x, rownames.force = NA) >> y[1:3,] > ALL ALL.1 ALL.2 ALL.3 ALL.4 ALL.5 ALL.6 ALL.7 ALL.8 ALL.9 ALL.10 > ALL.11 > 2 195 75 620 76 13 58 466 399 1967 472 > 97 377 > 3 106 533 467 37 39 433 89 278 68 12 > 83 432 > 4 499 1 315 1458 407 891 1096 1819 718 1320 476 > 1436 > ALL.12 ALL.13 ALL.14 ALL.15 ALL.16 ALL.17 ALL.18 ALL.19 ALL.20 ALL.21 > ALL.22 > 2 446 25 11 30 489 428 308 1156 52 > 178 > 381 > 3 30 87 438 159 244 61 346 188 103 > 482 > 196 > 4 219 33 37 383 186 28 474 2449 64 > 101 > 23 > ALL.23 ALL.24 ALL.25 ALL.26 AML AML.1 AML.2 AML.3 AML.4 AML.5 AML.6 > AML.7 > 2 35 485 23 278 173 1178 186 254 545 362 > 602 318 > 3 203 81 167 330 181 1 228 190 75 39 > 20 181 > 4 26 496 467 596 443 276 869 867 1 55 > 775 676 > AML.8 AML.9 AML.10 ALL.27 ALL.28 ALL.29 ALL.30 ALL.31 ALL.32 ALL.33 > ALL.34 > 2 273 67 102 169 544 1043 87 63 272 > 294 1226 > 3 386 615 190 94 196 86 74 130 267 > 109 170 > 4 1569 360 611 1004 1162 897 43 241 404 > 308 48 > ALL.35 ALL.36 ALL.37 ALL.38 ALL.39 ALL.40 ALL.41 ALL.42 ALL.43 ALL.44 > ALL.45 > 2 55 130 253 281 376 38 89 466 361 > 133 > 329 > 3 372 130 213 238 24 33 66 32 397 > 36 > 25 > 4 725 1231 1041 488 1028 316 1748 222 60 > 50 > 1020 > ALL.46 AML.11 AML.12 AML.13 AML.14 AML.15 AML.16 AML.17 AML.18 AML.19 > AML.20 > 2 22 131 170 21 47 381 36 67 77 > 279 > 403 > 3 52 42 245 100 97 374 236 166 28 > 265 > 130 > 4 397 1714 1932 915 1289 409 316 293 1054 > 136 > 351 > AML.21 AML.22 AML.23 AML.24 > 2 218 132 324 76 > 3 74 209 344 140 > 4 1015 420 65 342 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD Heritage Laboratories West Hartford, CT
David Winsemius
2009-May-31 16:03 UTC
[R] convert the contents of a date.frame to a matrix
On May 31, 2009, at 10:49 AM, Hongyuan Cao wrote:> x = read.table("hongyuan_5_30_forsafe.txt", sep = "\t")> x = read.table("/Users/davidwinsemius/Downloads/ hongyuan_5_30_forsafe.txt", sep = "\t") > str(x) 'data.frame': 7131 obs. of 74 variables: $ V1 : Factor w/ 7131 levels "","A28102_at",..: 1 934 120 122 118 126 124 130 128 134 ... $ V2 : Factor w/ 5528 levels "","2-Sep","6-Mar",..: 1 5528 NA NA NA NA NA NA NA NA ... $ V3 : Factor w/ 2375 levels "-1","-10","-100",..: 2374 2375 195 106 499 2262 287 483 1254 143 ... $ V4 : Factor w/ 2326 levels "-1","-10","-100",..: 2325 2326 75 533 1 1442 231 357 299 117 ... My guess (which became a conclusion after testing) was that you have encountered the stringsAsFactors pitfall. (It's probably in the R Inferno someplace bu my favorite quote is from Terry Terneau: .) Try reading the table in with stringsAsFactors=FALSE or as.is=TRUE. After doing that and applying the rest of your code, I get: > head(y) X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 AFFX-BioB-5_at -214 -139 -76 -135 -106 -138 -72 -413 5 -88 -165 -67 -92 -113 -107 -117 -476 AFFX-BioB-M_at -153 -73 -49 -114 -125 -85 -144 -260 -127 -105 -155 -93 -119 -147 -72 -219 -213 AFFX-BioB-3_at -58 -1 -307 265 -76 215 238 7 106 42 -71 84 -31 -118 -126 -50 -18 AFFX-BioC-5_at 88 283 309 12 168 71 55 -2 268 219 82 25 173 243 149 257 301 AFFX-BioC-3_at -295 -264 -376 -419 -230 -272 -399 -541 -210 -178 -163 -179 -233 -127 -205 -218 -403 AFFX-BioDn-5_at -558 -400 -650 -585 -284 -558 -551 -790 -535 -246 -430 -323 -227 -398 -284 -402 -394 ..... ...snipped the rest of the output. You replied to me only personally which is against R-help practice, so I am adding back the list to the address list. (You should also configure your maile-client so that it sends plain text to the r-help list.) -- David Winsemius, MD Heritage Laboratories West Hartford, CT