Dear All,
Sorry to bother
I want to write a function in R using if
Say I have a dataset x,
if x[i]<0, then x[i]=x[i],
if x[i]>0, then x[i]=0
for example, x=-3:3,
then using the function, x becomes [-3,-2,-1,0,0,0,0]
I write the codes as follows,
gjr=function(x)
{lena=length(x)
for(i in 1:lenx)
if (x[i]<0) return (x[i])
if (x[i]>0) return (0)
x}
but then, doing
gjr(x?
it only comes out with one number
Does anyone have any suggestions?
I appreciate a lot!
Sincerely,
Zoe
--
View this message in context: http://r.789695.n4.nabble.com/if-function-problems-tp3710995p3710995.html
Sent from the R help mailing list archive at Nabble.com.
if function problems
6 messages · zoe_zhang, David Winsemius, Steve Lianoglou +1 more
On Aug 1, 2011, at 6:57 PM, zoe_zhang wrote:
Dear All, Sorry to bother I want to write a function in R using if Say I have a dataset x, if x[i]<0, then x[i]=x[i], if x[i]>0, then x[i]=0 for example, x=-3:3, then using the function, x becomes [-3,-2,-1,0,0,0,0]
Just use logical indexing x[ x>0 ] <- 0
I write the codes as follows,
gjr=function(x)
{lena=length(x)
for(i in 1:lenx)
if (x[i]<0) return (x[i])
if (x[i]>0) return (0)
x}
but then, doing
gjr(x?
it only comes out with one number
'if' is not the right function. Look at ?"if" ?ifelse (But the logical indexing is easier in this case than using ifelse.)
David Winsemius, MD West Hartford, CT
David, I'm so appreciate! Sincerely, Zoe -- View this message in context: http://r.789695.n4.nabble.com/if-function-problems-tp3710995p3711062.html Sent from the R help mailing list archive at Nabble.com.
In addition to what David said:
On Mon, Aug 1, 2011 at 6:57 PM, zoe_zhang <1987.zhangxi at gmail.com> wrote:
Dear All,
Sorry to bother
I want to write a function in R using if
Say I have a dataset x,
if x[i]<0, then x[i]=x[i],
if x[i]>0, then x[i]=0
for example, x=-3:3,
then using the function, x becomes [-3,-2,-1,0,0,0,0]
I write the codes as follows,
gjr=function(x)
{lena=length(x)
for(i in 1:lenx)
if (x[i]<0) return (x[i])
if (x[i]>0) return (0)
x}
but then, doing
gjr(x?
it only comes out with one number
Does anyone have any suggestions?
You define `lena`, but then use `lenx` in `for (i in 1:lenx)` in your function ... I guess this might have something to do with it. You shouldn't use a for loop, though, and just follow david's advice by using logical indexing, or the `ifelse` function, ie: R> ifelse(x < 0, x, 0) HTH, -steve
Steve Lianoglou Graduate Student: Computational Systems Biology ?| Memorial Sloan-Kettering Cancer Center ?| Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
Thank you for your adding, Steve, i followed Daivd's suggection and finally got the answer. It is my careless that should put lena instead of lenx. I also tried your codes and worked well. I appreciate your help. I learnt a lot from this forum. Cheers, Zoe -- View this message in context: http://r.789695.n4.nabble.com/if-function-problems-tp3710995p3711340.html Sent from the R help mailing list archive at Nabble.com.
Hi another possibility is to use logical values properties
(x < 0)*x
[1] -3 -2 -1 0 0 0 0 Regards Petr
In addition to what David said: On Mon, Aug 1, 2011 at 6:57 PM, zoe_zhang <1987.zhangxi at gmail.com>
wrote:
Dear All,
Sorry to bother
I want to write a function in R using if
Say I have a dataset x,
if x[i]<0, then x[i]=x[i],
if x[i]>0, then x[i]=0
for example, x=-3:3,
then using the function, x becomes [-3,-2,-1,0,0,0,0]
I write the codes as follows,
gjr=function(x)
{lena=length(x)
for(i in 1:lenx)
if (x[i]<0) return (x[i])
if (x[i]>0) return (0)
x}
but then, doing
gjr(x?
it only comes out with one number
Does anyone have any suggestions?
You define `lena`, but then use `lenx` in `for (i in 1:lenx)` in your function ... I guess this might have something to do with it. You shouldn't use a for loop, though, and just follow david's advice by using logical indexing, or the `ifelse` function, ie: R> ifelse(x < 0, x, 0) HTH, -steve -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
______________________________________________ R-help at r-project.org 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.