Dear R users:
I have two set of data, as follow:
x<-c(0,0,0.28,0.55,1.2,2,1.95,1.85,
1.6,0.86,0.78,0.6,0.21,0.18)
y<-c(0,0,0,0.53,1.34,1.79,2.07,1.88,
1.52,0.92,0.71,0.55,0.32,0.19)
i<-1:length(x)
I want to sum each (x[i]-y[i])^2/x[i] together,
like:>Sum <-sum((x[i]-y[i])^2/x[i])
>Sum
[1] NaN
Because the denominator shoud not be zero.
So I want to overlook those when x[i]=0,
and just to sum those x[i] not equal to 0.
What should I do?
Any suggestion.
Thanks in advance !!
What about
x<-c(0,0,0.28,0.55,1.2,2,1.95,1.85,
1.6,0.86,0.78,0.6,0.21,0.18)
y<-c(0,0,0,0.53,1.34,1.79,2.07,1.88,
1.52,0.92,0.71,0.55,0.32,0.19)
sum(((x-y)^2/x)[x!=0])
Regards,
Christoph Buser
--------------------------------------------------------------
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C13
ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
phone: x-41-44-632-4673 fax: 632-1228
http://stat.ethz.ch/~buser/
--------------------------------------------------------------
Chun-Ying Lee writes:
> Dear R users:
>
> I have two set of data, as follow:
> x<-c(0,0,0.28,0.55,1.2,2,1.95,1.85,
> 1.6,0.86,0.78,0.6,0.21,0.18)
> y<-c(0,0,0,0.53,1.34,1.79,2.07,1.88,
> 1.52,0.92,0.71,0.55,0.32,0.19)
> i<-1:length(x)
>
> I want to sum each (x[i]-y[i])^2/x[i] together,
> like:
> >Sum <-sum((x[i]-y[i])^2/x[i])
> >Sum
> [1] NaN
>
> Because the denominator shoud not be zero.
> So I want to overlook those when x[i]=0,
> and just to sum those x[i] not equal to 0.
> What should I do?
> Any suggestion.
> Thanks in advance !!
>
> ______________________________________________
> 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
look at ?sum() and use the `na.rm' argument, i.e.,
sum((x - y)^2 / x, na.rm = TRUE)
I hope it helps.
Best,
Dimitris
p.s., R is vectorized, you don't have to use `x[i]' in your example.
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Chun-Ying Lee" <u9370004 at cc.kmu.edu.tw>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, July 27, 2005 10:51 AM
Subject: [R] how to overlook the zero in the denominator
> Dear R users:
>
> I have two set of data, as follow:
> x<-c(0,0,0.28,0.55,1.2,2,1.95,1.85,
> 1.6,0.86,0.78,0.6,0.21,0.18)
> y<-c(0,0,0,0.53,1.34,1.79,2.07,1.88,
> 1.52,0.92,0.71,0.55,0.32,0.19)
> i<-1:length(x)
>
> I want to sum each (x[i]-y[i])^2/x[i] together,
> like:
>>Sum <-sum((x[i]-y[i])^2/x[i])
>>Sum
> [1] NaN
>
> Because the denominator shoud not be zero.
> So I want to overlook those when x[i]=0,
> and just to sum those x[i] not equal to 0.
> What should I do?
> Any suggestion.
> Thanks in advance !!
>
>
--------------------------------------------------------------------------------
> ______________________________________________
> 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
Adaikalavan Ramasamy
2005-Jul-27 09:19 UTC
[R] how to overlook the zero in the denominator
The simplest way would be to ignore them in the sum sum( ( x - y )/x, na.rm=T ) and notice that I have utilised the vectorised operation. But if you want to, you can explicitly remove them with good <- which( x != 0 ) sum( ( x[good] - y[good] )/ x[good] ) Regards, Adai On Wed, 2005-07-27 at 16:51 +0800, Chun-Ying Lee wrote:> Dear R users: > > I have two set of data, as follow: > x<-c(0,0,0.28,0.55,1.2,2,1.95,1.85, > 1.6,0.86,0.78,0.6,0.21,0.18) > y<-c(0,0,0,0.53,1.34,1.79,2.07,1.88, > 1.52,0.92,0.71,0.55,0.32,0.19) > i<-1:length(x) > > I want to sum each (x[i]-y[i])^2/x[i] together, > like: > >Sum <-sum((x[i]-y[i])^2/x[i]) > >Sum > [1] NaN > > Because the denominator shoud not be zero. > So I want to overlook those when x[i]=0, > and just to sum those x[i] not equal to 0. > What should I do? > Any suggestion. > Thanks in advance !! > > ______________________________________________ > 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