Quantcast

creating a count variable in R

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

creating a count variable in R

JonC
Hi R helpers,

I'm trying to create a count in R , but as there is no retain function like in SAS I'm running into difficulties.

I have the following :

Date_var                           and wish to obtain      Date_var          Count_var
01/01/2011                                                       01/01/2011      1
01/01/2011                                                       01/01/2011      2
02/01/2011                                                       02/01/2011      1
02/01/2011                                                       02/01/2011      2
02/01/2011                                                       02/01/2011      3
02/01/2011                                                       02/01/2011      4
03/01/2011                                                       03/01/2011      1
03/01/2011                                                       03/01/2011      2
03/01/2011                                                       03/01/2011      3
03/01/2011                                                       03/01/2011      4
03/01/2011                                                       03/01/2011      5
03/01/2011                                                       03/01/2011      6
03/01/2011                                                       03/01/2011      7

As can be seen above the count var is re initialised every time a new date is found. I hope this is easy.

Many thanks in advance for assistance. It is appreciated.

Cheers

Jon
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: creating a count variable in R

William Dunlap
Use cumsum() to count the change points:

> Date_var <- as.Date(rep(c("2011-02-04","2011-02-07","2011-01-29"),
c(2,3,1)))
> data.frame(Date_var, count=cumsum(c(TRUE,
Date_var[-1]!=Date_var[-length(Date_var)])))
    Date_var count
1 2011-02-04     1
2 2011-02-04     1
3 2011-02-07     2
4 2011-02-07     2
5 2011-02-07     2
6 2011-01-29     3

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of JonC
> Sent: Thursday, March 03, 2011 1:58 PM
> To: [hidden email]
> Subject: [R] creating a count variable in R
>
> Hi R helpers,
>
> I'm trying to create a count in R , but as there is no retain
> function like
> in SAS I'm running into difficulties.
>
> I have the following :
>
> Date_var                           and wish to obtain      
> Date_var        
> Count_var
> 01/01/2011                                                    
>    01/01/2011    
> 1
> 01/01/2011                                                    
>    01/01/2011    
> 2
> 02/01/2011                                                    
>    02/01/2011    
> 1
> 02/01/2011                                                    
>    02/01/2011    
> 2
> 02/01/2011                                                    
>    02/01/2011    
> 3
> 02/01/2011                                                    
>    02/01/2011    
> 4
> 03/01/2011                                                    
>    03/01/2011    
> 1
> 03/01/2011                                                    
>    03/01/2011    
> 2
> 03/01/2011                                                    
>    03/01/2011    
> 3
> 03/01/2011                                                    
>    03/01/2011    
> 4
> 03/01/2011                                                    
>    03/01/2011    
> 5
> 03/01/2011                                                    
>    03/01/2011    
> 6
> 03/01/2011                                                    
>    03/01/2011    
> 7
>
> As can be seen above the count var is re initialised every
> time a new date
> is found. I hope this is easy.
>
> Many thanks in advance for assistance. It is appreciated.
>
> Cheers
>
> Jon
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/creating-a-count-variable-in-R-t
p3334288p3334288.html
> Sent from the R help mailing list archive at Nabble.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.
>

______________________________________________
[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
|  
Report Content as Inappropriate

Re: creating a count variable in R

David Winsemius
In reply to this post by JonC

On Mar 3, 2011, at 3:58 PM, JonC wrote:

> Hi R helpers,
>
> I'm trying to create a count in R , but as there is no retain  
> function like
> in SAS I'm running into difficulties.

Your data is not cut-pastable as presented but this should work:

 > dfrm$count_var <- ave(as.numeric(dfrm$Date_var), dfrm$Date_var,  
FUN=seq_along)
 > dfrm
      Date_var count_var
1  01/01/2011         1
2  01/01/2011         2
3  02/01/2011         1
4  02/01/2011         2
5  02/01/2011         3
6  02/01/2011         4
7  03/01/2011         1
8  03/01/2011         2
9  03/01/2011         3
10 03/01/2011         4
11 03/01/2011         5
12 03/01/2011         6
13 03/01/2011         7


>
> I have the following :
>
> Date_var                           and wish to obtain      Date_var
> Count_var
> 01/01/2011                                                        
> 01/01/2011
> 1
> 01/01/2011                                                        
> 01/01/2011
> 2
> 02/01/2011                                                        
> 02/01/2011
> 1
> 02/01/2011                                                        
> 02/01/2011
> 2
> 02/01/2011                                                        
> 02/01/2011
> 3
> 02/01/2011                                                        
> 02/01/2011
> 4
> 03/01/2011                                                        
> 03/01/2011
> 1
> 03/01/2011                                                        
> 03/01/2011
> 2
> 03/01/2011                                                        
> 03/01/2011
> 3
> 03/01/2011                                                        
> 03/01/2011
> 4
> 03/01/2011                                                        
> 03/01/2011
> 5
> 03/01/2011                                                        
> 03/01/2011
> 6
> 03/01/2011                                                        
> 03/01/2011
> 7
>
> As can be seen above the count var is re initialised every time a  
> new date
> is found. I hope this is easy.
>
> Many thanks in advance for assistance. It is appreciated.
>
> Cheers
>
> Jon
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/creating-a-count-variable-in-R-tp3334288p3334288.html
> Sent from the R help mailing list archive at Nabble.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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[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
|  
Report Content as Inappropriate

Re: creating a count variable in R

Bill.Venables
In reply to this post by JonC
You can probably simplify this if you can assume that the dates are in sorted order.  Here is a way of doing it even if the days are in arbitrary order.  The count refers to the number of times that this date has appeared so far in the sequence.

con <- textConnection("
01/01/2011
01/01/2011
02/01/2011
02/01/2011
02/01/2011
02/01/2011
03/01/2011
03/01/2011
03/01/2011
03/01/2011
03/01/2011
03/01/2011
03/01/2011
")
days <- scan(con, what = "")
close(con)
X <- model.matrix(~days-1)
XX <- apply(X, 2, cumsum)
dat <- data.frame(days = days, count = rowSums(X*XX))
dat

###
this uses days as a character string vector.  If they are actual dates, then convert them to character strings for this operation.

Bill Venables.

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of JonC
Sent: Friday, 4 March 2011 7:58 AM
To: [hidden email]
Subject: [R] creating a count variable in R

Hi R helpers,

I'm trying to create a count in R , but as there is no retain function like
in SAS I'm running into difficulties.

I have the following :

Date_var                           and wish to obtain      Date_var        
Count_var
01/01/2011                                                       01/01/2011    
1
01/01/2011                                                       01/01/2011    
2
02/01/2011                                                       02/01/2011    
1
02/01/2011                                                       02/01/2011    
2
02/01/2011                                                       02/01/2011    
3
02/01/2011                                                       02/01/2011    
4
03/01/2011                                                       03/01/2011    
1
03/01/2011                                                       03/01/2011    
2
03/01/2011                                                       03/01/2011    
3
03/01/2011                                                       03/01/2011    
4
03/01/2011                                                       03/01/2011    
5
03/01/2011                                                       03/01/2011    
6
03/01/2011                                                       03/01/2011    
7

As can be seen above the count var is re initialised every time a new date
is found. I hope this is easy.

Many thanks in advance for assistance. It is appreciated.

Cheers

Jon


--
View this message in context: http://r.789695.n4.nabble.com/creating-a-count-variable-in-R-tp3334288p3334288.html
Sent from the R help mailing list archive at Nabble.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.

______________________________________________
[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.
Loading...