creating table with sequences of numbers based on the table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

creating table with sequences of numbers based on the table

Goszkas
This post has NOT been accepted by the mailing list yet.
I have a problem with transfering one table to another automatically. From table like this:

> tab
  pop Freq
1       1   30
2       2   25
3       3   30
4       4   30
5       5   30
6       6   30
7       7   30

I want to use number of individuals (freq) and then in next table just list them with following numbers (depending on total number of individuals)
Like this:
in
pop        ind
 
1              1
1              2
1              3
1              4
.               .
.               .
1              30
2              1
2              2
2              3
2              4
.               .
2              25
3              1
3              2
.               .
.               .

How can i do it? I think i have to use loops but so far I failed.
Thank you in advance,
Best,
Malgorzata Gazda
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

arun kirshna
Hi,
Try:
Either

tab <- read.table(text="pop Freq
1       1   30
2       2   25
3       3   30
4       4   30
5       5   30
6       6   30
7       7   30",sep="",header=TRUE)

indx <- rep(1:nrow(tab),tab$Freq)
tab1 <- transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along))[,-2]
#or
tab2 <-  transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]
identical(tab1,tab2)
#[1] TRUE
#or
tab3 <- transform(tab[indx,], ind= with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-length(Freq)])),Freq)))[,-2]
identical(tab1,tab3)
#[1] TRUE

A.K.


I have a problem with transfering one table to another automatically. From table like this:

> tab
  pop Freq
1       1   30
2       2   25
3       3   30
4       4   30
5       5   30
6       6   30
7       7   30

I want to use number of individuals (freq) and then in next
table just list them with following numbers (depending on total number
of individuals)
Like this:
in
pop        ind
 
1              1
1              2
1              3
1              4
.               .
.               .
1              30
2              1
2              2
2              3
2              4
.               .
2              25
3              1
3              2
.               .
.               .

How can i do it? I think i have to use loops but so far I failed.
Thank you in advance,
Best,
Malgorzata Gazda

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

djmuseR
Less coding with plyr:

tab <- read.table(text="pop Freq
1       1   30
2       2   25
3       3   30
4       4   30
5       5   30
6       6   30
7       7   30",sep="",header=TRUE)

# Function to do the work on each row
f <- function(pop, Freq) data.frame(ind = seq_len(Freq))

library(plyr)
u <- mdply(tab, f)[, -2]

Dennis

On Thu, Mar 13, 2014 at 8:01 AM, arun <[hidden email]> wrote:

> Hi,
> Try:
> Either
>
> tab <- read.table(text="pop Freq
> 1       1   30
> 2       2   25
> 3       3   30
> 4       4   30
> 5       5   30
> 6       6   30
> 7       7   30",sep="",header=TRUE)
>
> indx <- rep(1:nrow(tab),tab$Freq)
> tab1 <- transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along))[,-2]
> #or
> tab2 <-  transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]
> identical(tab1,tab2)
> #[1] TRUE
> #or
> tab3 <- transform(tab[indx,], ind= with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-length(Freq)])),Freq)))[,-2]
> identical(tab1,tab3)
> #[1] TRUE
>
> A.K.
>
>
> I have a problem with transfering one table to another automatically. From table like this:
>
>> tab
>   pop Freq
> 1       1   30
> 2       2   25
> 3       3   30
> 4       4   30
> 5       5   30
> 6       6   30
> 7       7   30
>
> I want to use number of individuals (freq) and then in next
> table just list them with following numbers (depending on total number
> of individuals)
> Like this:
> in
> pop        ind
>
> 1              1
> 1              2
> 1              3
> 1              4
> .               .
> .               .
> 1              30
> 2              1
> 2              2
> 2              3
> 2              4
> .               .
> 2              25
> 3              1
> 3              2
> .               .
> .               .
>
> How can i do it? I think i have to use loops but so far I failed.
> Thank you in advance,
> Best,
> Malgorzata Gazda
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

