Hi How can do this in R. >df 48 1 35 32 80 If df < 30 then replace it with 30 and else if df > 60 replace it with 60. I have a large dataset so I cant afford to identify indexes and then replace. Desired o/p: 48 30 35 32 60 Thanx in advance. Sachin __________________________________________________ [[alternative HTML version deleted]]
x <- 10*1:10 pmin(pmax(x, 30), 60) # 30 30 30 40 50 60 60 60 60 60 On 5/23/06, Sachin J <sachinj.2006 at yahoo.com> wrote:> Hi > > How can do this in R. > > >df > > 48 > 1 > 35 > 32 > 80 > > If df < 30 then replace it with 30 and else if df > 60 replace it with 60. I have a large dataset so I cant afford to identify indexes and then replace. > Desired o/p: > > 48 > 30 > 35 > 32 > 60 > > Thanx in advance. > > Sachin > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >
On Tue, 2006-05-23 at 11:40 -0700, Sachin J wrote:> Hi > > How can do this in R. > > >df > > 48 > 1 > 35 > 32 > 80 > > If df < 30 then replace it with 30 and else if df > 60 replace it > with 60. I have a large dataset so I cant afford to identify indexes > and then replace. > Desired o/p: > > 48 > 30 > 35 > 32 > 60 > > Thanx in advance. > > SachinOne approach is to combine the use of two ifelse() statements:> ifelse(df < 30, 30, ifelse(df > 60, 60, df))[1] 48 30 35 32 60 Recall that if the condition (1st argument) is TRUE, then the second argument is evaluated and returned. If the condition is FALSE, then the third argument is evaluated, which in this case is another ifelse(). The same logic follows within that function. See ?ifelse HTH, Marc Schwartz
you could try something like:
ifelse(df < 30, 30, ifelse(df > 60, 60, df))
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
Quoting Sachin J <sachinj.2006 at yahoo.com>:
> Hi
>
> How can do this in R.
>
> >df
>
> 48
> 1
> 35
> 32
> 80
>
> If df < 30 then replace it with 30 and else if df > 60 replace it
> with 60. I have a large dataset so I cant afford to identify indexes
> and then replace.
> Desired o/p:
>
> 48
> 30
> 35
> 32
> 60
>
> Thanx in advance.
>
> Sachin
> __________________________________________________
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Sachin J wrote:> Hi > > How can do this in R. > > >df > > 48 > 1 > 35 > 32 > 80 > > If df < 30 then replace it with 30 and else if df > 60 replace it with 60. I have a large dataset so I cant afford to identify indexes and then replace. > Desired o/p: > > 48 > 30 > 35 > 32 > 60 > > Thanx in advance. > > Sachin > __________________________________________________ > >Try: pmax(pmin(df, 60), 30) assuming "df" is numeric (and not a data.frame). ifelse is also an option. --sundar
Sachin, there's another slower but more flexible way than Gabor's solution: ifelse(x<30,30,ifelse(x>60,60,x)) HTH, Rogerio. ----- Original Message ----- From: "Sachin J" <sachinj.2006 at yahoo.com> To: <R-help at stat.math.ethz.ch> Sent: Tuesday, May 23, 2006 3:40 PM Subject: [R] conditional replacement> Hi > > How can do this in R. > > >df > > 48 > 1 > 35 > 32 > 80 > > If df < 30 then replace it with 30 and else if df > 60 replace it with > 60. I have a large dataset so I cant afford to identify indexes and then > replace. > Desired o/p: > > 48 > 30 > 35 > 32 > 60 > > Thanx in advance. > > Sachin > __________________________________________________ > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >