Hi all, I had a dataset A like: TIME DV 0 0 1 10 5 20 24 30 36 80 48 60 72 15 I would like to add 24 to those values higher than 24 in the TIME column. I did the following: If (A$TIME>=24) { A$TIME <- A$TIME+24} It did not work. How should I do it? Thanks, -- View this message in context: http://r.789695.n4.nabble.com/help-with-if-statement-tp4650315.html Sent from the R help mailing list archive at Nabble.com.
Use ifelse(). ifelse(A$TIME >= 24, A$TIME + 24, A$TIME) Please in the future use dput() to provide your data, and explain what "did not work" means. Sarah On Wed, Nov 21, 2012 at 12:05 PM, york8866 <yu_york at hotmail.com> wrote:> Hi all, > > I had a dataset A like: > > TIME DV > 0 0 > 1 10 > 5 20 > 24 30 > 36 80 > 48 60 > 72 15 > > I would like to add 24 to those values higher than 24 in the TIME column. > > I did the following: > > If (A$TIME>=24) { > A$TIME <- A$TIME+24} > > It did not work. How should I do it? > > Thanks, > > >-- Sarah Goslee http://www.functionaldiversity.org
Does A$TIME <- ifelse( A$TIME >= 24, A$TIME + 24, A$TIME ) what you want? Rgds, Rainer On Wednesday 21 November 2012 09:05:39 york8866 wrote:> Hi all, > > I had a dataset A like: > > TIME DV > 0 0 > 1 10 > 5 20 > 24 30 > 36 80 > 48 60 > 72 15 > > I would like to add 24 to those values higher than 24 in the TIME column. > > I did the following: > > If (A$TIME>=24) { > A$TIME <- A$TIME+24} > > It did not work. How should I do it? > > Thanks, > > > > -- > View this message in context: http://r.789695.n4.nabble.com/help-with-if-statement-tp4650315.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On Nov 21, 2012, at 9:05 AM, york8866 wrote:> Hi all, > > I had a dataset A like: > > TIME DV > 0 0 > 1 10 > 5 20 > 24 30 > 36 80 > 48 60 > 72 15 > > I would like to add 24 to those values higher than 24 in the TIME > column. > > I did the following: > > If (A$TIME>=24) { > A$TIME <- A$TIME+24} > > It did not work. How should I do it?You should study the help pages for if and ifelse: ?"if" ?ifelse -- David Winsemius, MD Alameda, CA, USA
DT<-data.frame(time=c(0,1,5,24,36,48,72),DV=seq(0,60,10)) time DV 1 0 0 2 1 10 3 5 20 4 48 30 5 84 40 6 96 50 7 120 60 You want to add 24 to values that are >=24 in 'time' DT[DT$time>=24,'time']<-DT[DT$time>=24,'time']+24 time DV 1 0 0 2 1 10 3 5 20 4 48 30 5 60 40 6 72 50 7 96 60 Is this what you were looking for? A. -- View this message in context: http://r.789695.n4.nabble.com/help-with-if-statement-tp4652015p4652055.html Sent from the R help mailing list archive at Nabble.com.