Arunkumar Srinivasan
I think this'll be way simpler and also faster:

ans <- data.frame(pop = rep.int(tab$pop, tab$Freq), ind=sequence(tab$Freq))

Arun

From: Dennis Murphy [hidden email]
Reply: Dennis Murphy [hidden email]
Date: March 13, 2014 at 9:57:20 PM
To: arun [hidden email]
Cc: R help [hidden email]
Subject:  Re: [R] creating table with sequences of numbers based on the table  

Less coding with plyr:  

tab <- read.table(text="pop Freq  
1 1 30  
2 2 25  
3 3 30  
4 4 30  
5 5 30  
6 6 30  
7 7 30",sep="",header=TRUE)  

# Function to do the work on each row  
f <- function(pop, Freq) data.frame(ind = seq_len(Freq))  

library(plyr)  
u <- mdply(tab, f)[, -2]  

Dennis  

On Thu, Mar 13, 2014 at 8:01 AM, arun <[hidden email]> wrote:  

> Hi,  
> Try:  
> Either  
>  
> tab <- read.table(text="pop Freq  
> 1 1 30  
> 2 2 25  
> 3 3 30  
> 4 4 30  
> 5 5 30  
> 6 6 30  
> 7 7 30",sep="",header=TRUE)  
>  
> indx <- rep(1:nrow(tab),tab$Freq)  
> tab1 <- transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along))[,-2]  
> #or  
> tab2 <- transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]  
> identical(tab1,tab2)  
> #[1] TRUE  
> #or  
> tab3 <- transform(tab[indx,], ind= with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-length(Freq)])),Freq)))[,-2]  
> identical(tab1,tab3)  
> #[1] TRUE  
>  
> A.K.  
>  
>  
> I have a problem with transfering one table to another automatically. From table like this:  
>  
>> tab  
> pop Freq  
> 1 1 30  
> 2 2 25  
> 3 3 30  
> 4 4 30  
> 5 5 30  
> 6 6 30  
> 7 7 30  
>  
> I want to use number of individuals (freq) and then in next  
> table just list them with following numbers (depending on total number  
> of individuals)  
> Like this:  
> in  
> pop ind  
>  
> 1 1  
> 1 2  
> 1 3  
> 1 4  
> . .  
> . .  
> 1 30  
> 2 1  
> 2 2  
> 2 3  
> 2 4  
> . .  
> 2 25  
> 3 1  
> 3 2  
> . .  
> . .  
>  
> How can i do it? I think i have to use loops but so far I failed.  
> Thank you in advance,  
> Best,  
> Malgorzata Gazda  
>  
> ______________________________________________  
> [hidden email] 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.  
______________________________________________  
[hidden email] 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.  

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

William Dunlap
In reply to this post by djmuseR
Or use sequence() and rep(), as in
  u1 <- with(tab, data.frame(pop=rep(pop,Freq), ind=sequence(Freq)))
