Hello Everyone, I am trying to replace the values in the 2nd column (Variable 1) corresponding to certain dates (Date) with NAs as shown below. Both Date and Variable1 are numeric vectors . I am trying to use the transform function as shown below but it doesn?t seem to work even though if I am not getting any error Any suggestions/help in this regard? regards, --------------------------------------------------------------------------------------------------------------------------------------------------------- Df1 <- data.frame(Date, Variable1) a1 <- transform(Df1, ifelse(Date > "010301000300 " && Date < "010501000300", Variable1 ==NA, Variable1)) Original Data frame Date Variable1 010101000300 1 010201000300 2 010301000300 3 010401000300 4 010501000300 5 010601000300 6 010701000300 7 . . . ???. Transformed data frame (i hope to transform) Date Variable1 010101000300 1 010201000300 2 010301000300 NA 010401000300 NA 010501000300 NA 010601000300 6 010701000300 7 ???. [[alternative HTML version deleted]]
This might not be the whole story, but part of the problem is that you want to select a _*character string*_ greater/smaller than another. That doesn't make much sense! I am not sure how to best compare two dates, but if you convert the Date values into numeric, then that would work. The problem is that it seems your Date values are character, and the comparison in your ifelse is also a character. So something like this might work (untested because no reproducible example): transform(Df1, ifelse(as.numeric(Date) > numeric.value1 && as.numeric(Date) < numeric.value2, Variable1 ==NA, Variable1)) HTH, Ivan -- Ivan Calandra, PhD Scientific Mediator University of Reims Champagne-Ardenne GEGENAA - EA 3795 CREA - 2 esplanade Roland Garros 51100 Reims, France +33(0)3 26 77 36 89 ivan.calandra at univ-reims.fr -- https://www.researchgate.net/profile/Ivan_Calandra https://publons.com/author/705639/ Le 21/07/2016 ? 03:00, Bhaskar Mitra a ?crit :> Hello Everyone, > > > I am trying to replace the values in the 2nd column (Variable 1) > corresponding to certain dates (Date) > > > with NAs as shown below. Both Date and Variable1 are numeric vectors . I am > trying to use the transform function > > > as shown below but it doesn?t seem to work even though if I am not getting > any error > > > Any suggestions/help in this regard? > > regards, > > --------------------------------------------------------------------------------------------------------------------------------------------------------- > > > > Df1 <- data.frame(Date, Variable1) > > > > > a1 <- transform(Df1, ifelse(Date > "010301000300 " && Date < > "010501000300", Variable1 ==NA, Variable1)) > > > Original Data frame > > > > Date Variable1 > > 010101000300 1 > > 010201000300 2 > > 010301000300 3 > > 010401000300 4 > > 010501000300 5 > > 010601000300 6 > > 010701000300 7 > > . > > . > > . > > ???. > > > > > Transformed data frame (i hope to transform) > > > > Date Variable1 > > 010101000300 1 > > 010201000300 2 > > 010301000300 NA > > 010401000300 NA > > 010501000300 NA > > 010601000300 6 > > 010701000300 7 > > > > ???. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hello, Another thing to consider is to use Variable1 = NA, not '=='. With '==' it will probably return TRUE/FALSE/NA. Hope this helps, Rui Barradas ? Citando Ivan Calandra <ivan.calandra at univ-reims.fr>:> This might not be the whole story, but part of the problem is that > you want to select a _*character string*_ greater/smaller than > another. That doesn't make much sense! > > I am not sure how to best compare two dates, but if you convert the > Date values into numeric, then that would work. The problem is that > it seems your Date values are character, and the comparison in your > ifelse is also a character. > > So something like this might work (untested because no reproducible example): > transform(Df1, ifelse(as.numeric(Date) > numeric.value1 && > as.numeric(Date) < numeric.value2, Variable1 ==NA, Variable1)) > > HTH, > Ivan > > -- > Ivan Calandra, PhD > Scientific Mediator > University of Reims Champagne-Ardenne > GEGENAA - EA 3795 > CREA - 2 esplanade Roland Garros > 51100 Reims, France > +33(0)3 26 77 36 89 > ivan.calandra at univ-reims.fr > -- > https://www.researchgate.net/profile/Ivan_Calandra > https://publons.com/author/705639/ > > Le 21/07/2016 ? 03:00, Bhaskar Mitra a ?crit : >> Hello Everyone, >> >> I am trying to replace the values in the 2nd column (Variable 1) >> corresponding to certain dates? (Date) >> >> with NAs as shown below. Both Date and Variable1 are numeric vectors . I am >> trying to use the transform function >> >> as shown below but it doesn?t seem to work even though if I am not getting >> any error >> >> Any suggestions/help in this regard? >> >> regards, >> >> --------------------------------------------------------------------------------------------------------------------------------------------------------- >> >> Df1 <- data.frame(Date, Variable1) >> >> a1 <- transform(Df1, ifelse(Date? > "010301000300 " && Date < >> "010501000300", Variable1 ==NA, Variable1)) >> >> Original Data frame >> >> ? ? ?Date? ? ? ? ? ? ? ? ? ? ? ? ? Variable1 >> >> 010101000300? ? ? ? ? ? ? ? ? ? ?1 >> >> 010201000300? ? ? ? ? ? ? ? ? ? ?2 >> >> 010301000300? ? ? ? ? ? ? ? ? ? ?3 >> >> 010401000300? ? ? ? ? ? ? ? ? ? ?4 >> >> 010501000300? ? ? ? ? ? ? ? ? ? ?5 >> >> 010601000300? ? ? ? ? ? ? ? ? ? ?6 >> >> 010701000300? ? ? ? ? ? ? ? ? ? ?7 >> >> . >> >> . >> >> . >> >> ???. >> >> Transformed data frame (i hope to transform) >> >> ? ? ? Date? ? ? ? ? ? ? ? ? ? ? ?Variable1 >> >> 010101000300? ? ? ? ? ? ? ? ? ? 1 >> >> 010201000300? ? ? ? ? ? ? ? ? ? 2 >> >> 010301000300? ? ? ? ? ? ? ? ? ?NA >> >> 010401000300? ? ? ? ? ? ? ? ? ?NA >> >> 010501000300? ? ? ? ? ? ? ? ? ? NA >> >> 010601000300? ? ? ? ? ? ? ? ? ? 6 >> >> 010701000300? ? ? ? ? ? ? ? ? ? 7 >> >> ???. >> >> ? ? ? ? [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
Not sure I can translate the format of your Date column correctly, however the command DF1$Date <- as.Date(DF1$Date, format=?formatstr?) Will convert the dates into a format correctly handled by R. ?strptime Should give you an idea of what formatstr should look like. I.e. if date = 160721 as.Date(date, format=?%y%M%d?) From: Bhaskar Mitra [[alternative HTML version deleted]]