Hi, Suppose I have created something like this in R: foo <- "myfoo" and I want to find out the number of character in foo (in other words, R should return 5 since "myfoo" has 5 charactors. How can I do it? I tried: length(foo) but it returned 1. Cheers, Kevin ------------------------------------------------------------------------------ Ko-Kang Kevin Wang Postgraduate PGDipSci Student Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 E-mail: kwan022 at stat.auckland.ac.nz -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
foo <- "myfoo" nchar(foo) [1] 5 Ole Ko-Kang Kevin Wang wrote:> > Hi, > > Suppose I have created something like this in R: > foo <- "myfoo" > and I want to find out the number of character in foo (in other words, R > should return 5 since "myfoo" has 5 charactors. > > How can I do it? I tried: > length(foo) > but it returned 1. > > Cheers, > > Kevin > > ------------------------------------------------------------------------------ > Ko-Kang Kevin Wang > Postgraduate PGDipSci Student > Department of Statistics > University of Auckland > New Zealand > > Homepage: http://www.stat.auckland.ac.nz/~kwan022 > > E-mail: kwan022 at stat.auckland.ac.nz > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Ole F. Christensen Department of Mathematics and Statistics Fylde College, Lancaster University Lancaster, LA1 4YF, England -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
It worked! Thanks a lot for the prompt reply. However this remind me of another question. If I entered my string like: foo <- "myfoo" then, if there anyway to convert foo into a form that is: c("m", "y", "f", "o", "o") directly? Thanks again in advance, Kevin ------------------------------------------------ Ko-Kang Kevin Wang Post Graduate PGDipSci Student Department of Statistics University of Auckland New Zealand www.stat.auckand.ac.nz/~kwan022 ----- Original Message ----- From: "Ole Christensen" <o.christensen at lancaster.ac.uk> To: "Ko-Kang Kevin Wang" <kwan022 at stat.auckland.ac.nz> Cc: "R Help" <r-help at stat.math.ethz.ch> Sent: Sunday, May 19, 2002 10:35 AM Subject: Re: [R] Length of a string> foo <- "myfoo" > nchar(foo) > [1] 5 > > Ole > > > Ko-Kang Kevin Wang wrote: > > > > Hi, > > > > Suppose I have created something like this in R: > > foo <- "myfoo" > > and I want to find out the number of character in foo (in other words, R > > should return 5 since "myfoo" has 5 charactors. > > > > How can I do it? I tried: > > length(foo) > > but it returned 1. > > > > Cheers, > > > > Kevin > > > > ------------------------------------------------------------------------------> > Ko-Kang Kevin Wang > > Postgraduate PGDipSci Student > > Department of Statistics > > University of Auckland > > New Zealand > > > > Homepage: http://www.stat.auckland.ac.nz/~kwan022 > > > > E-mail: kwan022 at stat.auckland.ac.nz > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> > r-help mailing list -- Readhttp://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> > Send "info", "help", or "[un]subscribe" > > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._> > -- > Ole F. Christensen > Department of Mathematics and Statistics > Fylde College, Lancaster University > Lancaster, LA1 4YF, England > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-> r-help mailing list -- Readhttp://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._>-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kevin, Use strsplit(foo,"").> foo <- "myfoo" > newfoo <- strsplit(foo,"") > newfoo[[1]] [1] "m" "y" "f" "o" "o" For more information: ?strsplit Marc> -----Original Message----- > From: owner-r-help at stat.math.ethz.ch [mailto:owner-r- > help at stat.math.ethz.ch] On Behalf Of Ko-Kang Kevin Wang > Sent: Saturday, May 18, 2002 6:17 PM > To: Ole Christensen > Cc: R Help > Subject: Re: [R] Length of a string > > It worked! Thanks a lot for the prompt reply. > > However this remind me of another question. If I entered my stringlike:> foo <- "myfoo" > then, if there anyway to convert foo into a form that is: > c("m", "y", "f", "o", "o") > directly? > > Thanks again in advance, > > Kevin >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Try foo <- "myfoo" unlist(strsplit(foo, split = "")) -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Sun, 19 May 2002, Ko-Kang Kevin Wang wrote:> It worked! Thanks a lot for the prompt reply. > > However this remind me of another question. If I entered my string like: > foo <- "myfoo" > then, if there anyway to convert foo into a form that is: > c("m", "y", "f", "o", "o") > directly? > > Thanks again in advance, > > Kevin > > ------------------------------------------------ > Ko-Kang Kevin Wang > Post Graduate PGDipSci Student > Department of Statistics > University of Auckland > New Zealand > www.stat.auckand.ac.nz/~kwan022 > > ----- Original Message ----- > From: "Ole Christensen" <o.christensen at lancaster.ac.uk> > To: "Ko-Kang Kevin Wang" <kwan022 at stat.auckland.ac.nz> > Cc: "R Help" <r-help at stat.math.ethz.ch> > Sent: Sunday, May 19, 2002 10:35 AM > Subject: Re: [R] Length of a string > > > > foo <- "myfoo" > > nchar(foo) > > [1] 5 > > > > Ole > > > > > > Ko-Kang Kevin Wang wrote: > > > > > > Hi, > > > > > > Suppose I have created something like this in R: > > > foo <- "myfoo" > > > and I want to find out the number of character in foo (in other words, R > > > should return 5 since "myfoo" has 5 charactors. > > > > > > How can I do it? I tried: > > > length(foo) > > > but it returned 1. > > > > > > Cheers, > > > > > > Kevin > > > > > > > -------------------------------------------------------------------------- > ---- > > > Ko-Kang Kevin Wang > > > Postgraduate PGDipSci Student > > > Department of Statistics > > > University of Auckland > > > New Zealand > > > > > > Homepage: http://www.stat.auckland.ac.nz/~kwan022 > > > > > > E-mail: kwan022 at stat.auckland.ac.nz > > > > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.- > > > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > > > Send "info", "help", or "[un]subscribe" > > > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > > > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._ > > > > -- > > Ole F. Christensen > > Department of Mathematics and Statistics > > Fylde College, Lancaster University > > Lancaster, LA1 4YF, England > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.- > > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > > Send "info", "help", or "[un]subscribe" > > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > > > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._ > > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Kevin, Sorry, I forgot one step in my first reply. Strsplit results in a list, so use unlist() to reduce the list to a vector. So add:> newfoo <- unlist(newfoo)> newfoo[1] "m" "y" "f" "o" "o" Marc> -----Original Message----- > From: Marc Schwartz [mailto:MSchwartz at MedAnalytics.com] > Sent: Saturday, May 18, 2002 8:11 PM > To: 'Ko-Kang Kevin Wang' > Cc: 'R Help' > Subject: RE: [R] Length of a string > > Kevin, > > Use strsplit(foo,""). > > > foo <- "myfoo" > > newfoo <- strsplit(foo,"") > > newfoo > [[1]] > [1] "m" "y" "f" "o" "o" > > For more information: > > ?strsplit > > Marc > > > -----Original Message----- > > From: owner-r-help at stat.math.ethz.ch [mailto:owner-r- > > help at stat.math.ethz.ch] On Behalf Of Ko-Kang Kevin Wang > > Sent: Saturday, May 18, 2002 6:17 PM > > To: Ole Christensen > > Cc: R Help > > Subject: Re: [R] Length of a string > > > > It worked! Thanks a lot for the prompt reply. > > > > However this remind me of another question. If I entered my string > like: > > foo <- "myfoo" > > then, if there anyway to convert foo into a form that is: > > c("m", "y", "f", "o", "o") > > directly? > > > > Thanks again in advance, > > > > Kevin > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._