Hi all, I have some data x, which are actualy consisted of numerical enties. But the class of this matrix is set to be "factor" by someone else. I used "class(x)", it turns out to be "factor". So I can not calculate them. How can I turn them into numerical data so that I can apply math operations on them? Thanks a lot for your help. Selina [[alternative HTML version deleted]]
Try: x <- factor(1:10) class(x) x + 1 class(x) <- "numeric" x+1 On Jun 12, 2008, at 8:24 AM, Qman Fin wrote:> Hi all, > > I have some data x, which are actualy consisted of numerical > enties. But the > class of this matrix is set to be "factor" by someone else. I used > "class(x)", it turns out to be "factor". So I can not calculate them. > > How can I turn them into numerical data so that I can apply math > operations > on them? Thanks a lot for your help. > > Selina > > [[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.
When you have a data X with a class factor, you can transform it to numeric as y<-as.numeric(X) to transform it to a factor again use y<-as.factor(X) -- View this message in context: http://www.nabble.com/How-to-change-the-class-of-data--tp17793351p17793713.html Sent from the R help mailing list archive at Nabble.com.
On Jun 12, 2008, at 2:24 AM, Qman Fin wrote:> Hi all, > > I have some data x, which are actualy consisted of numerical > enties. But the > class of this matrix is set to be "factor" by someone else. I used > "class(x)", it turns out to be "factor". So I can not calculate them. >The typical approach is to do: as.numeric(as.character(x))> How can I turn them into numerical data so that I can apply math > operations > on them? Thanks a lot for your help. > > SelinaHaris Skiadas Department of Mathematics and Computer Science Hanover College
If x is a vector (one dimensional) then as.numeric(levels(x)) works - I am not sure whether this is the best solution. If you have a matrix you can use apply, i.e x1 <- apply(x,2,function(a) as.numeric(levels(a))) --- On Thu, 12/6/08, Qman Fin <finosaur at gmail.com> wrote:> From: Qman Fin <finosaur at gmail.com> > Subject: [R] How to change the class of data? > To: r-help at r-project.org > Received: Thursday, 12 June, 2008, 4:24 PM > Hi all, > > I have some data x, which are actualy consisted of > numerical enties. But the > class of this matrix is set to be "factor" by > someone else. I used > "class(x)", it turns out to be > "factor". So I can not calculate them. > > How can I turn them into numerical data so that I can apply > math operations > on them? Thanks a lot for your help. > > Selina > > [[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.
Hi Selina, try ?as.numeric, small example a=c(1,2,3,4,5) b=as.factor(a) class(b) c=as.numeric(b) class(c) in the case of a matrix of factor,try apply(matrix,1, as.numeric) Cheers A. ----- Messaggio originale ----- Da: Qman Fin <finosaur@gmail.com> A: r-help@r-project.org Inviato: Giovedì 12 giugno 2008, 8:24:08 Oggetto: [R] How to change the class of data? Hi all, I have some data x, which are actualy consisted of numerical enties. But the class of this matrix is set to be "factor" by someone else. I used "class(x)", it turns out to be "factor". So I can not calculate them. How can I turn them into numerical data so that I can apply math operations on them? Thanks a lot for your help. Selina [[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. ___________________________________ pinione! http://www.ymailblogit.com/blog/ [[alternative HTML version deleted]]
Seeing how there have been three wrong answers so far, I should point out that: 1) This is an FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#How- do-I-convert-factors-to-numeric_003f 2) Most of the other methods suggested so far fail if the example x used is not of the form 1:n. The only reason they happen to work, is that in that case the levels coincide with their labels. > x<-factor(8:5) > as.numeric(levels(x)) [1] 5 6 7 8 > > as.numeric(x) [1] 4 3 2 1 > class(x) <- "numeric" > x+1 [1] 5 4 3 2 attr(,"levels") [1] "5" "6" "7" "8" Haris Skiadas Department of Mathematics and Computer Science Hanover College On Jun 12, 2008, at 3:07 AM, anna freni sterrantino wrote:> Hi Selina, > try ?as.numeric, > > small example > a=c(1,2,3,4,5) > b=as.factor(a) > class(b) > > c=as.numeric(b) > class(c) > > in the case of a matrix of factor,try > apply(matrix,1, as.numeric) > > Cheers > > A. > > > ----- Messaggio originale ----- > Da: Qman Fin <finosaur at gmail.com> > A: r-help at r-project.org > Inviato: Gioved? 12 giugno 2008, 8:24:08 > Oggetto: [R] How to change the class of data? > > Hi all, > > I have some data x, which are actualy consisted of numerical > enties. But the > class of this matrix is set to be "factor" by someone else. I used > "class(x)", it turns out to be "factor". So I can not calculate them. > > How can I turn them into numerical data so that I can apply math > operations > on them? Thanks a lot for your help. > > Selina >
Conversion to factor may happen (and often does) when you read in data with read.table(). So one solution may be reading in the same data again in a slightly different way: read.table(file="mydatafile", as.is=TRUE) # see also ?read.table You can also specify a class to each column of the data you're about to read in: read.table(****, colClasses=c("numeric", "factor", "character", "my.funny.class")) Ad take a look at http://cran.r-project.org/doc/FAQ/R-FAQ.html p. 7.10 for the right answer -- in any case, don't use as.numeric(x)! Kenn On Thu, Jun 12, 2008 at 9:24 AM, Qman Fin <finosaur@gmail.com> wrote:> Hi all, > > I have some data x, which are actualy consisted of numerical enties. But > the > class of this matrix is set to be "factor" by someone else. I used > "class(x)", it turns out to be "factor". So I can not calculate them. > > How can I turn them into numerical data so that I can apply math operations > on them? Thanks a lot for your help. > > Selina > > [[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]]