Christoph Schlächter
2014-Apr-28 08:37 UTC
[R] subset of obersevation depending on multiple conditions
Hello, I want to subset a data.frame containing the variables "Date" in (%Y %m %d ) and "N". I want to print "Date" and "N" if N is less than or equal to 0.3 and if "N" is also less than or equal to 0.3 on the day before the day where "N" is less than or equal to 0.3. This would be the case in line 3 and 4 , 6 and 7, 12 to 18, and so on. "Date" "N" "1" 2010-01-01 0 "2" 2010-01-02 1.9 "3" 2010-01-03 0 "4" 2010-01-04 0 "5" 2010-01-05 1.6 "6" 2010-01-06 0 "7" 2010-01-07 0.3 "8" 2010-01-08 0 "9" 2010-01-09 1.1 "10" 2010-01-10 1.7 "11" 2010-01-11 2.6 "12" 2010-01-12 0 "13" 2010-01-13 0 "14" 2010-01-14 0 "15" 2010-01-15 0 "16" 2010-01-16 0 "17" 2010-01-17 0 "18" 2010-01-18 0.2 "19" 2010-01-19 0 "20" 2010-01-20 0 "21" 2010-01-21 0 "22" 2010-01-22 0 "23" 2010-01-23 0 "24" 2010-01-24 0 "25" 2010-01-25 0 "26" 2010-01-26 0 "27" 2010-01-27 1.9 "28" 2010-01-28 6.2 "29" 2010-01-29 0 "30" 2010-01-30 0 I tried some methods with subset but I couldn't work it out. Maybe I have to use something like " for (i in x) {} but as a beginner I really don't know how to do it. Can somebody please help me with this. Thanks in advance, Christoph [[alternative HTML version deleted]]
Ivan Calandra
2014-Apr-28 10:39 UTC
[R] subset of obersevation depending on multiple conditions
Hi Christoph, I'm not sure I understand your conditions. It is an "AND" or an "OR", i.e. must both conditions be met to subset or any one of them? From your explanation, I would think you really mean AND, but then I don't understand why you would select both lines 3 and 4. I would just say: df[df$N <= 0.3, ] but I might be missing something. You should also be careful with floating point numbers, I guess. HTH, Ivan -- Ivan Calandra University of Franche-Comt? Laboratoire Chrono-Environnement Bureau ATER -107L 16, Route de Gray 25030 Besan?on Cedex, France ivan.calandra at univ-fcomte.fr +33 (0) 381 66 20 60 http://chrono-environnement.univ-fcomte.fr/spip.php?article1830 Le 28/04/14 10:37, Christoph Schl?chter a ?crit :> Hello, > > I want to subset a data.frame containing the variables "Date" in (%Y %m %d > ) and "N". > > I want to print "Date" and "N" if N is less than or equal to 0.3 and if > "N" is also less than or equal to 0.3 on the day before the day where "N" > is less than or equal to 0.3. > > This would be the case in line 3 and 4 , 6 and 7, 12 to 18, and so on. > > "Date" "N" > "1" 2010-01-01 0 > "2" 2010-01-02 1.9 > "3" 2010-01-03 0 > "4" 2010-01-04 0 > "5" 2010-01-05 1.6 > "6" 2010-01-06 0 > "7" 2010-01-07 0.3 > "8" 2010-01-08 0 > "9" 2010-01-09 1.1 > "10" 2010-01-10 1.7 > "11" 2010-01-11 2.6 > "12" 2010-01-12 0 > "13" 2010-01-13 0 > "14" 2010-01-14 0 > "15" 2010-01-15 0 > "16" 2010-01-16 0 > "17" 2010-01-17 0 > "18" 2010-01-18 0.2 > "19" 2010-01-19 0 > "20" 2010-01-20 0 > "21" 2010-01-21 0 > "22" 2010-01-22 0 > "23" 2010-01-23 0 > "24" 2010-01-24 0 > "25" 2010-01-25 0 > "26" 2010-01-26 0 > "27" 2010-01-27 1.9 > "28" 2010-01-28 6.2 > "29" 2010-01-29 0 > "30" 2010-01-30 0 > > I tried some methods with subset but I couldn't work it out. Maybe I have > to use something like " for (i in x) {} but as a beginner I really don't > know how to do it. > > Can somebody please help me with this. > > Thanks in advance, > > Christoph > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Frede Aakmann Tøgersen
2014-Apr-28 12:27 UTC
[R] subset of obersevation depending on multiple conditions
Hi Here is a very simple way. mydat <- read.table(text=" Date N 2010-01-01 0 2010-01-02 1.9 2010-01-03 0 2010-01-04 0 2010-01-05 1.6 2010-01-06 0 2010-01-07 0.3 2010-01-08 0 2010-01-09 1.1 2010-01-10 1.7 2010-01-11 2.6 2010-01-12 0 2010-01-13 0 2010-01-14 0 2010-01-15 0 2010-01-16 0 2010-01-17 0 2010-01-18 0.2 2010-01-19 0 2010-01-20 0 2010-01-21 0 2010-01-22 0 2010-01-23 0 2010-01-24 0 2010-01-25 0 2010-01-26 0 2010-01-27 1.9 2010-01-28 6.2 2010-01-29 0 2010-01-30 0", sep = "", h = TRUE) mydat$NdayBefore <- c(NA, mydat[-nrow(mydat), "N"]) head(mydat) subset(mydat, N <= 0.3) subset(mydat, N <= 0.3 & NdayBefore <= 0.3) Yours sincerely / Med venlig hilsen Frede Aakmann T?gersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 frtog at vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Christoph Schl?chter > Sent: 28. april 2014 10:38 > To: r-help at r-project.org > Subject: [R] subset of obersevation depending on multiple conditions > > Hello, > > I want to subset a data.frame containing the variables "Date" in (%Y %m %d > ) and "N". > > I want to print "Date" and "N" if N is less than or equal to 0.3 and if > "N" is also less than or equal to 0.3 on the day before the day where "N" > is less than or equal to 0.3. > > This would be the case in line 3 and 4 , 6 and 7, 12 to 18, and so on. > > "Date" "N" > "1" 2010-01-01 0 > "2" 2010-01-02 1.9 > "3" 2010-01-03 0 > "4" 2010-01-04 0 > "5" 2010-01-05 1.6 > "6" 2010-01-06 0 > "7" 2010-01-07 0.3 > "8" 2010-01-08 0 > "9" 2010-01-09 1.1 > "10" 2010-01-10 1.7 > "11" 2010-01-11 2.6 > "12" 2010-01-12 0 > "13" 2010-01-13 0 > "14" 2010-01-14 0 > "15" 2010-01-15 0 > "16" 2010-01-16 0 > "17" 2010-01-17 0 > "18" 2010-01-18 0.2 > "19" 2010-01-19 0 > "20" 2010-01-20 0 > "21" 2010-01-21 0 > "22" 2010-01-22 0 > "23" 2010-01-23 0 > "24" 2010-01-24 0 > "25" 2010-01-25 0 > "26" 2010-01-26 0 > "27" 2010-01-27 1.9 > "28" 2010-01-28 6.2 > "29" 2010-01-29 0 > "30" 2010-01-30 0 > > I tried some methods with subset but I couldn't work it out. Maybe I have > to use something like " for (i in x) {} but as a beginner I really don't > know how to do it. > > Can somebody please help me with this. > > Thanks in advance, > > Christoph > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.