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
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,
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
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
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
,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)
>
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
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
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.
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
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
>
> 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...
&
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
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
> 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)
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:
>
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
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
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
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
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
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 <-
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
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
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
>
>
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
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
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.
>
28 matches
Mail list logo