[R] error using by(goop[, c("a", "b", "c")], goop[, "Arm"], mySD) was Re: by gives no results, gives warning that data are non-numeric, but the data appears to be numeric.

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Mon Dec 28 23:10:45 CET 2015


Try the following.

mySD <- function(x) {sapply(x, function(y) sqrt(var(y)))}

Hope this helps,

Rui Barradas

Citando John Sorkin <jsorkin at grecc.umaryland.edu>:

> I am  trying to use the by function to get the SD of each column of  
> a data frame, stratified by ARM. Using a suggestion provided by both  
> William Dunlap and Rolf Turner, I have written the code below which  
> fails with the error:
> Error in match.fun(FUN) :   'sqrt(var(x))' is not a function,  
> character or symbol
> code:
> mySD <- function(x) {sapply(x,sqrt(var(x)))}
> class(mySD)
> by(goop[,c("a","b","c")],goop[,"Arm"],mySD)
> Below I include code which will create goop (thank you Bill for  
> suggesting I use dput), which will run will recreate mmy error. Any  
> thoughts on why the by function fails and how to fix it would be  
> appreciated.
> Thank you,
> John
> goop <- structure(c(180L, 153L, 149L, 218L, 217L, 147L, 141L, 141L, 174L,
>                    162L, 163L, 206L, 168L, 227L, 142L, 179L, 270L,  
> 206L, 243L, 134L,
>                    131L, 222L, 142L, 119L, 149L, 190L, 196L, 145L,  
> 185L, 235L, 165L,
>                    246L, 152L, 186L, 222L, 248L, 125L, 149L, 233L,  
> 145L, 154L, 231L,
>                    146L, 161L, 150L, 133L, 115L, 145L, 180L, 202L,  
> 158L, 210L, 153L,
>                    136L, 265L, 255L, 199L, 131L, 110L, 169L, 147L,  
> 133L, 108L, 203L,
>                    185L, 153L, 154L, 207L, 167L, 233L, 135L, 127L,  
> 197L, 245L, 124L,
>                    125L, 146L, 139L, 158L, 211L, 183L, 169L, 137L,  
> 114L, 176L, 143L,
>                    156L, 209L, 163L, 194L, 151L, 238L, 253L, 293L,  
> 208L, 147L, 186L,
>                    179L, 150L, 203L, 112L, 185L, 156L, 127L, 132L,  
> 243L, 177L, 242L,
>                    144L, 140L, 194L, 249L, 109L, 91L, 10163L,  
> 10987L, 19882L, 19899L,
>                    20104L, 20105L, 20167L, 20318L, 20338L, 20392L,  
> 20453L, 20454L,
>                    20467L, 20500L, 20682L, 20687L, 20692L, 20744L,  
> 20750L, 20751L,
>                    20806L, 20848L, 20856L, 20959L, 21023L, 21048L,  
> 21114L, 21134L,
>                    21135L, 21320L, 21329L, 21349L, 21353L, 21521L,  
> 21663L, 21784L,
>                    22031L, 22081L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,  
> 1L, 1L, 2L, 2L,
>                    2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,  
> 1L, 1L, 2L, 1L,
>                    2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Dim =  
> c(38L, 5L), .Dimnames = list(
>                      NULL, c("a", "b", "c", "ID", "Arm")), na.action  
> = structure(37, class = "omit"))
> goop
> mySD <- function(x) {sapply(x,sqrt(var(x)))}
> class(mySD)
> by(goop[,c("a","b","c")],goop[,"Arm"],mySD)
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology  
> and Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:18}}

More information about the R-help mailing list