Skip to content
Prev 397143 / 398498 Next

R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments

On 11/27/24 08:30, Sorkin, John wrote:
There's an R base function named, wait for it, ... `by`

It returns a list? that is the results of a function applied to the 
sub-dataframes indexed by whatever grouping variable you specify in the 
second argument. My memory told me that it needed to be presented as a 
list which was why I chose to use the `[` function rather than `$` or `[[`

by(olddata, olddata["ID"], FUN= function(x) { rep( x$ID[1], 
times=nrow(x) )}) #------------------- ID: 1 [1] 1 1 1 1 1 1 1 1 1 1 
------------------------------------------------------------------------------------ 
ID: 2 [1] 2 2 2 2 2 2 
------------------------------------------------------------------------------------ 
ID: 3 [1] 3 3 So all you need to do from there is unlist it and assign 
to the new named column #------------------ olddata$FirstDay <- unlist( 
by(olddata, olddata["ID"], FUN= function(x) { rep( x$ID[1], 
times=nrow(x) )}) ) olddata #---------------------------- ID date 
FirstDay 1 1 1 1 2 1 1 1 3 1 2 1 4 1 2 1 5 1 3 1 6 1 3 1 7 1 4 1 8 1 4 1 
9 1 5 1 10 1 5 1 11 2 5 2 12 2 5 2 13 2 5 2 14 2 6 2 15 2 6 2 16 2 6 2 
17 3 10 3 18 3 10 3

HTH

David.

Thread (15 messages)

Sorkin, John R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Tom Woolman R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Tom Woolman R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Olivier Crouzet R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Richard M. Heiberger R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Ebert,Timothy Aaron R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 David Winsemius R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 David Winsemius R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 David Winsemius R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Ebert,Timothy Aaron R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 @vi@e@gross m@iii@g oii gm@ii@com R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Rui Barradas R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Jeff Newmiller R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Bert Gunter R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27 Naresh Gurbuxani R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments Nov 27