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

2024-11-27 Thread Bert Gunter
The grouping solutions offered seem to be the obvious way to do this and may even be more efficient in R then what follows below. However, note that they are to some extent doing unnecessary work, since the ordering in the data frame already implicitly provides the grouping, and the hashing or wha

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

2024-11-27 Thread Naresh Gurbuxani
In addition to many good solutions already provided, this solution uses data.table package. library(data.table) mydf <- data.frame(id = c(rep(1,10),rep(2,6),rep(3,2)), date = c(rep(1,2),rep(2,2),rep(3,2),rep(4,2),rep(5,2), rep(5,3),rep(6,3),rep(10,2))) setDT(mydf) mydf[, `:=`(firstdate = with(.S

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

2024-11-27 Thread Ebert,Timothy Aaron
Very similar to what Oliver posted: library(dplyr) newdata <- olddata |> group_by(ID) |> mutate(firstdate = first(date)) newdata 1) I attached dplyr to the entire program. Oliver used dplyr::group_by() and dplyr::mutate() to do the same thing. 2) I used the base R |> pipe while Oliver used

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

2024-11-27 Thread David Winsemius via R-help
On 11/27/24 08:30, Sorkin, John wrote: > I am an old, long time SAS programmer. I need to produce R code that > processes a dataframe in a manner that is equivalent to that produced by > using a by statement in SAS and an if first.day statement and a retain > statement: > > I want to take data

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

2024-11-27 Thread Tom Woolman
Oh and don't forget: #first line of code, bring dplyr into memory after that package has been installed. library(dplyr) On Wednesday, November 27th, 2024 at 12:05 PM, Tom Woolman wrote: > > > Check out the dplyr package, specifically the mutate function. > > # Create new column based o

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

2024-11-27 Thread Olivier Crouzet
Dear John, Considering that you've got the following dataframe: ID <- c(rep(1,10),rep(2,6),rep(3,2)) date <- c(rep(1,2),rep(2,2),rep(3,2),rep(4,2),rep(5,2), rep(5,3),rep(6,3),rep(10,2)) df <- data.frame(ID, date) I would suggest to go this way: newdf <- df %>% dplyr::group_by(ID) %>%

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

2024-11-27 Thread Tom Woolman
Check out the dplyr package, specifically the mutate function. # Create new column based on existing column value df <- df %>% mutate(FirstDay = if(ID = 2, 5)) df Repeat as needed to capture all of the day/firstday combinations you want to account for. Like everything else in R, there are

Re: [R] [External] R Processing dataframe by group - equivalent to SAS by group processing with a first. and retain statments

2024-11-27 Thread Richard M. Heiberger
I would use base R. newdata <- cbind(olddata, FirstDay=olddata$date) newdata$FirstDay <- with(newdata, { for (thisID in unique(ID)) FirstDay[ID==thisID] <- FirstDay[ID==thisID][1] FirstDay} ) newdata note that both my solution and Olivier have newdata$FirstDay[17:18] == 10 which is wha

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

2024-11-27 Thread Jeff Newmiller via R-help
Was wondering when this would be suggested. But the question was about getting the final dataframe... newdta <- olddta newdta$FirstDay <- ave(newdata$date, newdata$ID, FUN = \(x) x[1L]) On November 27, 2024 11:13:49 AM PST, Rui Barradas wrote: >Às 16:30 de 27/11/2024, Sorkin, John escreveu: >>

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

2024-11-27 Thread Sorkin, John
I am an old, long time SAS programmer. I need to produce R code that processes a dataframe in a manner that is equivalent to that produced by using a by statement in SAS and an if first.day statement and a retain statement: I want to take data (olddata) that looks like this ID Day 1 1

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

2024-11-27 Thread Rui Barradas
Às 16:30 de 27/11/2024, Sorkin, John escreveu: I am an old, long time SAS programmer. I need to produce R code that processes a dataframe in a manner that is equivalent to that produced by using a by statement in SAS and an if first.day statement and a retain statement: I want to take data (ol

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

2024-11-27 Thread David Winsemius via R-help
On 11/27/24 08:30, Sorkin, John wrote: > I am an old, long time SAS programmer. I need to produce R code that > processes a dataframe in a manner that is equivalent to that produced by > using a by statement in SAS and an if first.day statement and a retain > statement: > > I want to take data

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

2024-11-27 Thread Ebert,Timothy Aaron
Here is another version using for loops. newdata3 <- olddata |> dplyr::arrange(ID, date) newdata3$firstday <- NA for (i in 1:nrow(newdata3)) { if (i == 1) { dayz <- newdata3$date[i] } else { if (newdata3$ID[i] != newdata3$ID[i - 1]) { dayz <- newdata3$date[i] } } newdat

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

2024-11-27 Thread David Winsemius via R-help
On 11/27/24 09:44, David Winsemius via R-help wrote: On 11/27/24 08:30, Sorkin, John wrote: I am an old, long time SAS programmer. I need to produce R code that processes a dataframe in a manner that is equivalent to that produced by using a by statement in SAS and an if first.day statement

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

2024-11-27 Thread avi.e.gross
John, If I understood you, you want to take the minimum value of Day for each grouping by ID and add a new column to contain that. Right? There are likely many ways to do this in base R, but I prefer the dplyr/tidyverse package in which you can use group_by(ID) piped to mutate(FirstDay = min(Day