Re: [R] Simple but elusive - expand back from counts

2011-03-29 Thread jjap
Many thanks to all, the first 3 solutions worked nicely but I did not get around to tweaking properly the others. It's really nice to get thing going in a "one liner" or about... Again greatly appreciated. Cheers! -- View this message in context: http://r.789695.n4.nabble.com/Simple-but-elusive-e

Re: [R] Simple but elusive - expand back from counts

2011-03-29 Thread Henrique Dallazuanna
Try this: transform(tmp[rep(seq(nrow(tmp)), as.numeric(tmp$V4)),], V4 = 1) On Tue, Mar 29, 2011 at 3:15 PM, jjap wrote: > Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3,

Re: [R] Simple but elusive - expand back from counts

2011-03-29 Thread Peter Alspach
Tena koe Try something like: tmp[rep(1:nrow(tmp), each=tmp[,4]),] # untested HTH ... Peter Alspach > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of jjap > Sent: Wednesday, 30 March 2011 7:16 a.m. > To: r-help@r-project.org

Re: [R] Simple but elusive - expand back from counts

2011-03-29 Thread Petr Savicky
On Tue, Mar 29, 2011 at 11:15:33AM -0700, jjap wrote: > Dear R-users, > > This should be simple but still eludes me: > > Given the following > tmp<-as.data.frame(matrix(c(44, 10, "abc", 1, 44, 10, "def", 1, 44, 12, > "abc", 2), 3, 4, byrow=T)) > > I want to expand the data to the following form

Re: [R] Simple but elusive - expand back from counts

2011-03-29 Thread Joshua Wiley
Hi, You can use rep() to repeat the appropriate rows as in V4. For example: tmp[rep(rownames(tmp), tmp$V4), ] V1 V2 V3 V4 1 44 10 abc 1 2 44 10 def 1 3 44 12 abc 2 3.1 44 12 abc 2 if you wanted, you could then reset the row names to NULL to get 1, 2, 3, 4 rather than 3, 3.1 row

Re: [R] Simple... but...