I think that u1 is the same as your u.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Dennis Murphy
> Sent: Thursday, March 13, 2014 1:55 PM
> To: arun
> Cc: R help
> Subject: Re: [R] creating table with sequences of numbers based on the table
>
> Less coding with plyr:
>
> tab <- read.table(text="pop Freq
> 1       1   30
> 2       2   25
> 3       3   30
> 4       4   30
> 5       5   30
> 6       6   30
> 7       7   30",sep="",header=TRUE)
>
> # Function to do the work on each row
> f <- function(pop, Freq) data.frame(ind = seq_len(Freq))
>
> library(plyr)
> u <- mdply(tab, f)[, -2]
>
> Dennis
>
> On Thu, Mar 13, 2014 at 8:01 AM, arun <[hidden email]> wrote:
> > Hi,
> > Try:
> > Either
> >
> > tab <- read.table(text="pop Freq
> > 1       1   30
> > 2       2   25
> > 3       3   30
> > 4       4   30
> > 5       5   30
> > 6       6   30
> > 7       7   30",sep="",header=TRUE)
> >
> > indx <- rep(1:nrow(tab),tab$Freq)
> > tab1 <- transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along))[,-2]
> > #or
> > tab2 <-  transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]
> > identical(tab1,tab2)
> > #[1] TRUE
> > #or
> > tab3 <- transform(tab[indx,], ind= with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-
> length(Freq)])),Freq)))[,-2]
> > identical(tab1,tab3)
> > #[1] TRUE
> >
> > A.K.
> >
> >
> > I have a problem with transfering one table to another automatically. From table like
> this:
> >
> >> tab
> >   pop Freq
> > 1       1   30
> > 2       2   25
> > 3       3   30
> > 4       4   30
> > 5       5   30
> > 6       6   30
> > 7       7   30
> >
> > I want to use number of individuals (freq) and then in next
> > table just list them with following numbers (depending on total number
> > of individuals)
> > Like this:
> > in
> > pop        ind
> >
> > 1              1
> > 1              2
> > 1              3
> > 1              4
> > .               .
> > .               .
> > 1              30
> > 2              1
> > 2              2
> > 2              3
> > 2              4
> > .               .
> > 2              25
> > 3              1
> > 3              2
> > .               .
> > .               .
> >
> > How can i do it? I think i have to use loops but so far I failed.
> > Thank you in advance,
> > Best,
> > Malgorzata Gazda
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

David Carlson
In reply to this post by djmuseR
I think we're down to counting the number of characters in each
solution! Arun's 3 two-line versus your two-line solution (not
counting loading plyr). How about three short lines?

pop <- rep(1:nrow(tab), tab$Freq)
ind <- unlist(sapply(tab$Freq, seq_len))
tab2 <- data.frame(pop, ind)

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Dennis Murphy
Sent: Thursday, March 13, 2014 3:55 PM
To: arun
Cc: R help
Subject: Re: [R] creating table with sequences of numbers based
on the table

Less coding with plyr:

tab <- read.table(text="pop Freq
1       1   30
2       2   25
3       3   30
4       4   30
5       5   30
6       6   30
7       7   30",sep="",header=TRUE)

# Function to do the work on each row
f <- function(pop, Freq) data.frame(ind = seq_len(Freq))

library(plyr)
u <- mdply(tab, f)[, -2]

Dennis

On Thu, Mar 13, 2014 at 8:01 AM, arun <[hidden email]>
wrote:

> Hi,
> Try:
> Either
>
> tab <- read.table(text="pop Freq
> 1       1   30
> 2       2   25
> 3       3   30
> 4       4   30
> 5       5   30
> 6       6   30
> 7       7   30",sep="",header=TRUE)
>
> indx <- rep(1:nrow(tab),tab$Freq)
> tab1 <-
transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along)
)[,-2]
> #or
> tab2 <-
transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]
> identical(tab1,tab2)
> #[1] TRUE
> #or
> tab3 <- transform(tab[indx,], ind=
with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-length(Freq)])
),Freq)))[,-2]
> identical(tab1,tab3)
> #[1] TRUE
>
> A.K.
>
>
> I have a problem with transfering one table to another
automatically. From table like this:

>
>> tab
>   pop Freq
> 1       1   30
> 2       2   25
> 3       3   30
> 4       4   30
> 5       5   30
> 6       6   30
> 7       7   30
>
> I want to use number of individuals (freq) and then in next
> table just list them with following numbers (depending on
total number

> of individuals)
> Like this:
> in
> pop        ind
>
> 1              1
> 1              2
> 1              3
> 1              4
> .               .
> .               .
> 1              30
> 2              1
> 2              2
> 2              3
> 2              4
> .               .
> 2              25
> 3              1
> 3              2
> .               .
> .               .
>
> How can i do it? I think i have to use loops but so far I
failed.
> Thank you in advance,
> Best,
> Malgorzata Gazda
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: creating table with sequences of numbers based on the table

