s-news
[Top] [All Lists]

Re: simple concatenation question

To: "'Johnnidis, Jonathan'" <Jonathan.Johnnidis@joslin.harvard.edu>, S-news <s-news@lists.biostat.wustl.edu>
Subject: Re: simple concatenation question
From: "Fowler, Mark" <FowlerM@mar.dfo-mpo.gc.ca>
Date: Wed, 19 May 2004 08:20:42 -0300
Possibly all you want is;

whatever<-paste(test,collapse=" ")

e.g.
> test <- paste("no",1:3)
> test
[1] "no 1" "no 2" "no 3"
> x<-paste(test,collapse=" ")
> x
[1] "no 1 no 2 no 3"

>       Mark Fowler
>       Marine Fish Division
>       Bedford Inst of Oceanography
>       Dept Fisheries & Oceans
>       Dartmouth NS Canada
>       fowlerm@mar.dfo-mpo.gc.ca
>


-----Original Message-----
From: Johnnidis, Jonathan [mailto:Jonathan.Johnnidis@joslin.harvard.edu] 
Sent: May 18, 2004 11:00 PM
To: S-news
Subject: [S] simple concatenation question


greetings listers,
apologies for the somewhat trivial nature of this question, but I'd much
appreciate it if anyone could point me in the right direction:

I have a vector (numerical or character) of length x and would like to
output the contents of the entire vector into a single vector of length 1.

for example:

> test <- paste("no",1:3)

and then I see:

> test
[1] "no 1" "no 2" "no 3"
> length(test)
[1] 3

what I want though is an output that looks like this:

> paste(test[1],test[2],test[3])
[1] "no 1 no 2 no 3"


but the above command is lacking in elegance and furthermore impractical if
one doesn't know the length of the starting 'test' vector.

i should note my first idea was to use cat() as in
> cat(test)

but when I try
> output <- cat(test)

for some reason this doesn't work and the vector 'output' exists but is
empty. is there a way to direct cat() output from the screen into an
assigned vector? or perhaps another command I have not considered?


with thanks for any suggestions,
Jonathan Johnnidis

ps: S+6.1R1/Win
--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with the
BODY of the message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>