I assume that I have missed something fundamental and that it is there in front of me in "An Introduction to R", but I need someone to point me in the right direction.> x1 <- "1159 1129 1124 -5 -0.44 -1.52" > x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > x3 <- unlist(strsplit(x1," ")) > > > str(x2)chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52"> str(x3)chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52"> > as.numeric(x2)[1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52> as.numeric(x3)[1] 1159 1129 1124 NA NA NA Warning message: NAs introduced by coercion What do I have to do to get x3 to be the same as x2. Tom
your code works for me, e.g.,> x1 <- "1159 1129 1124 -5 -0.44 -1.52" > x2 <- c("1159", "1129", "1124", "-5", "-0.44", "-1.52") > x3 <- unlist(strsplit(x1, " ")) > ################ > all.equal(x2, x3)[1] TRUE> as.numeric(x2)[1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52> as.numeric(x3)[1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 maybe you could restart R and try again. Best, Dimitris -- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting "Mulholland, Tom" <Tom.Mulholland at dpi.wa.gov.au>:> I assume that I have missed something fundamental and that it is > there in front of me in "An Introduction to R", but I need someone to > point me in the right direction. > > > x1 <- "1159 1129 1124 -5 -0.44 -1.52" > > x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > > x3 <- unlist(strsplit(x1," ")) > > > > > > str(x2) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > str(x3) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > > > as.numeric(x2) > [1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 > > as.numeric(x3) > [1] 1159 1129 1124 NA NA NA > Warning message: > NAs introduced by coercion > > What do I have to do to get x3 to be the same as x2. > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
After replies off the list which indicate the code should work. I tried a variety of approaches. Rebooting, Using the --vanilla option and then removing the whole lot and resinstalling. It now works. I guess it's another of those windows things? Thanks to those that helped.> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of Mulholland, Tom > Sent: Friday, 19 May 2006 11:48 AM > To: R-Help (E-mail) > Subject: [R] Converting character strings to numeric > > > I assume that I have missed something fundamental and that it > is there in front of me in "An Introduction to R", but I need > someone to point me in the right direction. > > > x1 <- "1159 1129 1124 -5 -0.44 -1.52" > > x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > > x3 <- unlist(strsplit(x1," ")) > > > > > > str(x2) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > str(x3) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > > > as.numeric(x2) > [1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 > > as.numeric(x3) > [1] 1159 1129 1124 NA NA NA > Warning message: > NAs introduced by coercion > > What do I have to do to get x3 to be the same as x2. > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html
Hi Maybe change your R version? Works for me R 2.3.0pat, W 2000> x1 <- "1159 1129 1124 -5 -0.44 -1.52" > x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > x3 <- unlist(strsplit(x1," ")) > str(x2)chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52"> str(x3)chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52"> as.numeric(x2)[1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52> as.numeric(x3)[1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52> >HTH Petr On 19 May 2006 at 11:47, Mulholland, Tom wrote: Date sent: Fri, 19 May 2006 11:47:54 +0800 From: "Mulholland, Tom" <Tom.Mulholland at dpi.wa.gov.au> To: "R-Help (E-mail)" <r-help at stat.math.ethz.ch> Subject: [R] Converting character strings to numeric> I assume that I have missed something fundamental and that it is there > in front of me in "An Introduction to R", but I need someone to point > me in the right direction. > > > x1 <- "1159 1129 1124 -5 -0.44 -1.52" > > x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > > x3 <- unlist(strsplit(x1," ")) > > > > > > str(x2) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > str(x3) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > > > as.numeric(x2) > [1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 > > as.numeric(x3) > [1] 1159 1129 1124 NA NA NA > Warning message: > NAs introduced by coercion > > What do I have to do to get x3 to be the same as x2. > > Tom > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
I think you are correct (as expected) I don't know where in the original data the string is, but there is other data doing the same thing. + > strsplit(test," ")[[1]] [1] "5159" "3336" "3657" "559" "3042" "55" "307" "-8" "16104"> as.numeric(strsplit(test," ")[[1]])[1] 5159 3336 3657 559 3042 55 307 NA 16104 Warning message: NAs introduced by coercion> charToRaw(test)[1] 35 31 35 39 20 33 33 33 36 20 33 36 35 37 20 35 35 39 20 33 30 34 32 20 35 35 20 33 30 37 20 96 38 20 31 36 31 30 34> test[1] "5159 3336 3657 559 3042 55 307 -8 16104"> x1 <- "5159 3336 3657 559 3042 55 307 -8 16104" > charToRaw(x1)[1] 35 31 35 39 20 33 33 33 36 20 33 36 35 37 20 35 35 39 20 33 30 34 32 20 35 35 20 33 30 37 20 2d 38 20 31 36 31 30 34> as.numeric(strsplit(x1," ")[[1]])[1] 5159 3336 3657 559 3042 55 307 -8 16104>So it looks as if the 96 is throwing it out. I'll dig deeper. I guess there's a bit more pre-Processing to do. The only thing that seems slightly strange is that the small example I made up did not use the original data source, but was typed in the same way I did x1 above. However I can't reproduce the error so it may still be a case of finger trouble on my part. Tom> -----Original Message----- > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > Sent: Friday, 19 May 2006 3:03 PM > To: Mulholland, Tom > Cc: R-Help (E-mail) > Subject: Re: [R] Converting character strings to numeric > > > On Fri, 19 May 2006, Mulholland, Tom wrote: > > > After replies off the list which indicate the code should > work. I tried a variety of approaches. > > > > Rebooting, Using the --vanilla option and then removing the > whole lot and resinstalling. It now works. > > > > I guess it's another of those windows things? > > No, it works under Windows. > > What you have not shown us is x3: > > > x3 > [1] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > > My guess is that you have something invisible in x1, e.g. a > nbspace not a > space (although that does not fully explain the results). What does > > > charToRaw(x1) > [1] 31 31 35 39 20 31 31 32 39 20 31 31 32 34 20 2d 35 20 > 2d 30 2e 34 34 20 2d > [26] 31 2e 35 32 > > give for you? > > > > > > > Thanks to those that helped. > > > >> -----Original Message----- > >> From: r-help-bounces at stat.math.ethz.ch > >> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of > Mulholland, Tom > >> Sent: Friday, 19 May 2006 11:48 AM > >> To: R-Help (E-mail) > >> Subject: [R] Converting character strings to numeric > >> > >> > >> I assume that I have missed something fundamental and that it > >> is there in front of me in "An Introduction to R", but I need > >> someone to point me in the right direction. > >> > >>> x1 <- "1159 1129 1124 -5 -0.44 -1.52" > >>> x2 <- c("1159","1129","1124","-5","-0.44","-1.52") > >>> x3 <- unlist(strsplit(x1," ")) > >>> > >>> > >>> str(x2) > >> chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > >>> str(x3) > >> chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" > >>> > >>> as.numeric(x2) > >> [1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 > >>> as.numeric(x3) > >> [1] 1159 1129 1124 NA NA NA > >> Warning message: > >> NAs introduced by coercion > >> > >> What do I have to do to get x3 to be the same as x2. > >> > >> Tom > > -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 >
On 5/18/2006 11:47 PM, Mulholland, Tom wrote:> I assume that I have missed something fundamental and that it is there in front of me in "An Introduction to R", but I need someone to point me in the right direction. > >> x1 <- "1159 1129 1124 -5 -0.44 -1.52" >> x2 <- c("1159","1129","1124","-5","-0.44","-1.52") >> x3 <- unlist(strsplit(x1," ")) >> >> >> str(x2) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" >> str(x3) > chr [1:6] "1159" "1129" "1124" "-5" "-0.44" "-1.52" >> as.numeric(x2) > [1] 1159.00 1129.00 1124.00 -5.00 -0.44 -1.52 >> as.numeric(x3) > [1] 1159 1129 1124 NA NA NA > Warning message: > NAs introduced by coercion > > What do I have to do to get x3 to be the same as x2.They should be, and are on my system in 2.2.1 and 2.3.0. Which version/platform are you using? Duncan Murdoch