Re: [R] Simple question

From: peter dalgaard <pdalgd_at_gmail.com>
Date: Tue, 19 Apr 2011 23:30:31 +0200

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Tue 19 Apr 2011 - 21:33:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 19 Apr 2011 - 22:20:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive