Applying a function to a subset

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

Applying a function to a subset

gaiarrido
Here´s my problem,
i have developed the function kurtosis (using R-book as a guide) with this commands:
kurtosis<-function(x) {
m4<-sum((x-mean(x))^4)/length(x)
s4<-var(x)^2
m4/s4 - 3 }

Then create the object fem, which is the difference between the count of a trait in the left side of the body minus the right side. It´s a numeric variable. But i´ve got no data for some individuals, so in the variable "fem" i got numbers and smoe "NA".
When i try to apply kurtosis to "fem", the NA, don´t allow me to do that, that`s what i get:

> kurtosis(fem)
[1] NA

I tried with simple tricks, but i´ve got no results:
> kurtosis(fem,na.rm=T)
Error en kurtosis(fem, na.rm = T) :
  el argumento(s) no fue utilizado(s) (na.rm = T)

What can i do? the values of fem vary from -4 to 9.

Thanks very much
> kurtosis(fem[fem!="NA"])
[1] NA
Mario Garrido Escudero
PhD student
Dpto. de Biología Animal, Ecología, Parasitología, Edafología y Qca. Agrícola
Universidad de Salamanca
Reply | Threaded
Open this post in threaded view
|

Re: Applying a function to a subset

jholtman
kurtosis(fem[!is.na(fem)])

Sent from my iPad

On May 29, 2011, at 4:54, gaiarrido <[hidden email]> wrote:

> Here´s my problem,
> i have developed the function kurtosis (using R-book as a guide) with this
> commands:
> kurtosis<-function(x) {
> m4<-sum((x-mean(x))^4)/length(x)
> s4<-var(x)^2
> m4/s4 - 3 }
>
> Then create the object fem, which is the difference between the count of a
> trait in the left side of the body minus the right side. It´s a numeric
> variable. But i´ve got no data for some individuals, so in the variable
> "fem" i got numbers and smoe "NA".
> When i try to apply kurtosis to "fem", the NA, don´t allow me to do that,
> that`s what i get:
>
>> kurtosis(fem)
> [1] NA
>
> I tried with simple tricks, but i´ve got no results:
>> kurtosis(fem,na.rm=T)
> Error en kurtosis(fem, na.rm = T) :
>  el argumento(s) no fue utilizado(s) (na.rm = T)
>
> What can i do? the values of fem vary from -4 to 9.
>
> Thanks very much
>> kurtosis(fem[fem!="NA"])
> [1] NA
>
>
> -----
> Mario Garrido Escudero
> PhD student
> Dpto. de Biología Animal, Ecología, Parasitología, Edafología y Qca. Agrícola
> Universidad de Salamanca
> --
> View this message in context: http://r.789695.n4.nabble.com/Applying-a-function-to-a-subset-tp3558567p3558567.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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: Applying a function to a subset

gaiarrido
That's it.
thanks very much, I save hours if work qith your reply
Mario Garrido Escudero
PhD student
Dpto. de Biología Animal, Ecología, Parasitología, Edafología y Qca. Agrícola
Universidad de Salamanca
Reply | Threaded
Open this post in threaded view
|

Re: Applying a function to a subset

Stephan Kolassa
In reply to this post by jholtman
Or just include is.na=TRUE in the definition of kurtosis():

kurtosis<-function(x) {
   m4<-sum((x-mean(x,na.rm=TRUE))^4,na.rm=TRUE)/length(x)
   s4<-var(x,na.rm=TRUE)^2
   m4/s4 - 3 }

HTH
Stephan


Am 29.05.2011 11:34, schrieb Jim Holtman:

> kurtosis(fem[!is.na(fem)])
>
> Sent from my iPad
>
> On May 29, 2011, at 4:54, gaiarrido<[hidden email]>  wrote:
>
>> Here´s my problem,
>> i have developed the function kurtosis (using R-book as a guide) with this
>> commands:
>> kurtosis<-function(x) {
>> m4<-sum((x-mean(x))^4)/length(x)
>> s4<-var(x)^2
>> m4/s4 - 3 }
>>
>> Then create the object fem, which is the difference between the count of a
>> trait in the left side of the body minus the right side. It´s a numeric
>> variable. But i´ve got no data for some individuals, so in the variable
>> "fem" i got numbers and smoe "NA".
>> When i try to apply kurtosis to "fem", the NA, don´t allow me to do that,
>> that`s what i get:
>>
>>> kurtosis(fem)
>> [1] NA
>>
>> I tried with simple tricks, but i´ve got no results:
>>> kurtosis(fem,na.rm=T)
>> Error en kurtosis(fem, na.rm = T) :
>>   el argumento(s) no fue utilizado(s) (na.rm = T)
>>
>> What can i do? the values of fem vary from -4 to 9.
>>
>> Thanks very much
>>> kurtosis(fem[fem!="NA"])
>> [1] NA
>>
>>
>> -----
>> Mario Garrido Escudero
>> PhD student
>> Dpto. de Biología Animal, Ecología, Parasitología, Edafología y Qca. Agrícola
>> Universidad de Salamanca
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Applying-a-function-to-a-subset-tp3558567p3558567.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> 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.
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.