If you are very inexperienced with R you still have time to forget you ever heard of the attach function. Your code > attach(datATMP) > datATMP1 <- datATMP[order(MM),] > detach(datATMP) can be replaced by > datATMP1 <- datATMP[order(datATMP[["MM"]]),]
Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 18, 2016 at 6:18 AM, Tom Mosca <t...@vims.edu> wrote: > Dear Jim, > > > > I'm very inexperienced with R. > > > > I'm sorry I failed to recognize the flaw in my example. I just clipped > the first few lines of data, and should have realized that they were for > the first few hours of a single day. > > > > My first problem was to take the means by day. As all the data come from > a single year, I accomplished the goal by using: > > > datATMP<-aggregate(mydata, by=mydata[c("MM","DD")], FUN=mean) > > > > My second problem was to sort the results by month, for which I used: > > > attach(datATMP) > > datATMP1 <- datATMP[order(MM),] > > datATMP1 > > detach(datATMP) > > > > I have read that I should avoid using attach(), and should use with() > instead. However, I have not yet figured out how to do this with with(). > I'm only running short segments of code, and think I'll be alright with > attach() for the time being, but do want to develop better form. So, I'll > keep working on it. > > > > Thank you for your kind response and examples. I will study them. > > > > Sincerely, Tom > > > > > > ________________________________ > From: jim holtman [jholt...@gmail.com] > Sent: Sunday, July 17, 2016 7:14 PM > To: Jianling Fan > Cc: Tom Mosca; r-help@r-project.org > Subject: Re: [R] means by year, month and day > > Here is an example of using dplyr. Please provide a reasonable subset of > data. Your was all for the same date. Use 'dput' to put in your email. > > > x <- read.table(text = " X.YY MM DD hh WDI R.WSP D.GST PRES ATMP > DEWP > + 2015 1 1 0 328 3.6 4.5 1028.0 3.8 -3.5 > + 2015 1 1 1 300 2.1 2.7 1027.9 3.7 -4.4 > + 2015 1 1 2 264 2.4 2.9 1027.7 3.6 -4.5 > + 2015 1 1 3 230 4.1 4.5 1027.4 4.2 -3.8 > + 2015 1 1 4 242 8.1 9.2 1026.6 4.4 -3.1 > + 2015 1 1 5 262 9.3 10.1 1026.6 4.1 -3.8 > + 2015 1 1 6 267 8.6 9.6 1026.3 4.2 -3.8 > + 2015 1 1 7 264 9.3 9.9 1026.1 3.9 -2.8 > + 2015 1 1 8 268 8.2 9.1 1026.1 3.5 -3.0 > + 2015 1 1 9 272 8.8 9.6 1025.4 3.2 -3.3 > + 2015 2 1 0 328 3.6 4.5 1028.0 3.8 -3.5 > + 2015 2 1 1 300 2.1 2.7 1027.9 3.7 -4.4 > + 2015 2 1 2 264 2.4 2.9 1027.7 3.6 -4.5 > + 2015 2 1 3 230 4.1 4.5 1027.4 4.2 -3.8 > + 2015 2 1 4 242 8.1 9.2 1026.6 4.4 -3.1 > + 2015 2 1 5 262 9.3 10.1 1026.6 4.1 -3.8 > + 2015 2 1 6 267 8.6 9.6 1026.3 4.2 -3.8 > + 2015 2 1 7 264 9.3 9.9 1026.1 3.9 -2.8 > + 2015 2 1 8 268 8.2 9.1 1026.1 3.5 -3.0 > + 2015 2 1 9 272 8.8 9.6 1025.4 3.2 -3.3 > + 2015 3 1 0 328 3.6 4.5 1028.0 3.8 -3.5 > + 2015 3 1 1 300 2.1 2.7 1027.9 3.7 -4.4 > + 2015 3 1 2 264 2.4 2.9 1027.7 3.6 -4.5 > + 2015 3 1 3 230 4.1 4.5 1027.4 4.2 -3.8 > + 2015 3 1 4 242 8.1 9.2 1026.6 4.4 -3.1 > + 2015 3 1 5 262 9.3 10.1 1026.6 4.1 -3.8 > + 2015 3 1 6 267 8.6 9.6 1026.3 4.2 -3.8 > + 2015 3 1 7 264 9.3 9.9 1026.1 3.9 -2.8 > + 2015 3 1 8 268 8.2 9.1 1026.1 3.5 -3.0 > + 2015 3 1 9 272 8.8 9.6 1025.4 3.2 -3.3 > + ", > + header = TRUE, > + as.is<http://as.is> = TRUE) > > > > library(dplyr) > > by_year <- x %>% > + group_by(X.YY) %>% > + summarise_each(funs(mean)) > > > > by_ym <- x %>% > + group_by(X.YY, MM ) %>% > + summarise_each(funs(mean)) > > > > by_ymd <- x %>% > + group_by(X.YY, MM, DD) %>% > + summarise_each(funs(mean)) > > > > by_year > Source: local data frame [1 x 10] > X.YY MM DD hh WDI R.WSP D.GST PRES ATMP DEWP > <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> > 1 2015 2 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > > by_ym > Source: local data frame [3 x 10] > Groups: X.YY [?] > X.YY MM DD hh WDI R.WSP D.GST PRES ATMP DEWP > <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> > 1 2015 1 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > 2 2015 2 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > 3 2015 3 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > > by_ymd > Source: local data frame [3 x 10] > Groups: X.YY, MM [?] > X.YY MM DD hh WDI R.WSP D.GST PRES ATMP DEWP > <int> <int> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> > 1 2015 1 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > 2 2015 2 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > 3 2015 3 1 4.5 269.7 6.45 7.21 1026.81 3.86 -3.6 > > > > > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. > > On Sun, Jul 17, 2016 at 5:42 PM, Jianling Fan <fanjianl...@gmail.com > <mailto:fanjianl...@gmail.com>> wrote: > Hello Tom, > > try aggregate() or cast(). Both works.I prefer the latter. > > > library(reshape) > desc<-melt(mydata, measure.vars=c("WDI","R.WSP", "D.GST", "PRES", > "ATMP", "DEWP"), > id.vars=c("X.YY","MM","DD")) > summary<-cast(desc, X.YY+MM+DD~variable, mean) > > > > > > > > > > On 17 July 2016 at 06:22, Tom Mosca <t...@vims.edu<mailto:t...@vims.edu>> > wrote: > > Hello Good Folk, > > > > My dataframe looks like this: > >> mydata > > X.YY MM DD hh WDI R.WSP D.GST PRES ATMP DEWP > > 1 2015 1 1 0 328 3.6 4.5 1028.0 3.8 -3.5 > > 2 2015 1 1 1 300 2.1 2.7 1027.9 3.7 -4.4 > > 3 2015 1 1 2 264 2.4 2.9 1027.7 3.6 -4.5 > > 4 2015 1 1 3 230 4.1 4.5 1027.4 4.2 -3.8 > > 5 2015 1 1 4 242 8.1 9.2 1026.6 4.4 -3.1 > > 6 2015 1 1 5 262 9.3 10.1 1026.6 4.1 -3.8 > > 7 2015 1 1 6 267 8.6 9.6 1026.3 4.2 -3.8 > > 8 2015 1 1 7 264 9.3 9.9 1026.1 3.9 -2.8 > > 9 2015 1 1 8 268 8.2 9.1 1026.1 3.5 -3.0 > > 10 2015 1 1 9 272 8.8 9.6 1025.4 3.2 -3.3 … > > > > The first four columns are year, month, day, hour (0 – 23). I wish to > take the means of the next six columns (WDIR, WSPD, GST, PRES, ATMP and > DEWP) by year, month and day. That is, I want daily averages. > > > > Please help. Thank you. > > > > Tom > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > R-help@r-project.org<mailto:R-help@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. > > > > -- > Jianling Fan > 樊建凌 > > ______________________________________________ > R-help@r-project.org<mailto:R-help@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. > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.