[R] creating character vector

peter dalgaard
Mon, 14 Mar 2011 20:37:23 -0000

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