using factor variable in the "DO" function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

using factor variable in the "DO" function

Veerappa Chetty
Hi,

The following call does not work:

dfhosp=dat.2.wide.sub %>% group_by(HOSP_NRD)%>%
  do(fitHosp=lm(log(y)~ log(x)+I(log(x)^2)+NCHRONIC+AGE+sex ,data=.))

Error in `$<-.data.frame`(`*tmp*`, "sex", value = integer(0)) :
  replacement has 0 rows, data has 25174

When I use "sex" as a binary variable with values "0" and "1", it works.

Is there a way to use factor in this call?
Thanks.

V.K.Chetty

--
Professor of Family Medicine
Boston University
Tel: 617-414-6221, Fax:617-414-3345
emails: [hidden email],[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: using factor variable in the "DO" function

Jeff Newmiller
You have competing non-standard evaluation tools in play. I would decouple them with a function (untested):

my fit <- function( DF ) {
    data.frame( HOSP_NRD = .$HOSP_NRD[1]
        , fitHosp = lm(log(y)~ log(x)+I(log(x)^2)+NCHRONIC+AGE+sex
      , data=DF)
      )
}
dfhosp <- ( dat.2.wide.sub
  %>% group_by(HOSP_NRD)
  %>% do( my_fit( . ) )
--
Sent from my phone. Please excuse my brevity.

On March 7, 2016 10:48:28 AM PST, Veerappa Chetty <[hidden email]> wrote:

>Hi,
>
>The following call does not work:
>
>dfhosp=dat.2.wide.sub %>% group_by(HOSP_NRD)%>%
>  do(fitHosp=lm(log(y)~ log(x)+I(log(x)^2)+NCHRONIC+AGE+sex ,data=.))
>
>Error in `$<-.data.frame`(`*tmp*`, "sex", value = integer(0)) :
>  replacement has 0 rows, data has 25174
>
>When I use "sex" as a binary variable with values "0" and "1", it
>works.
>
>Is there a way to use factor in this call?
>Thanks.
>
>V.K.Chetty
>
>--
>Professor of Family Medicine
>Boston University
>Tel: 617-414-6221, Fax:617-414-3345
>emails: [hidden email],[hidden email]
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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 -- To UNSUBSCRIBE and more, see
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.