Dear Users, I have a variable in my dataset which is of type factor. But it actually contains numeric entries which like 5.735 4.759 ..... This is because the data was read from a CSV file into R and this variable contained other charaters which were not numeric. I have now dropped the records with the characters which are not numeric for this variable and want to change it to numeric srotage type. I have tried using as.numeric() function but it changes the values in the variable to what I think are the ranks of the individual values of the varible in the dataset. For example if 5.735 is the current content in the field, then the new object created by as.numeric will contain a value like 680 if the 5.735 was the highest value for the varible and the dataset had 680 records. How can I change the storage type without changing the contents of this variable in this case? Thanks for your consideration. -- Ojal John Owino P.O Box 230-80108 Kilifi, Kenya. Mobile:+254 728 095 710 [[alternative HTML version deleted]]
From ?factor: The interpretation of a factor depends on both the codes and the "levels" attribute. Be careful only to compare factors with the same set of levels (in the same order). In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)). Uwe Ligges ojal john owino wrote:> Dear Users, > I have a variable in my dataset which is of type factor. But it actually > contains numeric entries which like 5.735 4.759 ..... This is because the > data was read from a CSV file into R and this variable contained other > charaters which were not numeric. I have now dropped the records with the > characters which are not numeric for this variable and want to change it to > numeric srotage type. > > I have tried using as.numeric() function but it changes the values in the > variable to what I think are the ranks of the individual values of the > varible in the dataset. For example if 5.735 is the current content in the > field, then the new object created by as.numeric will contain a value like > 680 if the 5.735 was the highest value for the varible and the dataset had > 680 records. > > > How can I change the storage type without changing the contents of this > variable in this case? > > Thanks for your consideration. > > >
Try: as.numeric(as.character(x)) I usually define the following for this purpose: factor.to.number=function(x){ as.numeric(as.character(x)) } On Tue, Mar 10, 2009 at 2:25 AM, ojal john owino <ojal.johnowino at googlemail.com> wrote:> Dear Users, > I have a variable in my dataset which is of type factor. But it actually > contains numeric entries which like 5.735 ?4.759 ..... This is because the > data was read from a CSV file into R and this variable contained other > charaters which were not numeric. I have now dropped the records with the > characters which are not numeric for this variable and want to change it to > numeric srotage type. > > I have tried using as.numeric() function but it changes the values in the > variable to what I think are the ranks of the individual values of the > varible in the dataset. For example if 5.735 is the current content in the > field, then the new object created by as.numeric will contain a value like > 680 if the 5.735 was the highest value for the varible and the dataset had > 680 records. > > > How can I change the storage type without changing the contents of this > variable in this case? > > Thanks for your consideration. > > > > -- > Ojal John Owino > P.O Box 230-80108 > Kilifi, Kenya. > Mobile:+254 728 095 710 > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Mike Lawrence Graduate Student Department of Psychology Dalhousie University Looking to arrange a meeting? Check my public calendar: tinyurl.com/mikes-public-calendar ~ Certainty is folly... I think. ~
Hi Ojal, I don't know why it happens, but try as.numeric(as.character(XXX)) Good luck miltinho astronauta brazil On Tue, Mar 10, 2009 at 1:25 AM, ojal john owino < ojal.johnowino@googlemail.com> wrote:> Dear Users, > I have a variable in my dataset which is of type factor. But it actually > contains numeric entries which like 5.735 4.759 ..... This is because the > data was read from a CSV file into R and this variable contained other > charaters which were not numeric. I have now dropped the records with the > characters which are not numeric for this variable and want to change it to > numeric srotage type. > > I have tried using as.numeric() function but it changes the values in the > variable to what I think are the ranks of the individual values of the > varible in the dataset. For example if 5.735 is the current content in the > field, then the new object created by as.numeric will contain a value like > 680 if the 5.735 was the highest value for the varible and the dataset had > 680 records. > > > How can I change the storage type without changing the contents of this > variable in this case? > > Thanks for your consideration. > > > > -- > Ojal John Owino > P.O Box 230-80108 > Kilifi, Kenya. > Mobile:+254 728 095 710 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
If you don't need any factors use read.csv(...whatever..., as.is = TRUE) and then as.numeric will work. On Tue, Mar 10, 2009 at 1:25 AM, ojal john owino <ojal.johnowino at googlemail.com> wrote:> Dear Users, > I have a variable in my dataset which is of type factor. But it actually > contains numeric entries which like 5.735 ?4.759 ..... This is because the > data was read from a CSV file into R and this variable contained other > charaters which were not numeric. I have now dropped the records with the > characters which are not numeric for this variable and want to change it to > numeric srotage type. > > I have tried using as.numeric() function but it changes the values in the > variable to what I think are the ranks of the individual values of the > varible in the dataset. For example if 5.735 is the current content in the > field, then the new object created by as.numeric will contain a value like > 680 if the 5.735 was the highest value for the varible and the dataset had > 680 records. > > > How can I change the storage type without changing the contents of this > variable in this case? > > Thanks for your consideration. > > > > -- > Ojal John Owino > P.O Box 230-80108 > Kilifi, Kenya. > Mobile:+254 728 095 710 > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
By the way, it could be that one of your numbers has "space" in them. in which case, R tends to turn the entire vector into a factor. try opening the file in a spreadsheet like excel, and do "search replace" on " " with "". and see how many it catches. Tal On Tue, Mar 10, 2009 at 7:25 AM, ojal john owino < ojal.johnowino@googlemail.com> wrote:> Dear Users, > I have a variable in my dataset which is of type factor. But it actually > contains numeric entries which like 5.735 4.759 ..... This is because the > data was read from a CSV file into R and this variable contained other > charaters which were not numeric. I have now dropped the records with the > characters which are not numeric for this variable and want to change it to > numeric srotage type. > > I have tried using as.numeric() function but it changes the values in the > variable to what I think are the ranks of the individual values of the > varible in the dataset. For example if 5.735 is the current content in the > field, then the new object created by as.numeric will contain a value like > 680 if the 5.735 was the highest value for the varible and the dataset had > 680 records. > > > How can I change the storage type without changing the contents of this > variable in this case? > > Thanks for your consideration. > > > > -- > Ojal John Owino > P.O Box 230-80108 > Kilifi, Kenya. > Mobile:+254 728 095 710 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- ---------------------------------------------- My contact information: Tal Galili Phone number: 972-50-3373767 FaceBook: Tal Galili My Blogs: talgalili.com biostatistics.co.il [[alternative HTML version deleted]]