Dear all, I am trying to learn R. Is it possible to find the mean of some rows (of some table) and to put it in new table. For example we have following table date x y z 1 05-23-2001 7 1 3 2 05-24-2001 8 4 5 3 05-24-2001 6 0 0 4 05-24-2001 26 2 6 5 06-19-2001 0 7 0 6 06-19-2001 5 0 2 and I want to make the table date x y z 1 05-23-2001 2 05-24-2001 (mean values for each day's) 6 06-19-2001 Is it possible to do this using R??? If possible pl. help me. Thanking you. Regards, mini
Ghosh Mini wrote:> > Dear all, > > I am trying to learn R. > Is it possible to find the mean of some rows (of some table) and to put it > in new table. > > For example we have following table > > date x y z > 1 05-23-2001 7 1 3 > 2 05-24-2001 8 4 5 > 3 05-24-2001 6 0 0 > 4 05-24-2001 26 2 6 > 5 06-19-2001 0 7 0 > 6 06-19-2001 5 0 2 > > > and I want to make the table > > date x y z > 1 05-23-2001 > 2 05-24-2001 (mean values for each day's) > 6 06-19-2001 > > Is it possible to do this using R??? > > If possible pl. help me. Thanking you. >Let `dframe' contain your first table. Then use aggregate: aggregate(dframe[, c("x", "y", "z")], list(date = dframe$date), mean) HTH, Sundar
<BODY><P></P> <P><FONT face=Courier>Ghosh Mini <<A href="mailto:ghosh@science.unitn.it">ghosh@science.unitn.it</A>></FONT></P> <P><FONT face=Courier>> Is it possible to find the mean of some rows (of some table) and <BR>> to put it in new table.</FONT></P> <P><FONT face=Courier>Maybe you want something like this:</FONT></P> <P><FONT face=Courier>apply(mydata[, c("x", "y", "z")], 2, function(x){tapply(x, list(mydata$date), mean, na.rm=TRUE)})</FONT></P> <P><FONT face=Courier>hope this helps,</FONT></P> <P><FONT face=Courier>Chuck Cleland</FONT></P> <P><FONT face=Courier><BR> </P></FONT></BODY>
"rowMeans" also works. Alternatives can be timed using "proc.time". Spencer Graves ccleland at optonline.net wrote:> Ghosh Mini <ghosh at science.unitn.it <mailto:ghosh at science.unitn.it>> > > > Is it possible to find the mean of some rows (of some table) and > > to put it in new table. > > Maybe you want something like this: > > apply(mydata[, c("x", "y", "z")], 2, function(x){tapply(x, > list(mydata$date), mean, na.rm=TRUE)}) > > hope this helps, > > Chuck Cleland > > > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Dear all, I am calling a table (data of several years) by psql command (order by date) aggregate(mydata[, c("nymphs", "adults")],list(date = mydata$date) is giving table order by month (of each year) like 1 09-29-1999 0.00000000 0.00000000 2 09-30-1999 0.16666667 0.00000000 3 10-05-2000 0.55555556 0.05555556 4 11-04-2000 0.07142857 0.00000000 5 11-05-2000 0.55000000 0.05000000 6 12-08-1999 0.00000000 0.00000000 7 12-09-2000 0.56410256 0.00000000 I want it arranged by date (increasing order). If possible pl. help me, thanking you. regards, mini
As far as I can see those are just character strings, and what has psql to do with it? Some more details would have been helpful. Your strings do not appear to be valid dates: the ISO standard is 1999-09-29 etc. So the simplest solution is to use a Standard data format, which will sort the way you want them. If you cannot do that, try ?strptime and ?as.POSIXct to see how to convert your strings to POSIXct dates, and if you want, back to a useful format. Alternatively, you could re-arrange the strings via strsplit. The bottom line is simple: use a proper format. That someone with an Italian email address is using an American-only format is puzzling. On Sat, 29 Mar 2003, Ghosh Mini wrote:> > Dear all, > > I am calling a table (data of several years) by psql command (order by > date) > > aggregate(mydata[, c("nymphs", "adults")],list(date = mydata$date) > > is giving table order by month (of each year) like > > > 1 09-29-1999 0.00000000 0.00000000 > 2 09-30-1999 0.16666667 0.00000000 > 3 10-05-2000 0.55555556 0.05555556 > 4 11-04-2000 0.07142857 0.00000000 > 5 11-05-2000 0.55000000 0.05000000 > 6 12-08-1999 0.00000000 0.00000000 > 7 12-09-2000 0.56410256 0.00000000 > > I want it arranged by date (increasing order).-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Dear all, Hope you are fine there. I want to know " Is it possible to treat Day,month and year seperately in a data frame in R" Means, suppose I want to take mean of data first by day wise and then by month. Is it possible using R?? Thanking you, regards, mini
On Mon, Mar 31, 2003 at 01:40:23PM +0200, Ghosh Mini wrote:> > Dear all, > > Hope you are fine there. I want to know > > " Is it possible to treat Day,month and year seperately in a data frame in > R"It''s easy, if your date column is a POSIX*t date/time class. help(DateTimeClasses) should shed some light.> Means, suppose I want to take mean of data first by day wise and then by > month. Is it possible using R??Use by() or tapply() with format(your.POSIX*t.object) for indicies, and it''s pretty easy. Something like this, as a toy example... myData <- data.frame(numbers=1:20) myData$date <- as.POSIXct(paste(sep="-","2003",1:4,1:20)) myData #by day by(myData$numbers,as.character(format(myData$date,"%d")),sum) #by month by(myData$numbers,as.character(format(myData$date,"%m")),sum) Cheers Jason -- Indigo Industrial Controls Ltd. 64-21-343-545 jasont at indigoindustrial.co.nz
Apparently Analagous Threads
- NaNs in Nested Mixed Model
- How to subset based on column name that is a number ?
- Efficient passing through big data.frame and modifying select
- How to use access results of gregexpr in data frames
- quotation problem/dataframe names as function input argument.