Hi All, I have problem about the factor features. I read in a .cvs file, a column which is supposed to be numeric, but it becomes factor level data after reading in. mydata[,"newXaxis"] [1] 0 0.430010601 11.23198508 25.00940297 37.75338045 45.98289639 [7] 48.92328133 52.51142822 61.12359751 63.85832121 70.98927949 77.16550181 [13] 97.73545713 113.6588007 118.6588007 129.9635717 151.8944844 178.7997437 [19] 206.45806 248.5916724 309.2463405 373.436891 441.0136089 508.6003268 [25] 586.8717683 670.5198381 760.8728108 854.0728565 948.6242311 1048.159042 [31] 1150.177895 1257.016622 1262.016622 1273.775731 1295.941555 1331.142905 [37] 1368.605796 1419.096727 1478.299769 1557.275945 1636.262122 1641.262122 [43] 1642.586431 44 Levels: 0 0.430010601 10030.11259 10096.32795 10167.4591 ... newXaxis I tried to convert this column to numeric using command: mydata[,"newXaxis"]<-as.numeric(mydata[,"newXaxis"]) However, it turned out a batch of integer numbers.> as.numeric(mydata[,"newXaxis"])[1] 1 2 31 202 230 245 250 256 271 276 292 307 337 33 46 82 124 186 [19] 191 201 213 228 240 253 266 280 305 317 332 15 39 64 65 71 80 87 [37] 92 97 112 Does anyone know how to remove the factor and remain the actual numeric data? Thanks a lot! Phoebe [[alternative HTML version deleted]]
If you read it in properly it _would_ be numeric. So there is either a stray nonnumeric entry somewhere (comma, quote, ..?) -- check your input carefully -- or you have not read it in properly. The latter appears to be the case: what's that "newXaxis" level in the printout about? You need to fix this. Bert Gunter Nonclinical Biostatistics 467-7374 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of phoebe kong Sent: Friday, May 01, 2009 1:16 PM To: r-help Subject: [R] factor level Hi All, I have problem about the factor features. I read in a .cvs file, a column which is supposed to be numeric, but it becomes factor level data after reading in. mydata[,"newXaxis"] [1] 0 0.430010601 11.23198508 25.00940297 37.75338045 45.98289639 [7] 48.92328133 52.51142822 61.12359751 63.85832121 70.98927949 77.16550181 [13] 97.73545713 113.6588007 118.6588007 129.9635717 151.8944844 178.7997437 [19] 206.45806 248.5916724 309.2463405 373.436891 441.0136089 508.6003268 [25] 586.8717683 670.5198381 760.8728108 854.0728565 948.6242311 1048.159042 [31] 1150.177895 1257.016622 1262.016622 1273.775731 1295.941555 1331.142905 [37] 1368.605796 1419.096727 1478.299769 1557.275945 1636.262122 1641.262122 [43] 1642.586431 44 Levels: 0 0.430010601 10030.11259 10096.32795 10167.4591 ... newXaxis I tried to convert this column to numeric using command: mydata[,"newXaxis"]<-as.numeric(mydata[,"newXaxis"]) However, it turned out a batch of integer numbers.> as.numeric(mydata[,"newXaxis"])[1] 1 2 31 202 230 245 250 256 271 276 292 307 337 33 46 82 124 186 [19] 191 201 213 228 240 253 266 280 305 317 332 15 39 64 65 71 80 87 [37] 92 97 112 Does anyone know how to remove the factor and remain the actual numeric data? Thanks a lot! Phoebe [[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.
Tell me how this works out for you. I think R is seeing your "newXaxis" variable as a character variable because there is some non-numeric string in there, then it converts to factor. Try setting stringsAsFactors=FALSE in the read.csv and see if it is then read as character. If it is, try your command again: mydata[,"newXaxis"]<-as.numeric(mydata[,"newXaxis"]) I don't know if this will work. Just guessing. Mike On Fri, 1 May 2009, phoebe kong wrote:> Hi All, > > I have problem about the factor features. > > I read in a .cvs file, a column which is supposed to be numeric, but it > becomes factor level data after reading in. > > mydata[,"newXaxis"] > [1] 0 0.430010601 11.23198508 25.00940297 37.75338045 > 45.98289639 > [7] 48.92328133 52.51142822 61.12359751 63.85832121 70.98927949 > 77.16550181 > [13] 97.73545713 113.6588007 118.6588007 129.9635717 151.8944844 > 178.7997437 > [19] 206.45806 248.5916724 309.2463405 373.436891 441.0136089 > 508.6003268 > [25] 586.8717683 670.5198381 760.8728108 854.0728565 948.6242311 > 1048.159042 > [31] 1150.177895 1257.016622 1262.016622 1273.775731 1295.941555 > 1331.142905 > [37] 1368.605796 1419.096727 1478.299769 1557.275945 1636.262122 > 1641.262122 > [43] 1642.586431 > > 44 Levels: 0 0.430010601 10030.11259 10096.32795 10167.4591 ... newXaxis > > I tried to convert this column to numeric using command: > > mydata[,"newXaxis"]<-as.numeric(mydata[,"newXaxis"]) > > However, it turned out a batch of integer numbers. > >> as.numeric(mydata[,"newXaxis"]) > [1] 1 2 31 202 230 245 250 256 271 276 292 307 337 33 46 82 124 > 186 > [19] 191 201 213 228 240 253 266 280 305 317 332 15 39 64 65 71 80 > 87 > [37] 92 97 112 > > Does anyone know how to remove the factor and remain the actual numeric > data? > > Thanks a lot! > > Phoebe > > [[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. >Please note that I am using a new email address. My old email address @taxa.epi.umn.edu, will stop working because that old computer is being retired.
Dear phoebe, See http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f HTH, Jorge On Fri, May 1, 2009 at 4:15 PM, phoebe kong <sityeekong@gmail.com> wrote:> Hi All, > > I have problem about the factor features. > > I read in a .cvs file, a column which is supposed to be numeric, but it > becomes factor level data after reading in. > > mydata[,"newXaxis"] > [1] 0 0.430010601 11.23198508 25.00940297 37.75338045 > 45.98289639 > [7] 48.92328133 52.51142822 61.12359751 63.85832121 70.98927949 > 77.16550181 > [13] 97.73545713 113.6588007 118.6588007 129.9635717 151.8944844 > 178.7997437 > [19] 206.45806 248.5916724 309.2463405 373.436891 441.0136089 > 508.6003268 > [25] 586.8717683 670.5198381 760.8728108 854.0728565 948.6242311 > 1048.159042 > [31] 1150.177895 1257.016622 1262.016622 1273.775731 1295.941555 > 1331.142905 > [37] 1368.605796 1419.096727 1478.299769 1557.275945 1636.262122 > 1641.262122 > [43] 1642.586431 > > 44 Levels: 0 0.430010601 10030.11259 10096.32795 10167.4591 ... newXaxis > > I tried to convert this column to numeric using command: > > mydata[,"newXaxis"]<-as.numeric(mydata[,"newXaxis"]) > > However, it turned out a batch of integer numbers. > > > as.numeric(mydata[,"newXaxis"]) > [1] 1 2 31 202 230 245 250 256 271 276 292 307 337 33 46 82 124 > 186 > [19] 191 201 213 228 240 253 266 280 305 317 332 15 39 64 65 71 80 > 87 > [37] 92 97 112 > > Does anyone know how to remove the factor and remain the actual numeric > data? > > Thanks a lot! > > Phoebe > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]