Greetings, Amigos: I have been trying without success to convert a character string,> repeated.measures.columns[1] "3,6,10" into c(3,6,10) for subsequent use. as.numeric(repeated.measures.columns) doesn't work (likely because of the commas) [1] NA Warning message: NAs introduced by coercion I've tried many things including strsplit(repeated.measures.columns, split = ",") which produces a list with only one element, viz: [[1]] [1] "3" "6" "10" as.numeric() doesn't like that either. Clearly: 1) I cannot be the first person to attempt this, and 2) I've made this WAY harder than it is. Would some kind soul please instruct me (and perhaps subsequent searchers) how to convert the elements of a string into numbers? Thank you. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 StatisticalEngineering.com ?
On Sat, 2006-08-19 at 07:58 -0400, Charles Annis, P.E. wrote:> Greetings, Amigos: > > I have been trying without success to convert a character string, > > repeated.measures.columns > [1] "3,6,10" > > into c(3,6,10) for subsequent use. > > as.numeric(repeated.measures.columns) doesn't work (likely because of the > commas) > [1] NA > Warning message: > NAs introduced by coercion > > I've tried many things including > strsplit(repeated.measures.columns, split = ",") > > which produces a list with only one element, viz: > [[1]] > [1] "3" "6" "10" > > as.numeric() doesn't like that either. > > Clearly: 1) I cannot be the first person to attempt this, and 2) I've made > this WAY harder than it is. > > Would some kind soul please instruct me (and perhaps subsequent searchers) > how to convert the elements of a string into numbers? > > Thank you.One more step:> as.numeric(unlist(strsplit(repeated.measures.columns, ",")))[1] 3 6 10 Use unlist() to take the output of strsplit() and convert it to a vector, before coercing to numeric. HTH, Marc Schwartz
"Charles Annis, P.E." <Charles.Annis at statisticalengineering.com> writes:> Greetings, Amigos: > > I have been trying without success to convert a character string, > > repeated.measures.columns > [1] "3,6,10" > > into c(3,6,10) for subsequent use. > > as.numeric(repeated.measures.columns) doesn't work (likely because of the > commas) > [1] NA > Warning message: > NAs introduced by coercion > > I've tried many things including > strsplit(repeated.measures.columns, split = ",") > > which produces a list with only one element, viz: > [[1]] > [1] "3" "6" "10" > > as.numeric() doesn't like that either. > > Clearly: 1) I cannot be the first person to attempt this, and 2) I've made > this WAY harder than it is. > > Would some kind soul please instruct me (and perhaps subsequent searchers) > how to convert the elements of a string into numbers?3) you're almost there, just not realizing it:> x <- "3,6,10" > as.numeric(strsplit(x,split = ",")[[1]])[1] 3 6 10 or for that matter> scan(textConnection(x), sep=",")Read 3 items [1] 3 6 10 although that leaves you with a dangling open connection. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Sat, 19 Aug 2006, Charles Annis, P.E. wrote:> Greetings, Amigos: > > I have been trying without success to convert a character string, > > repeated.measures.columns > [1] "3,6,10" > > into c(3,6,10) for subsequent use. > > as.numeric(repeated.measures.columns) doesn't work (likely because of the > commas) > [1] NA > Warning message: > NAs introduced by coercion > > I've tried many things including > strsplit(repeated.measures.columns, split = ",") > > which produces a list with only one element, viz: > [[1]] > [1] "3" "6" "10" > > as.numeric() doesn't like that either.repeated.measures.columns is a vector. Consider: repeated.measures.columns <- c("3,6,10", "5,4,9") lst <- strsplit(repeated.measures.columns, split = ",") lapply(lst, as.numeric) which is why strsplit() returns a list - one list component for each repeated.measures.columns element. Just pick off the one you want with [[]]: as.numeric(strsplit(repeated.measures.columns, split = ",")[[1]])> > Clearly: 1) I cannot be the first person to attempt this, and 2) I've made > this WAY harder than it is. > > Would some kind soul please instruct me (and perhaps subsequent searchers) > how to convert the elements of a string into numbers? > > Thank you. > > > Charles Annis, P.E. > > Charles.Annis at StatisticalEngineering.com > phone: 561-352-9699 > eFax:? 614-455-3265 > StatisticalEngineering.com > ? > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Much gratitude to Professor Ripley, Peter Dalgaard, Marc Schwartz, and Roger Bivand. __________________ Roger Bivand wrote that ... strsplit() returns a list - one list component for each repeated.measures.columns element. Just pick off the one you want with [[]]: as.numeric(strsplit(repeated.measures.columns, split = ",")[[1]]) which had stumped me, since that syntax fails without the [[1]] specification. __________________ Peter Dalgaard, who also suggested the [[1]] specification, pointed out that scan(textConnection(x), sep=",") will work, although that leaves you with a dangling open connection. __________________ Marc Schwartz advised to ... Use unlist() to take the output of strsplit() and convert it to a vector, before coercing to numeric. as.numeric(unlist(strsplit(repeated.measures.columns, ","))) ____________________________________ Brian D. Ripley suggested that the following looks competitive, and is quite a bit more general (e.g. allows spaces, works with complex numbers) eval(parse(text=paste("c(", repeated.measures.columns, ")"))) and Marc Schwartz showed that Professor Ripley's suggestion is much faster than the competition with some system.time trials. ____________________________________ Many thanks to all. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax: 614-455-3265 StatisticalEngineering.com -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Charles Annis, P.E. Sent: Saturday, August 19, 2006 7:59 AM To: r-help at stat.math.ethz.ch Subject: [R] string-to-number Greetings, Amigos: I have been trying without success to convert a character string,> repeated.measures.columns[1] "3,6,10" into c(3,6,10) for subsequent use. as.numeric(repeated.measures.columns) doesn't work (likely because of the commas) [1] NA Warning message: NAs introduced by coercion I've tried many things including strsplit(repeated.measures.columns, split = ",") which produces a list with only one element, viz: [[1]] [1] "3" "6" "10" as.numeric() doesn't like that either. Clearly: 1) I cannot be the first person to attempt this, and 2) I've made this WAY harder than it is. Would some kind soul please instruct me (and perhaps subsequent searchers) how to convert the elements of a string into numbers? Thank you. Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com phone: 561-352-9699 eFax:? 614-455-3265 StatisticalEngineering.com ? ______________________________________________ R-help at stat.math.ethz.ch 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.