2008-07-23 Thread Jorge Ivan Velez
,2,3) > > Thanks, Shubha > > -Original Message- > From: Doran, Harold [mailto:[EMAIL PROTECTED] > Sent: Wednesday, July 23, 2008 6:47 PM > To: Shubha Vishwanath Karanth; [EMAIL PROTECTED] > Subject: RE: [R] Simple... but... > > x <- c(1,3,5) >

Re: [R] Simple... but...

2008-07-23 Thread Wacek Kusnierczyk
john seers (IFR) wrote: > > > > > This is definitely the best way: > > c(lapply(1:length(x), function(i, x, y) c(x[i], y[i]), x, y), > recursive=TRUE) > > > you'd better test it yourself; it simply won't do the job -- you get a list of vectors, no? (hint: replace the initial 'c' with 'unlis

Re: [R] Simple... but...

2008-07-23 Thread Wacek Kusnierczyk
ROTECTED] >> Sent: Wednesday, July 23, 2008 9:17 AM >> To: Doran, Harold; [EMAIL PROTECTED] >> Subject: RE: [R] Simple... but... >> >> OK, >> >> Let x=c(4,2,2) >> y=c(1,5,3) >> >> My result should be c(4,1,2,5,2,3) >> >> Th

Re: [R] Simple... but...

2008-07-23 Thread Prof Brian Ripley
On Wed, 23 Jul 2008, Shubha Vishwanath Karanth wrote: If x=c(1,3,5) y=c(2,4,6) I need a vector which is c(1,2,3,4,5,6) from x and y. I am assuming that you want to interleave the vectors, but that is not the only algorithm giving that result. How do I do it? I mean the best way as.

Re: [R] Simple... but...

2008-07-23 Thread Wacek Kusnierczyk
Shubha Vishwanath Karanth wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way > > ... but seriously, rbind(x,y)[1:6] is one concise way to do it (x and y are bound into rows i

Re: [R] Simple... but...

2008-07-23 Thread Ben Bolker
Doran, Harold air.org> writes: > > Shubba > > I'm confused. Your first post said the result should be c(1,2,3,4,5,6) > when x and y are combined. The code I sent does that. But here you say > your result should be c(4,1,2,5,2,3). > > What do you want your result to actually be? > I think

Re: [R] Simple... but...

2008-07-23 Thread Gustaf Rydevik
> > What do you want your result to actually be? > >> -Original Message- >> From: Shubha Vishwanath Karanth [mailto:[EMAIL PROTECTED] >> Sent: Wednesday, July 23, 2008 9:17 AM >> To: Doran, Harold; [EMAIL PROTECTED] >> Subject: RE: [R] Simple... but... &

Re: [R] Simple... but...

2008-07-23 Thread Gabor Grothendieck
Try c(rbind(x, y)) On Wed, Jul 23, 2008 at 8:54 AM, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way > > > > Thanks, Shubha > > > > Thi

Re: [R] Simple... but...

2008-07-23 Thread Ted Harding
On 23-Jul-08 12:54:49, Shubha Vishwanath Karanth wrote: > Hi R, > If > x=c(1,3,5) > y=c(2,4,6) > > I need a vector which is c(1,2,3,4,5,6) from x and y. > How do I do it? I mean the best way > Thanks, Shubha Your query is ambiguous, in that it is not clear whether you want a) The elements of

Re: [R] Simple... but...

2008-07-23 Thread Nutter, Benjamin
> x <- c(1,3,5) > y <- c(2,4,6) > xy <- sort(c(x,y)) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, July 23, 2008 8:55 AM To: [EMAIL PROTECTED] Subject: [R] Simple... but... Hi R, If x=c(1,3,5) y=c(2,4,6)

Re: [R] Simple... but...

2008-07-23 Thread Jim Porzak
Hi Shubha, Assuming you are after ordering by position in x and y (and not values), how about as.vector(t(cbind(x, y))) HTH, Jim Porzak Responsys, Inc. San Francisco, CA http://www.linkedin.com/in/jimporzak On Wed, Jul 23, 2008 at 5:54 AM, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: >

Re: [R] Simple... but...

2008-07-23 Thread Dimitris Rizopoulos
check the following: x <- c(1,3,5) y <- c(2,4,6) c(t(cbind(x, y))) c(matrix(c(x, y), 2, by = TRUE)) I hope it helps. Best, Dimitris Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16

Re: [R] Simple... but...

2008-07-23 Thread Berwin A Turlach
G'day Shubha, On Wed, 23 Jul 2008 18:24:49 +0530 "Shubha Vishwanath Karanth" <[EMAIL PROTECTED]> wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way R> as.vector(rbind

Re: [R] Simple... but...

2008-07-23 Thread ONKELINX, Thierry
Something like sort(c(x, y)) should do the trick. HTH, Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Sec

Re: [R] Simple... but...

2008-07-23 Thread Doran, Harold
How about this as.vector(t(cbind(x,y))) > -Original Message- > From: Shubha Vishwanath Karanth [mailto:[EMAIL PROTECTED] > Sent: Wednesday, July 23, 2008 9:17 AM > To: Doran, Harold; [EMAIL PROTECTED] > Subject: RE: [R] Simple... but... > > OK, > > Let x

Re: [R] Simple... but...

2008-07-23 Thread john seers (IFR)
This is definitely the best way: c(lapply(1:length(x), function(i, x, y) c(x[i], y[i]), x, y), recursive=TRUE) JS --- -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Shubha Vishwanath Karanth Sent: 23 July 2008 13:55 To: [EMAIL PROTECTED] Sub

Re: [R] Simple... but...

2008-07-23 Thread Shubha Vishwanath Karanth
OK, Let x=c(4,2,2) y=c(1,5,3) My result should be c(4,1,2,5,2,3) Thanks, Shubha -Original Message- From: Doran, Harold [mailto:[EMAIL PROTECTED] Sent: Wednesday, July 23, 2008 6:47 PM To: Shubha Vishwanath Karanth; [EMAIL PROTECTED] Subject: RE: [R] Simple... but... x <-

Re: [R] Simple... but...

2008-07-23 Thread Mitchell Maltenfort
Z=NULL;for (i in 1:3) Z=c(Z,c(x[i],y[i])) On Wed, Jul 23, 2008 at 8:54 AM, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way > > > > Th

Re: [R] Simple... but...

2008-07-23 Thread Wacek Kusnierczyk
Shubha Vishwanath Karanth wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. > > > > How do I do it? I mean the best way > > > the absolutely best way is: ?c vQ __ R-hel

Re: [R] Simple... but...

2008-07-23 Thread Doran, Harold
x <- c(1,3,5) y <- c(2,4,6) sort(c(x,y)) > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Shubha > Vishwanath Karanth > Sent: Wednesday, July 23, 2008 8:55 AM > To: [EMAIL PROTECTED] > Subject: [R] Simple... but... > > Hi R, > > > > If > >

Re: [R] Simple... but...

2008-07-23 Thread Shubha Vishwanath Karanth
Guess this should be fine c(rbind(x,y)) Please let me know if there are some better ways... Thanks, Shubha -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Shubha Vishwanath Karanth Sent: Wednesday, July 23, 2008 6:25 PM To: [EMAIL PROTECTED] Subje

Re: [R] Simple... but...

2008-07-23 Thread Doran, Harold
ath Karanth [mailto:[EMAIL PROTECTED] > Sent: Wednesday, July 23, 2008 9:17 AM > To: Doran, Harold; [EMAIL PROTECTED] > Subject: RE: [R] Simple... but... > > OK, > > Let x=c(4,2,2) > y=c(1,5,3) > > My result should be c(4,1,2,5,2,3) > > Thanks, Shubha

Re: [R] Simple... but...

2008-07-23 Thread Henrique Dallazuanna
If I understand: sort(c(x,y)) or if you want combine the elements: as.vector(mapply(c, x, y)) On Wed, Jul 23, 2008 at 9:54 AM, Shubha Vishwanath Karanth <[EMAIL PROTECTED]> wrote: > Hi R, > > > > If > > x=c(1,3,5) > > y=c(2,4,6) > > > > I need a vector which is c(1,2,3,4,5,6) from x and y. >