Skip to content
Prev 362252 / 398498 Next

R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset

Hi Jeff, ?likes its Excel?, I don?t follow. Pardon me for any mix up.

Thanks for the code.  After running it, this is the error I get.

Error: cannot join on columns 'admin_period' x 'admin_period1': index out of bounds

Regards
-------------------------------------------------------------------------------
Kevin Wame | Ph.D. Student (IDeAL)
KEMRI-Wellcome Trust Collaborative Research Programme
Centre for Geographic Medicine Research
P.O. Box 230-80108, Kilifi, Kenya
On 7/3/16, 9:34 PM, "Jeff Newmiller" <jdnewmil at dcn.davis.ca.us> wrote:
I still get the impression from your mixing of information types that you are thinking like this is Excel.

Perhaps something like

drug_study$admin_period  <- ave( "Y" == drug_study$drug_admin, drug_study$ID, FUN=cumsum )
library(dplyr)
result0 <- (   drug_study
          %>% filter( 0 != admin_period )
          %>% group_by( ID, admin_period )
          %>% summarise( start = min( date ) )
          %>% mutate( admin_period1 = admin_period -1 )
          )
result <- (   result0 
          %>% select( -admin_period )
          %>% inner_join( result0 %>% select( ID, admin_period1, end=start )
                       , by = c( ID="ID", admin_period ="admin_period1" )
                        )
          %>% mutate( ddays = end - start )
          )

Thread (20 messages)

Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 2 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 2 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 2 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 2 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 John Kane R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Bert Gunter R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Jeff Newmiller R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Bert Gunter R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3 Kevin Wamae R - Populate Another Variable Based on Multiple Conditions | For a Large Dataset Jul 3