Simple question
On Apr 19, 2011, at 21:56 , David Winsemius wrote:
On Apr 19, 2011, at 3:19 PM, Steven Wolf wrote:
I am trying to convert a string to a vector, and I'm stuck! Suppose you have a string of numbers (string) that you want to convert to a vector (vec). I am able to split the string turning it into a list by using strsplit, but this makes a list, which I can't seem to access the way that I'd like to (it becomes a character list of only one(?!?) element.) string<-"1,1,2,3,5,8" list<-strsplit(string,",") list # Produces this ugly output with the [[1]] crap which doesn't let me in!!!!!!!
It produces a list. To return an unlist-ed version, what else?
unlist(list)
[1] "1" "1" "2" "3" "5" "8" And to make it a numeric vector ... also simple:
as.numeric(unlist(list))
[1] 1 1 2 3 5 8
Also, there is the possibility to use scan() on a text connection
string<-"1,1,2,3,5,8" v <- scan(tc <- textConnection(string), sep=","); close(tc)
Read 6 items
v
[1] 1 1 2 3 5 8 In r-devel, this works too:
v <- scan(text=string, sep=",")
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