Goszkas
In reply to this post by Arunkumar Srinivasan
Thank you so much for all your answers my problem was solved.
I really appreciate all your emails and really fast reaction.
All the best,
Malgorzata Gazda

Date sent: Thu, 13 Mar 2014 22:38:54 +0100
From: Arunkumar Srinivasan <[hidden email]>
To: arun <[hidden email]>, Dennis Murphy <[hidden email]>
Copies to: R help <[hidden email]>
Subject: Re: [R] creating table with sequences of numbers based on the table

I think this'll be way simpler and also faster:

ans <- data.frame(pop = rep.int(tab$pop, tab$Freq), ind=sequence(tab$Freq))

Arun

From: Dennis Murphy [hidden email]
Reply: Dennis Murphy [hidden email]
Date: March 13, 2014 at 9:57:20 PM
To: arun [hidden email]
Cc: R help [hidden email]
Subject:  Re: [R] creating table with sequences of numbers based on the table  

Less coding with plyr:  

tab <- read.table(text="pop Freq  
1 1 30  
2 2 25  
3 3 30  
4 4 30  
5 5 30  
6 6 30  
7 7 30",sep="",header=TRUE)  

# Function to do the work on each row  
f <- function(pop, Freq) data.frame(ind = seq_len(Freq))  

library(plyr)  
u <- mdply(tab, f)[, -2]  

Dennis  

On Thu, Mar 13, 2014 at 8:01 AM, arun <[hidden email]> wrote:  

> Hi,  
> Try:  
> Either  
>  
> tab <- read.table(text="pop Freq  
> 1 1 30  
> 2 2 25  
> 3 3 30  
> 4 4 30  
> 5 5 30  
> 6 6 30  
> 7 7 30",sep="",header=TRUE)  
>  
> indx <- rep(1:nrow(tab),tab$Freq)  
> tab1 <- transform(tab[indx,],ind=ave(seq_along(indx),indx,FUN=seq_along))[,-2]  
> #or  
> tab2 <- transform(tab[indx,],ind=unlist(sapply(tab$Freq,seq)))[,-2]  
> identical(tab1,tab2)  
> #[1] TRUE  
> #or  
> tab3 <- transform(tab[indx,], ind= with(tab,seq_len(sum(Freq))-rep(cumsum(c(0L,Freq[-length(Freq)])),Freq)))[,-2]  
> identical(tab1,tab3)  
> #[1] TRUE  
>  
> A.K.  
>  
>  
> I have a problem with transfering one table to another automatically. From table like this:  
>  
>> tab  
> pop Freq  
> 1 1 30  
> 2 2 25  
> 3 3 30  
> 4 4 30  
> 5 5 30  
> 6 6 30  
> 7 7 30  
>  
> I want to use number of individuals (freq) and then in next  
> table just list them with following numbers (depending on total number  
> of individuals)  
> Like this:  
> in  
> pop ind  
>  
> 1 1  
> 1 2  
> 1 3  
> 1 4  
> . .  
> . .  
> 1 30  
> 2 1  
> 2 2  
> 2 3  
> 2 4  
> . .  
> 2 25  
> 3 1  
> 3 2  
> . .  
> . .  
>  
> How can i do it? I think i have to use loops but so far I failed.  
> Thank you in advance,  
> Best,  
> Malgorzata Gazda  
>  
> ______________________________________________  
> [hidden email] 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.  

______________________________________________  
[hidden email] 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.  

        [[alternative HTML version deleted]]



Gosia Gazda
ul. Gardowskiego 20
30-864 Krakow


---
Ta wiadomość e-mail jest wolna od wirusów i złośliwego oprogramowania, ponieważ ochrona avast! Antivirus jest aktywna.
http://www.avast.com

______________________________________________
[hidden email] 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.