[R] self-written function
Christoph Scherber
Christoph.Scherber at uni-jena.de
Fri Jan 28 11:53:06 CET 2005
Dear Jari,
I had included the test for NA´s just to be sure the function can handle
datasets with missing values.
Here´s an updated version without this specification:
backsin<-function(x,y){
backtransf<-list()
back<-((sin(x))^2)*100
backtransf$mback<-tapply(back,y,mean)
backtransf$sdback<-tapply(back,y,sd)/sqrt(length(y))
backtransf
}
Jari Oksanen wrote:
>Christoph,
>
>I guess your "NA" refers to Not Available (missing, NA). In that case,
>you should check it with is.na() instead of != "NA". See this:
>
>
>
>>x <- c(3,4,NA,5)
>>x != "NA"
>>
>>
>[1] TRUE TRUE NA TRUE
>
>
>>!is.na(x)
>>
>>
>[1] TRUE TRUE FALSE TRUE
>
>No idea if this helps, but it was a problem with the code anyway.
>
>cheers, jari oksanen
>On Fri, 2005-01-28 at 11:04 +0100, Christoph Scherber wrote:
>
>
>>Hi!
>>
>>OK, here are some more details on the function: My dataframe consists of
>>several columns of categorical variables (let´s call them A,B,C) plus a
>>column with a response variable y (which is arcsine-square root
>>transformed proportions)
>>
>>I am now trying to write a function that automatically gives me the
>>back-transformed mean values + standard errors for y for each column in
>>the dataframe. Ideally, this would be something like
>>
>>tapply(y,list(A,B,C,D),backtransformed.mean)
>>
>>Here is the correct version of the function:
>>
>>backsin<-function(x,y){
>>backtransf<-list()
>>
>>back<-((sin(x[x!="NA"]))^2)*100
>>backtransf$mback<-tapply(back,y[x!="NA"],mean)
>>backtransf$sdback<-tapply(back,y[x!="NA"],std)/sqrt(length(y[x!="NA"]))
>>backtransf
>>}
>>
>>
>>Regards,
>>Christoph
>>
>>
>>Petr Pikal wrote:
>>
>>
>>
>>>Hi
>>>
>>>I am not sure if anybody gave you a reply, but do you think you gave
>>>enough detail about your data, what you expect, what you did, what
>>>was the response and how it differ from expected output and best of
>>>all ***working*** example?
>>>
>>>BTW, what is stdev?
>>>
>>>If you wanted to compute standard deviation sd is enough.
>>>
>>>Cheers
>>>Petr
>>>
>>>On 27 Jan 2005 at 12:20, Christoph Scherber wrote:
>>>
>>>
>>>
>>>
>>>
>>>>Dear all,
>>>>
>>>>I´ve got a simple self-written function to calculate the mean + s.e.
>>>>
>>>>
>>>>from arcsine-transformed data:
>>>
>>>
>>>>backsin<-function(x,y,...){
>>>>backtransf<-list()
>>>>backtransf$back<-((sin(x[x!="NA"]))^2)*100
>>>>backtransf$mback<-tapply(backtransf$back,y[x!="NA"],mean)
>>>>backtransf$sdback<-tapply(backtransf$back,y[x!="NA"],std)/sqrt(length(y[x!="NA"]))
>>>>
>>>>
>>>>
>>>>backtransf
>>>>
>>>>
>>>>}
>>>>
>>>>I would like to apply this function to whole datasets, such as
>>>>
>>>>tapply(variable,list(A,B,C,D),backsin)
>>>>
>>>>Of course, this doesn´t work with the way in which the backsin()
>>>>function is specified.
>>>>
>>>>Does anyone have suggestions on how I could improve my function?
>>>>
>>>>Regards,
>>>>Christoph
>>>>
>>>>______________________________________________
>>>>R-help at stat.math.ethz.ch mailing list
>>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide!
>>>>http://www.R-project.org/posting-guide.html
>>>>
>>>>
>>>>
>>>>
>>>Petr Pikal
>>>petr.pikal at precheza.cz
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>>
>
>
>
>
More information about the R-help
mailing list