[R] R Error: wrong result size (...), expected ... or 1”
Muhuri, Pradip (SAMHSA/CBHSQ)
Pradip.Muhuri at samhsa.hhs.gov
Tue Apr 28 15:48:33 CEST 2015
Hello,
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 (816841), expected 52939 or 1.
myFunc involves a data frame (named xanloid_set), which has 816841 rows. R is correct to say that I was expecting only 52939 rows because of the filter() function.
These results are from the following code outside myFunc:
addmargins(table(xanloid_set$cohort_type))
NMPR_Cohort OID_Cohort Others Sum
52939 158192 605710 816841
How would I resolve the issue: error message from the muFunc? Any hints would be appreciated.
Thanks,
Pradip Muhuri
#count_nmpr_oid_nmproid_by_year.R
setwd ("H:/R/cis_data")
library(dplyr)
library(knitr)
rm(list = ls())
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)))
}
load("xanloid_set.rdata")
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
)
Pradip K. Muhuri, PhD
SAMHSA/CBHSQ
1 Choke Cherry Road, Room 2-1071
Rockville, MD 20857
Tel: 240-276-1070
Fax: 240-276-1260
[[alternative HTML version deleted]]
More information about the R-help
mailing list