[R] if function problems

zoe_zhang 1987.zhangxi at gmail.com
Tue Aug 2 00:57:23 CEST 2011


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.



More information about the R-help mailing list