[R] R Error: wrong result size (...), expected ... or 1 (minimal example provided)

William Dunlap wdunlap at tibco.com
Sat May 2 18:41:47 CEST 2015


  # invoke user defined function
  myFunc (  newdata=nmpr_nmproid,
          oridata=xanloid_set,
          cohort=xanloid_set$cohort_type,
          value= "NMPR_Cohort",
          xdate_to_int_time=anl_to_int_time,
          xflag=xanloid_set$anlflag,
          idate=xanloid_set$intdate,
          xdate=xanloid_set$anldate,
          xdate_to_int_time_cat=xanloid_set$anl_to_int_time_cat,
          year=xanloid_set$xyear
          )

Note that some of your arguments are NULL because xanloid_set
does not contain components by the given name.  E.g., xanloid_set$anlflag
is NULL because there is no column called 'anlflag' in xanloid_set.
The NULL's will cause various problems downstream.

You can avoid this problem by using the syntax
   xanloid_set[ , "anlflag"]
which will give an error if the requested column does not exist.



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Fri, May 1, 2015 at 9:13 PM, Muhuri, Pradip (SAMHSA/CBHSQ) <
Pradip.Muhuri at samhsa.hhs.gov> wrote:

> Hello,
>
> I am reposting my question with a reproducible example/minimal dataset (6
> rows) this time.
>
> I have written a user-defined function (myFunc below) with ten arguments.
> When calling the function, I get the following message: “Error: wrong
> result size (0), expected 2 or 1”.
> I am not getting the desired output dataset that will have 2 rows. How
> would I resolve the issue?   Any hints would be appreciated.
>
>
> These results are from the following code chunk  outside myFunc:
>
> addmargins(table(xanloid_set$cohort_type))
>
>
>
> NMPR_Cohort  OID_Cohort       Other         Sum
>           2           1           3           6
>
> .
>
> Thanks,
>
> Pradip Muhuri
>
>
>
>
>
> # myFunc_rev.R
> setwd ("H:/R/cis_data")
> library(dplyr)
> rm(list = ls())
> # data object - description
> temp <- "id  intdate anldate oiddate herdate cohort_type
> 1     2004-11-04 2002-07-18 2001-07-07 2003-11-03  NMPR_Cohort
> 2     2004-10-24         NA 2002-10-13 NA          OID_Cohort
> 3     2004-10-10         NA         NA NA          Other
> 4     2004-09-01 1999-08-10         NA 2002-11-04  NMPR_Cohort
> 5     2004-09-04 1997-10-05         NA NA          Other
> 6     2004-10-25         NA         NA 2011-11-04  Other"
> # read the data object
> xanloid_set <- read.table(textConnection(temp),
>                            colClasses=c("character", "Date", "Date",
> "Date", "Date", "character"),
>                            header=TRUE, as.is=TRUE
> )
> # print the data object
> xanloid_set
> # Define user-defined function
> myFunc <- function (newdata,
>                     oridata,
>                     cohort,
>                     value,
>                     xdate_to_int_time,
>                     xflag,
>                     idate,
>                     xdate,
>                     xdate_to_int_time_cat,
>                     year) {
>
>                     newdata  <-    filter (oridata, cohort== value ) %>%
>                                    mutate(xdate_to_int_time =
> ifelse(xflag==1, (idate-xdate)/365.25, NA),
>                                    xdate_to_int_time_cat =
> cut(xdate_to_int_time, breaks=c(0,1,2,3,4,5,6,7),
>
>  include.lowest=TRUE, stringsAsFactors = FALSE) )
>                     addmargins(with(newdata, table(year,
> xdate_to_int_time_cat, na.rm=TRUE)))
>                                             }
> # invoke user defined function
> myFunc (  newdata=nmpr_nmproid,
>         oridata=xanloid_set,
>         cohort=xanloid_set$cohort_type,
>         value= "NMPR_Cohort",
>         xdate_to_int_time=anl_to_int_time,
>         xflag=xanloid_set$anlflag,
>         idate=xanloid_set$intdate,
>         xdate=xanloid_set$anldate,
>         xdate_to_int_time_cat=xanloid_set$anl_to_int_time_cat,
>         year=xanloid_set$xyear
>         )
> # tabulate cohort_type
>   addmargins(table(xanloid_set$cohort_type))
>
>
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org 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]]



More information about the R-help mailing list