[R] creating character vector

peter dalgaard pdalgd at gmail.com
Mon Mar 14 21:37:23 CET 2011


On Mar 14, 2011, at 16:32 , Allan Engelhardt wrote:

> You could try
> 
> scan(what=character(0), sep=",", file=textConnection("first,second,third"))
> 
> 
> but better to put the strings in a file (say, strings.txt), one per line, and read it using
> 
> scan("strings.txt", what=character(0), sep="\n")

Also, things like this come in handy at times:

## From dept. of slightly dirty trix:
txt <- "fmffmfmfmfmfmmmmfmmmffmff"
lizards$sex <- factor(unlist(strsplit(txt,"")),levels=c("m","f"))

It is easily generalized to include a delimiter.

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list