Hi Dennis,
I am looking for similar function and this post is useful. But a strange
thing is happening when I try which I couldn't figure out (details below).
Could you or anyone help me understand why this is so?
> df = data.frame(date = seq(as.Date("2010-1-1"), by =
"days", length 250))
> df$value = cumsum(rnorm(1:250))
When I use the statement (as given in ?aggregate help file) the following
error is displayed> aggregate(df$value, by = months(df$date), FUN = median)
Error in aggregate.data.frame(as.data.frame(x), ...) :
'by' must be a list
But it works when I use as was suggested > aggregate(value~months(date), data = df, FUN = median)
months(date) value
1 April 15.5721440
2 August -0.1261205
3 February -1.0230631
4 January -0.9277885
5 July -2.1890907
6 June 1.3045260
7 March 11.4126371
8 May 2.1625091
The second question, is it possible to have the median across the months and
years. Say I have daily data for last five years the above function will
give me the median of Jan of all the five years, while I want Jan-2010,
Jan-2009 and so... Wish my question is clear.
Any assistance will be greatly appreciated and many thanks for the same.
Regards,
Krishna
Date: Sun, 19 Dec 2010 15:42:15 -0800
From: Dennis Murphy <djmuser@gmail.com>
To: HUXTERE <emilyhuxter@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] monthly median in a daily dataset
Message-ID:
<AANLkTimXTJhbsE1mq4o121fEKXTf8d1pSYEEgzKKzdYu@mail.gmail.com>
Content-Type: text/plain
Hi:
There is a months() function associated with Date objects, so you should be
able to do something like
aggregate(value ~ months(date), data = data$flow$daily, FUN = median)
Here's a toy example because your data are not in a ready form:
df <- data.frame(date = seq(as.Date('2010-01-01'), by =
'days', length 250),
val = rnorm(250))> aggregate(val ~ months(date), data = df, FUN = median)
months(date) val
1 April -0.18864817
2 August -0.16203705
3 February 0.03671700
4 January 0.04500988
5 July -0.12753151
6 June 0.09864811
7 March 0.23652105
8 May 0.25879994
9 September 0.53570764
HTH,
Dennis
On Sun, Dec 19, 2010 at 2:31 PM, HUXTERE <emilyhuxter@gmail.com> wrote:
>
> Hello,
>
> I have a multi-year dataset (see below) with date, a data value and a flag
> for the data value. I want to find the monthly median for each month in
> this
> dataset and then plot it. If anyone has suggestions they would be greatly
> apperciated. It should be noted that there are some dates with no values
> and
> they should be removed.
>
> Thanks
> Emily
>
> > print ( str(data$flow$daily) )
> 'data.frame': 16071 obs. of 3 variables:
> $ date :Class 'Date' num [1:16071] -1826 -1825 -1824 -1823 -1822
...
> $ value: num NA NA NA NA NA NA NA NA NA NA ...
> $ flag : chr "" "" "" "" ...
> NULL
>
> 520 2008-11-01 0.034
> 1041 2008-11-02 0.034
> 1562 2008-11-03 0.034
> 2083 2008-11-04 0.038
> 2604 2008-11-05 0.036
> 3125 2008-11-06 0.035
> 3646 2008-11-07 0.036
> 4167 2008-11-08 0.039
> 4688 2008-11-09 0.039
> 5209 2008-11-10 0.039
> 5730 2008-11-11 0.038
> 6251 2008-11-12 0.039
> 6772 2008-11-13 0.039
> 7293 2008-11-14 0.038
> 7814 2008-11-15 0.037
> 8335 2008-11-16 0.037
> 8855 2008-11-17 0.037
> 9375 2008-11-18 0.037
> 9895 2008-11-19 0.034 B
> 10415 2008-11-20 0.034 B
> 10935 2008-11-21 0.033 B
> 11455 2008-11-22 0.034 B
> 11975 2008-11-23 0.034 B
> 12495 2008-11-24 0.034 B
> 13016 2008-11-25 0.034 B
> 13537 2008-11-26 0.033 B
> 14058 2008-11-27 0.033 B
> 14579 2008-11-28 0.033 B
> 15068 2008-11-29 0.034 B
> 15546 2008-11-30 0.035 B
> 521 2008-12-01 0.035 B
> 1042 2008-12-02 0.034 B
> 1563 2008-12-03 0.033 B
> 2084 2008-12-04 0.031 B
> 2605 2008-12-05 0.031 B
> 3126 2008-12-06 0.031 B
> 3647 2008-12-07 0.032 B
> 4168 2008-12-08 0.032 B
> 4689 2008-12-09 0.032 B
> 5210 2008-12-10 0.033 B
> 5731 2008-12-11 0.033 B
> 6252 2008-12-12 0.032 B
> 6773 2008-12-13 0.031 B
> 7294 2008-12-14 0.030 B
> 7815 2008-12-15 0.030 B
> 8336 2008-12-16 0.029 B
> 8856 2008-12-17 0.028 B
> 9376 2008-12-18 0.028 B
> 9896 2008-12-19 0.028 B
> 10416 2008-12-20 0.027 B
> 10936 2008-12-21 0.027 B
> 11456 2008-12-22 0.028 B
> 11976 2008-12-23 0.028 B
> 12496 2008-12-24 0.029 B
> 13017 2008-12-25 0.029 B
> 13538 2008-12-26 0.029 B
> 14059 2008-12-27 0.030 B
> 14580 2008-12-28 0.030 B
> 15069 2008-12-29 0.030 B
> 15547 2008-12-30 0.031 B
> 15851 2008-12-31 0.031 B
> --
> View this message in context:
>
http://r.789695.n4.nabble.com/monthly-median-in-a-daily-dataset-tp3094917p30
94917.html> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
------------------------------
Message: 35
Date: Mon, 20 Dec 2010 00:03:24 +0000
From: "Enrico R. Crema" <enryu_crema@yahoo.it>
To: r-help@r-project.org
Subject: [R] Time Series of Histograms
Content-Type: text/plain; charset=us-ascii
Dear List,
I have a set of distributions recorded at an equal interval of time and I
would like to plot them as series of horizontal histograms (with the x-axis
representing time, and y-axis representing the bins) since the distribution
shifts from unimodal to multimodal in several occasions. What I would like
to see is something close to a violinplot, but I do not want a kernel
density estimate...
[[elided Yahoo spam]]
Thanks in Advance,
Enrico
------------------------------
Message: 36
Date: Mon, 20 Dec 2010 00:21:02 +0000
From: Paolo Rossi <statmailinglists@googlemail.com>
To: r-help@r-project.org
Subject: [R] Turning a Variable into String
Message-ID:
<AANLkTi=fa+982ZNie+z-iDwURvq8Ee5zJoQ7OPXLhMK6@mail.gmail.com>
Content-Type: text/plain
I would like to know how to turn a variable into a string. I have tried
as.symbol and as.name but it doesnt work for what I'd like to do
Essentially, I'd like to feed the function below with two variables. This
works fine in the bit working out number of elements in each variable.
In the print(sprintf("OK with %s and %s\n", var1, var2)) line I would
like
var1 and var2 to be magically substituted with a string containing the name
of var1 and name of var2.
Thanks in advance
Paolo
haveSameLength <- function(var1, var2) {
if (length(var1)==length(var2))
{
print(sprintf("OK with %s and %s\n", var1, var2))
} else {
print("Problems!!")
}
}
[[alternative HTML version deleted]]
------------------------------
Message: 37
Date: Sun, 19 Dec 2010 16:30:38 -0800 (PST)
From: Phil Spector <spector@stat.berkeley.edu>
To: Paolo Rossi <statmailinglists@googlemail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Turning a Variable into String
Message-ID:
<alpine.DEB.2.00.1012191627170.26060@springer.Berkeley.EDU>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
Paolo -
One way to make the function do what you want is to replace
the line
print(sprintf("OK with %s and %s\n", var1, var2))
with
cat('OK
with',substitute(var1),'and',substitute(var2),'\n')
With sprintf, you'd need
print(sprintf("OK with %s and %s\n", deparse(substitute(var1)),
deparse(substitute(var2))))
but since you're just printing the string returned by sprintf, I'd
go with cat.
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector@stat.berkeley.edu
On Mon, 20 Dec 2010, Paolo Rossi wrote:
> I would like to know how to turn a variable into a string. I have tried
> as.symbol and as.name but it doesnt work for what I'd like to do
>
> Essentially, I'd like to feed the function below with two variables.
This
> works fine in the bit working out number of elements in each variable.
>
> In the print(sprintf("OK with %s and %s\n", var1, var2)) line I
would
like> var1 and var2 to be magically substituted with a string containing the
name> of var1 and name of var2.
>
> Thanks in advance
>
> Paolo
>
>
>
> haveSameLength <- function(var1, var2) {
> if (length(var1)==length(var2))
> {
> print(sprintf("OK with %s and %s\n", var1, var2))
> } else {
> print("Problems!!")
> }
> }
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
------------------------------
Message: 38
Date: Sun, 19 Dec 2010 19:35:28 -0500
From: Duncan Murdoch <murdoch.duncan@gmail.com>
To: Paolo Rossi <statmailinglists@googlemail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Turning a Variable into String
Message-ID: <4D0EA4D0.10507@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 19/12/2010 7:21 PM, Paolo Rossi wrote:> I would like to know how to turn a variable into a string. I have tried
> as.symbol and as.name but it doesnt work for what I'd like to do
>
> Essentially, I'd like to feed the function below with two variables.
This
> works fine in the bit working out number of elements in each variable.
>
> In the print(sprintf("OK with %s and %s\n", var1, var2)) line I
would
like> var1 and var2 to be magically substituted with a string containing the
name> of var1 and name of var2.
The name of var1 is var1, so I assume you mean the expression passed to
your function and bound to var1. In that case, what you want is
deparse(substitute(var1))
Watch out: if the expression is really long, that can be a vector with
more than one element. See ?deparse for ways to deal with that.
Duncan Murdoch
>
> Thanks in advance
>
> Paolo
>
>
>
> haveSameLength<- function(var1, var2) {
> if (length(var1)==length(var2))
> {
> print(sprintf("OK with %s and %s\n", var1, var2))
> } else {
> print("Problems!!")
> }
> }
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
------------------------------
Message: 39
Date: Sun, 19 Dec 2010 20:11:58 -0500
From: Duncan Murdoch <murdoch.duncan@gmail.com>
To: Jeff Breiwick <jeff.breiwick@noaa.gov>
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] system/system2 command
Message-ID: <4D0EAD5E.5060006@gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 17/12/2010 4:36 PM, Jeff Breiwick wrote:> All,
>
> I had a simple function call I used to open up a dos shell running R under
> Win XP:
> system("cmd.exe", wait=FALSE, invisible=FALSE).
>
> This does not work with R 2.12.1 - I get a window that briefly flashes
open> but then disappears. Does anyone know the method to open a DOS command
> window in running R with Win XP? Thank you.
This is a new bug in 2.12.1, which I am about to fix in R-patched. The
problem was that it was passing a null input stream to cmd.exe, which
saw an immediate EOF, and quit. A similar thing happened in Rterm,
where system("cmd") should drop into a command shell in the same
window,
but it would immediately exit.
Duncan Murdoch
------------------------------
Message: 40
Date: Sun, 19 Dec 2010 17:47:20 -0800
From: Dennis Murphy <djmuser@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Time Series of Histograms
Message-ID:
<AANLkTiknfHmEuaHp_7GirmpNKB=eMKSUeDNiE7jgh6Q_@mail.gmail.com>
Content-Type: text/plain
Hi:
You can get a violin plot in lattice rather straightforwardly. It's easiest
if time is an ordered factor, but you can also do it if time is numeric; in
the latter case, the code associated with Figure 10.14 in the Lattice book
provides a template to start with:
http://lmdvr.r-forge.r-project.org/figures/figures.html
To get horizontal violin plots, use time as the y variable and start by
replacing panel.boxplot with panel.violin; see the help page of the latter
if more specific options are required. It also contains an example using a
panel function.
I don't know how you expect to get horizontal histograms without setting the
time variable to be a factor. If you have enough time periods, the result
will not be pretty. If you have a fairly large number of time periods, the
best distributional displays are boxplots, violin plots, beanplots or some
variation of that general concept.
Since neither data nor code were offered, one can only speculate so far as
to what your intentions might be. A reproducible example with data and code
would undoubtedly elicit more useful responses.
HTH,
Dennis
On Sun, Dec 19, 2010 at 4:03 PM, Enrico R. Crema
> Dear List,
>
> I have a set of distributions recorded at an equal interval of time and I
> would like to plot them as series of horizontal histograms (with the
x-axis> representing time, and y-axis representing the bins) since the
distribution> shifts from unimodal to multimodal in several occasions. What I would
like> to see is something close to a violinplot, but I do not want a kernel
> density estimate...
[[elided Yahoo spam]]>
> Thanks in Advance,
> Enrico
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
------------------------------
Message: 41
Date: Mon, 20 Dec 2010 02:04:22 +0000
To: Dennis Murphy <djmuser@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Time Series of Histograms
Content-Type: text/plain
Many Thanks Dennis,
The distributions are simulated ordinal data all bounded in the same upper
and lower limit, and I wanted to plot how the distribution changes through
time. Since the distributions are often multimodal boxplots were not useful
so I made some violinplots... My practical solution which I'm testing right
now is to create a matrix of frequencies and then plot these as a series of
horrizontal barplots (after normalising each distribution) , using the
offset parameter to control the temporal sequence....It actually works fine,
but I was wondering if there were better ways...
Enrico
On 20 Dec 2010, at 01:47, Dennis Murphy wrote:
> Hi:
>
> You can get a violin plot in lattice rather straightforwardly. It's
easiest if time is an ordered factor, but you can also do it if time is
numeric; in the latter case, the code associated with Figure 10.14 in the
Lattice book provides a template to start with:
http://lmdvr.r-forge.r-project.org/figures/figures.html>
> To get horizontal violin plots, use time as the y variable and start by
replacing panel.boxplot with panel.violin; see the help page of the latter
if more specific options are required. It also contains an example using a
panel function.>
> I don't know how you expect to get horizontal histograms without
setting
the time variable to be a factor. If you have enough time periods, the
result will not be pretty. If you have a fairly large number of time
periods, the best distributional displays are boxplots, violin plots,
beanplots or some variation of that general concept.>
> Since neither data nor code were offered, one can only speculate so far as
to what your intentions might be. A reproducible example with data and code
would undoubtedly elicit more useful responses.>
> HTH,
> Dennis
>
>
wrote:> Dear List,
>
> I have a set of distributions recorded at an equal interval of time and I
would like to plot them as series of horizontal histograms (with the x-axis
representing time, and y-axis representing the bins) since the distribution
shifts from unimodal to multimodal in several occasions. What I would like
to see is something close to a violinplot, but I do not want a kernel
density estimate...
[[elided Yahoo spam]]>
> Thanks in Advance,
> Enrico
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
------------------------------
Message: 42
Date: Sun, 19 Dec 2010 21:11:15 -0500
From: Jorge Ivan Velez <jorgeivanvelez@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Time Series of Histograms
Message-ID:
<AANLkTikp5Zr3_AMJ7uGeHnWRuovW1ddnja2JXPhpD6Uu@mail.gmail.com>
Content-Type: text/plain
Hi Enrico,
Is this close to what you want to do?
http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=109
HTH,
Jorge
On Sun, Dec 19, 2010 at 7:03 PM, Enrico R. Crema <> wrote:
> Dear List,
>
> I have a set of distributions recorded at an equal interval of time and I
> would like to plot them as series of horizontal histograms (with the
x-axis> representing time, and y-axis representing the bins) since the
distribution> shifts from unimodal to multimodal in several occasions. What I would
like> to see is something close to a violinplot, but I do not want a kernel
> density estimate...
[[elided Yahoo spam]]>
> Thanks in Advance,
> Enrico
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
------------------------------
Message: 43
Date: Mon, 20 Dec 2010 13:17:59 +1100
From: <Bill.Venables@csiro.au>
To: <emilyhuxter@gmail.com>, <r-help@r-project.org>
Subject: Re: [R] monthly median in a daily dataset
Message-ID:
<1BDAE2969943D540934EE8B4EF68F95FB27A44FE07@EXNSW-MBX03.nexus.csiro.au>
Content-Type: text/plain; charset="us-ascii"
I find this function useful for digging out months from Date objects
Month <- function(date, ...)
factor(month.abb[as.POSIXlt(date)$mon + 1], levels = month.abb)
For this little data set below this is what it gives
> with(data, tapply(value, Month(date), median, na.rm = TRUE))
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
NA NA NA NA NA NA NA NA NA NA 0.035 0.030
Here is another useful little one:
Year <- function(date, ...)
as.POSIXlt(date)$year + 1900
So if you wanted the median by year and month you could do
> with(data, tapply(value, list(Year(date), Month(date)), median, na.rm
TRUE))
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 NA NA NA NA NA NA NA NA NA NA 0.035 0.03
(The result is a matrix, which in this case has only one row, of course.)
See how you go.
Bill Venables.
-----Original Message-----
From: r-help-bounces@r-project.org [mailto:r-help-bounces@r-project.org] On
Behalf Of HUXTERE
Sent: Monday, 20 December 2010 8:32 AM
To: r-help@r-project.org
Subject: [R] monthly median in a daily dataset
Hello,
I have a multi-year dataset (see below) with date, a data value and a flag
for the data value. I want to find the monthly median for each month in this
dataset and then plot it. If anyone has suggestions they would be greatly
apperciated. It should be noted that there are some dates with no values and
they should be removed.
Thanks
Emily
> print ( str(data$flow$daily) )
'data.frame': 16071 obs. of 3 variables:
$ date :Class 'Date' num [1:16071] -1826 -1825 -1824 -1823 -1822 ...
$ value: num NA NA NA NA NA NA NA NA NA NA ...
$ flag : chr "" "" "" "" ...
NULL
520 2008-11-01 0.034
1041 2008-11-02 0.034
1562 2008-11-03 0.034
2083 2008-11-04 0.038
2604 2008-11-05 0.036
3125 2008-11-06 0.035
3646 2008-11-07 0.036
4167 2008-11-08 0.039
4688 2008-11-09 0.039
5209 2008-11-10 0.039
5730 2008-11-11 0.038
6251 2008-11-12 0.039
6772 2008-11-13 0.039
7293 2008-11-14 0.038
7814 2008-11-15 0.037
8335 2008-11-16 0.037
8855 2008-11-17 0.037
9375 2008-11-18 0.037
9895 2008-11-19 0.034 B
10415 2008-11-20 0.034 B
10935 2008-11-21 0.033 B
11455 2008-11-22 0.034 B
11975 2008-11-23 0.034 B
12495 2008-11-24 0.034 B
13016 2008-11-25 0.034 B
13537 2008-11-26 0.033 B
14058 2008-11-27 0.033 B
14579 2008-11-28 0.033 B
15068 2008-11-29 0.034 B
15546 2008-11-30 0.035 B
521 2008-12-01 0.035 B
1042 2008-12-02 0.034 B
1563 2008-12-03 0.033 B
2084 2008-12-04 0.031 B
2605 2008-12-05 0.031 B
3126 2008-12-06 0.031 B
3647 2008-12-07 0.032 B
4168 2008-12-08 0.032 B
4689 2008-12-09 0.032 B
5210 2008-12-10 0.033 B
5731 2008-12-11 0.033 B
6252 2008-12-12 0.032 B
6773 2008-12-13 0.031 B
7294 2008-12-14 0.030 B
7815 2008-12-15 0.030 B
8336 2008-12-16 0.029 B
8856 2008-12-17 0.028 B
9376 2008-12-18 0.028 B
9896 2008-12-19 0.028 B
10416 2008-12-20 0.027 B
10936 2008-12-21 0.027 B
11456 2008-12-22 0.028 B
11976 2008-12-23 0.028 B
12496 2008-12-24 0.029 B
13017 2008-12-25 0.029 B
13538 2008-12-26 0.029 B
14059 2008-12-27 0.030 B
14580 2008-12-28 0.030 B
15069 2008-12-29 0.030 B
15547 2008-12-30 0.031 B
15851 2008-12-31 0.031 B
--
View this message in context:
http://r.789695.n4.nabble.com/monthly-median-in-a-daily-dataset-tp3094917p30
94917.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help@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.
------------------------------
Message: 44
Date: Mon, 20 Dec 2010 02:28:00 +0000
To: Jorge Ivan Velez <jorgeivanvelez@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] Time Series of Histograms
Content-Type: text/plain
Hi Jorge,
[[elided Yahoo spam]]
Many Thanks!!!
E.
On 20 Dec 2010, at 02:11, Jorge Ivan Velez wrote:
> Hi Enrico,
>
> Is this close to what you want to do?
>
> http://addictedtor.free.fr/graphiques/RGraphGallery.php?graph=109
>
> HTH,
> Jorge
>
>
> On Sun, Dec 19, 2010 at 7:03 PM, Enrico R. Crema <> wrote:
> Dear List,
>
> I have a set of distributions recorded at an equal interval of time and I
would like to plot them as series of horizontal histograms (with the x-axis
representing time, and y-axis representing the bins) since the distribution
shifts from unimodal to multimodal in several occasions. What I would like
to see is something close to a violinplot, but I do not want a kernel
density estimate...
[[elided Yahoo spam]]>
> Thanks in Advance,
> Enrico
> ______________________________________________
> R-help@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.
>
[[alternative HTML version deleted]]
------------------------------
Message: 45
Date: Mon, 20 Dec 2010 03:54:59 +0000 (UTC)
From: Ben Bolker <bbolker@gmail.com>
To: r-help@stat.math.ethz.ch
Subject: Re: [R] R.matlab memory use
Message-ID: <loom.20101220T043805-939@post.gmane.org>
Content-Type: text/plain; charset=us-ascii
Stefano Ghirlanda <dr.ghirlanda <at> gmail.com> writes:
> I am trying to load into R a MATLAB format file (actually, as saved by
> octave). The file is about 300kB but R complains with a memory
> allocation error:
>
> > library(Rcompression)
> > library(R.matlab)
> Loading required package: R.oo
> Loading required package: R.methodsS3
> R.methodsS3 v1.2.0 (2010-03-13) successfully loaded. See ?R.methodsS3 for
help.> R.oo v1.7.2 (2010-04-13) successfully loaded. See ?R.oo for help.
> R.matlab v1.3.1 (2010-04-20) successfully loaded. See ?R.matlab for help.
> > f <- readMat("freq.mat")
> Error: cannot allocate vector of size 296.5 Mb
>
> On the other hand, if I save the same data in ascii format (from
> octave: "save -text"), resulting in a 75MB file, then I can load
it
> without problems with the read.octave() function from package foreign.
> Is this a known issue or am I doing something wrong? My R version is:
This is not a package I'm particularly familiar with, but:
what commands did you use to save the file in octave? Based on
'help save' I think that 'save' by default would get you an
octave
format file ... you might have to do some careful reading in
?readMat (in R) and 'help save' (in octave) to figure out the
correspondence between octave/MATLAB and R/MATLAB.
If possible, try saving a small file and see if it works; if
you still don't know what's going on, post that file somewhere for
people to try.
I was able to
save -6 "save.mat" in octave and
readMat("save.mat") in R successfully,
saving a vector of integers from 1 to 1 million (which
took about 7.7 Mb)
------------------------------
Message: 46
Date: Mon, 20 Dec 2010 07:07:35 +0100
From: Luca Meyer <lucam1968@gmail.com>
To: R-help@r-project.org
Subject: [R] tabulating 2 factors weighting by a third var
Message-ID: <AB89CA50-A9F4-4F62-98B5-B3A5A41D3B92@gmail.com>
Content-Type: text/plain; charset=us-ascii
Hi,
This must be an easy one but so far I haven't find a way out...
I have a data frame such as:
$ v1 : Factor w/ 5 levels
$ v2 : Factor w/ 2 levels
$ v3 : Class 'difftime' atomic [1:6666]
basically v1 and v2 are factors, while v3 is a variable containing the
duration of certain activities (values ranging from 11 to 45000 sec, no
missing values)
How can I get a table such that v1 levels will show as rows, v2 levels as
columns and v3 is the weight by which table(v1,v2) is weighted? That is,
instead of getting the count of occurences in each of the 10 cells of
table(v1,v2) I would like to get the sum(v3), how can it be done?
Thanks,
Luca
Luca Meyer
www.lucameyer.com
IBM SPSS Statistics release 19.0.0
R version 2.12.1 (2010-12-16)
Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0
------------------------------
Message: 47
Date: Mon, 20 Dec 2010 17:41:20 +1100
From: Kohleth Chia <kohleth@gmail.com>
To: r-help@r-project.org
Subject: [R] package "arules" - 'transpose' of the
transactions
Message-ID:
<AANLkTimmhuJ+GnThVJVT8yoMF3C5sqorkXCVGb3UT7mc@mail.gmail.com>
Content-Type: text/plain
Suppose this is my list of transactions:
set.seed(200)
tran=random.transactions(100,3)
inspect(tran)
items transactionID
1 {item80} trans1
2 {item8,
item20} trans2
3 {item28} trans3
I want to get the 'transpose' of the data, i.e.
transactionID items
1 {trans2} item8
2 {trans2} item20
3 {trans3} item28
4 {trans1} item80
I tried converting tran into a matrix, then transpose it, then convert it
back to transactions. But my dataset is actually very very large, so I
wonder if there is any faster method?
Thanks
--
KC
[[alternative HTML version deleted]]
------------------------------
Message: 48
Date: Mon, 20 Dec 2010 07:43:55 +0100 (MET)
From: Gerrit Eichner <Gerrit.Eichner@math.uni-giessen.de>
To: Luca Meyer <lucam1968@gmail.com>
Cc: R-help@r-project.org
Subject: Re: [R] tabulating 2 factors weighting by a third var
Message-ID:
<Pine.SOC.4.64.1012200742340.9098@solcom.hrz.uni-giessen.de>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed
Hi, Luca,
if V is you data frame, maybe
with( V, tapply( v3, list( v1, v2), sum))
does what you want.
Hth -- Gerrit
On Mon, 20 Dec 2010, Luca Meyer wrote:
> Hi,
>
> This must be an easy one but so far I haven't find a way out...
>
> I have a data frame such as:
>
> $ v1 : Factor w/ 5 levels
> $ v2 : Factor w/ 2 levels
> $ v3 : Class 'difftime' atomic [1:6666]
>
> basically v1 and v2 are factors, while v3 is a variable containing the
> duration of certain activities (values ranging from 11 to 45000 sec, no
> missing values)
>
> How can I get a table such that v1 levels will show as rows, v2 levels
> as columns and v3 is the weight by which table(v1,v2) is weighted? That
> is, instead of getting the count of occurences in each of the 10 cells
> of table(v1,v2) I would like to get the sum(v3), how can it be done?
>
> Thanks,
> Luca
>
> Luca Meyer
> www.lucameyer.com
> IBM SPSS Statistics release 19.0.0
> R version 2.12.1 (2010-12-16)
> Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0
>
> ______________________________________________
> R-help@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.
------------------------------
Message: 49
Date: Mon, 20 Dec 2010 08:10:36 +0100
From: Petr PIKAL <petr.pikal@precheza.cz>
To: Luca Meyer <lucam1968@gmail.com>
Cc: R-help@r-project.org
Subject: [R] Odp: tabulating 2 factors weighting by a third var
Message-ID:
<OFE9BA6049.C3483613-ONC12577FF.002703C6-C12577FF.00277B70@precheza.cz>
Content-Type: text/plain; charset="US-ASCII"
Hi
r-help-bounces@r-project.org napsal dne 20.12.2010 07:07:35:
> Hi,
>
> This must be an easy one but so far I haven't find a way out...
>
> I have a data frame such as:
>
> $ v1 : Factor w/ 5 levels
> $ v2 : Factor w/ 2 levels
> $ v3 : Class 'difftime' atomic [1:6666]
>
> basically v1 and v2 are factors, while v3 is a variable containing the
> duration of certain activities (values ranging from 11 to 45000 sec, no
missing values)>
> How can I get a table such that v1 levels will show as rows, v2 levels
as > columns and v3 is the weight by which table(v1,v2) is weighted? That is,
> instead of getting the count of occurences in each of the 10 cells of
table> (v1,v2) I would like to get the sum(v3), how can it be done?
xtabs(v3~v1+v2, data=your.data.frame)
is other option.
Regards
Petr
>
> Thanks,
> Luca
>
> Luca Meyer
> www.lucameyer.com
> IBM SPSS Statistics release 19.0.0
> R version 2.12.1 (2010-12-16)
> Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0
>
> ______________________________________________
> R-help@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.
------------------------------
Message: 50
Date: Mon, 20 Dec 2010 09:41:13 +0100
From: Jon Olav Skoien <jon.skoien@jrc.ec.europa.eu>
To: Duncan Murdoch <murdoch.duncan@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] install.packages() - old version deleted, new version
did not install
Message-ID: <4D0F16A9.8090108@jrc.ec.europa.eu>
Content-Type: text/plain; charset=windows-1252; format=flowed
On 12/17/2010 6:22 PM, Duncan Murdoch wrote:> On 17/12/2010 11:13 AM, Jon Olav Skoien wrote:
>> Dear list,
>>
>> (R 2.12.0, Windows 7, 64bit)
>>
>> I recently tried to install a new package ("spacetime"), that
depends on
>> "sp" among others. I already had the last one installed, but
there was
>> probably a newer version on CRAN, so the command
>> > install.packages("spacetime")
>> also gave me:
>> also installing the dependencies ?sp?, ?zoo?, ?xts?
>>
>> sp was already loaded in this session, so installation failed:
>> package 'sp' successfully unpacked and MD5 sums checked
>> Warning: cannot remove prior installation of package 'sp'
>>
>> Unfortunately, the warning should rather say:
>> "cannot completely remove prior installation of package
'sp'"
>> R managed to remove most of the prior installation of sp, except for
the
>> .dll. I could go on using sp in the existing sessions, but not load the
>> package in a new session or open the help pages. This has happened to
me
>> several times, and the only solution I have found to this is to close
>> all R-sessions and install the package again. This is normally ok, but
>> this time I had some long-time computations running in another
R-session
>> that I did not want to interrupt. For the next time, is there a way to
>> reinstall a package without interrupting running R-sessions?
>>
>> For me it seems like the cause of the problem could have been solved by
>> checking if the .dll can be removed before removing the rest of the
>> package, by adding something like the following in
utils:::unpackPkgZip?
>> if (unlink(paste(instPath,"/libs/x64/sp.dll", sep =
"")) != 0)
>> warning("cannot remove...")
>> before
>> ret<- unlink(instPath, recursive = TRUE) (line 95)
>> x64 in the path would have to be changed to something architecture
>> dependent...
>
>
> Could you try out the new 2.12.1 release? I recall hearing that
> something like this had changed, but I can't spot the NEWS item right
> now.
>
> Duncan Murdoch
It seems it didnt change yet...
I installed 2.12.1 (on a different computer, still Windows, but Vista
and 32 bit), and after installing and loading sp in one session, I
opened a new session and got:
R version 2.12.1 (2010-12-16)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: i386-pc-mingw32/i386 (32-bit)
............................
> install.packages("sp")
Installing package(s) into ?C:\Users\Jon\Documents/R/win-library/2.12?
(as ?lib? is unspecified)
--- Please select a CRAN mirror for use in this session ---
provo con l'URL
'http://cran.at.r-project.org/bin/windows/contrib/2.12/sp_0.9-76.zip'
Content type 'application/zip' length 997444 bytes (974 Kb)
URL aperto
downloaded 974 Kb
package 'sp' successfully unpacked and MD5 sums checked
Warning: cannot remove prior installation of package 'sp'
The downloaded packages are in
C:\Users\Jon\AppData\Local\Temp\RtmpCTJeBk\downloaded_packages
> library(sp)
Errore in library(sp) : non c'? alcun pacchetto chiamato 'sp'
>
The error message is the same as earlier, there is no package called
"sp", the attempt to install it again removed the old version except
for
the .dll.
Jon
------------------------------
Message: 51
Date: Mon, 20 Dec 2010 06:54:42 +0100
From: Luca Meyer <lucam1968@gmail.com>
To: Uwe Ligges <ligges@statistik.tu-dortmund.de>
Cc: R-help@r-project.org
Subject: Re: [R] Alternative to extended recode sintax? Bug?
Message-ID: <DEC1F69D-97C6-417A-8706-EBB8E745F01C@gmail.com>
Content-Type: text/plain; charset=iso-8859-1
All right, I get it now: lubridate's week() define weeks from Thursday till
the following Wednesday. You'd probably agree with me that it's a bit
strange what it is going to do over the turn of the year:
> y <-
as.POSIXct(c("2010-12-27","2010-12-28","2010-12-29","2010-12-30","2010-12-31
","2011-01-01","2011-01-02","2011-01-03","2011-01-04","2011-01-05","2011-01-
06","2011-01-07","2011-01-08","2011-01-09","2011-01-10","2011-01-11","2010-0
1-12","2010-01-13","2010-01-14"))> week(y)
[1] 52 52 52 53 53 1 1 1 1 1 1 2 2 2 2 2 2 2 3
Why would the first week of the year be made of 6 days and the turn from
week 1 to week 2 on the night between Thursday and Friday and not Wednesday
and Friday like every other week?
Cheers,
Luca
Il giorno 19/dic/2010, alle ore 18.14, Uwe Ligges ha scritto:
>
>
> On 19.12.2010 13:20, David Winsemius wrote:
>>
>> On Dec 19, 2010, at 5:11 AM, Luca Meyer wrote:
>>
>>> Something goes wrong with the week function of the lubridate
package:
>>>
>>>> x= as.POSIXct(factor(c("2010-12-15 17:28:27",
>>> + "2010-12-15 17:32:34",
>>> + "2010-12-15 18:48:39",
>>> + "2010-12-15 19:25:00",
>>> + "2010-12-16 08:00:00",
>>> + "2010-12-16 08:25:49",
>>> + "2010-12-16 09:00:00")))
>>>> require(lubridate)
>>
>>>> weekdays(x)
>>> [1] "Mercoled?" "Mercoled?"
"Mercoled?" "Mercoled?" "Gioved?"
>>> "Gioved?" "Gioved?"
>>>> week(x)
>>> [1] 50 50 50 50 51 51 51
>>
>> But 2010-12-15 is a Wednesday and 2010-12-16 is a Thursday.
>>
>
>
> Together with the description of ?week this shows that lubridate's
week()
function works as documented rather than as expected by Luca
Meyer.>
> Uwe Ligges
------------------------------
Message: 52
Date: Mon, 20 Dec 2010 01:29:03 -0800 (PST)
From: Roslina Zakaria <zroslina@yahoo.com>
To: r-help@r-project.org
Subject: [R] R is not running well
Message-ID: <136427.51206.qm@web120505.mail.ne1.yahoo.com>
Content-Type: text/plain
Hi,
I'm working on windows 7. Recently I install the latest R and also use
together
with Tinn-R but it is not working well. I got the following message. I
have
tried saving the Rprofile as suggested by the R forum, but still it is not
running. What should I do?
> source(.trPaths[5], echo=TRUE, max.deparse.length=150)
Error in source(.trPaths[5], echo = TRUE, max.deparse.length = 150) :
object '.trPaths' not found
[[alternative HTML version deleted]]
------------------------------
Message: 53
Date: Mon, 20 Dec 2010 09:36:18 -0000
From: "ying zhang" <ying.zhang@struq.com>
To: <r-help@r-project.org>
Subject: [R] incremental learning for LOESS time series model
Message-ID: <002101cba029$5d664d80$1832e880$@struq.com>
Content-Type: text/plain
Hi All,
I am currently working on some time series data, I know I can use
LOESS/ARIMA model.
The data is written to a vector whose length is 1000, which is a queue,
updating every 15 minutes,
Thus the old data will pop out while the new data push in the vector.
I can rerun the whole model on a scheduler, e.g. retrain the model every 15
minutes, that is, Use the whole 1000 value to train
The LOESS model, However it is very inefficient, as every time only one
value is insert while another 999 vlaues still same as last time.
So how can I achieve better performance?
Many thanks
Ying
[[alternative HTML version deleted]]
------------------------------
Message: 54
Date: Mon, 20 Dec 2010 10:57:01 +0100
From: Uwe Ligges <ligges@statistik.tu-dortmund.de>
To: Luca Meyer <lucam1968@gmail.com>
Cc: R-help@r-project.org
Subject: Re: [R] Alternative to extended recode sintax? Bug?
Message-ID: <4D0F286D.4000900@statistik.tu-dortmund.de>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 20.12.2010 06:54, Luca Meyer wrote:> All right, I get it now: lubridate's week() define weeks from Thursday
till the following Wednesday. You'd probably agree with me that it's a
bit
strange what it is going to do over the turn of the
year:>
>> y<-
as.POSIXct(c("2010-12-27","2010-12-28","2010-12-29","2010-12-30","2010-12-31
","2011-01-01","2011-01-02","2011-01-03","2011-01-04","2011-01-05","2011-01-
06","2011-01-07","2011-01-08","2011-01-09","2011-01-10","2011-01-11","2010-0
1-12","2010-01-13","2010-01-14"))>> week(y)
> [1] 52 52 52 53 53 1 1 1 1 1 1 2 2 2 2 2 2 2 3
>
> Why would the first week of the year be made of 6 days and the turn from
week 1 to week 2 on the night between Thursday and Friday and not Wednesday
and Friday like every other week?
Well, it's the definition in that week() function from that package, if
you don't like that definition, choose another one. I have not said that
I like it, just that it seems to work as documented.
Uwe
> Cheers,
> Luca
>
>
>
> Il giorno 19/dic/2010, alle ore 18.14, Uwe Ligges ha scritto:
>
>>
>>
>> On 19.12.2010 13:20, David Winsemius wrote:
>>>
>>> On Dec 19, 2010, at 5:11 AM, Luca Meyer wrote:
>>>
>>>> Something goes wrong with the week function of the lubridate
package:
>>>>
>>>>> x= as.POSIXct(factor(c("2010-12-15 17:28:27",
>>>> + "2010-12-15 17:32:34",
>>>> + "2010-12-15 18:48:39",
>>>> + "2010-12-15 19:25:00",
>>>> + "2010-12-16 08:00:00",
>>>> + "2010-12-16 08:25:49",
>>>> + "2010-12-16 09:00:00")))
>>>>> require(lubridate)
>>>
>>>>> weekdays(x)
>>>> [1] "Mercoled?" "Mercoled?"
"Mercoled?" "Mercoled?" "Gioved?"
>>>> "Gioved?" "Gioved?"
>>>>> week(x)
>>>> [1] 50 50 50 50 51 51 51
>>>
>>> But 2010-12-15 is a Wednesday and 2010-12-16 is a Thursday.
>>>
>>
>>
>> Together with the description of ?week this shows that lubridate's
week()
function works as documented rather than as expected by Luca
Meyer.>>
>> Uwe Ligges
>
------------------------------
Message: 55
Date: Mon, 20 Dec 2010 02:09:25 -0800 (PST)
From: Dieter Menne <dieter.menne@menne-biomed.de>
To: r-help@r-project.org
Subject: Re: [R] Layout of mulitpage conditioned lattice plots
Message-ID: <1292839765053-3095284.post@n4.nabble.com>
Content-Type: text/plain; charset=us-ascii
David Winsemius wrote:>
>
> Here's my latest guess at what you may want:
>
> pdf(file="multpage.pdf")
> xyplot(val~time|subj + comp, data=dt,type="l",
> layout=c(3,5, 3),
> skip=rep(c(rep(FALSE,13), TRUE, TRUE), 3) )
> dev.off()
>
>
Not really, but "skip" was the right idea. I added another idea of
Deepayan
from a cited thread, first to plot all, then to update indexed parts with a
computed skip.
The code has become a bit lengthy because I added a more flexible
orphan-avoiding scheme.
Dieter
library(lattice)
# Distribute panels on page, so that each panel has the same size,
# even on last page
# Use adjustCol to adjust colPerPage to avoid orphans on the last page
#
# --------------------- adjustedColPerPage
-------------------------------------
adjustedColPerPage = function(colPerPage, ncols){
# Allow for 20% or plus/minus 2
searchRange = max(2L,as.integer(colPerPage*0.2))
colsPerPage = (colPerPage-searchRange):(colPerPage+searchRange)
nColLast = ncols %% colsPerPage
nPages = (ncols %/% colsPerPage)+ as.integer(nColLast!=0)
# Prefer solution with equal number on a page
matchPage = which(nColLast==0)
if (length(matchPage) >0) {
colsPerPage[matchPage[which.min(abs(matchPage-searchRange))]]
} else {
colsPerPage[which.max(nColLast)] # not perfect
}
}
# --------------------- xyPaged
----------------------------------------------
xyPaged = function(x, adjustCol = FALSE, colPerPage = 5,main=NULL) {
nrows = nlevels(x$comp) # This is not very general
ncols = nlevels(x$subj) #
if (adjustCol) # try to get an alternative layout that fits the pages
better
{
colPerPage = adjustedColPerPage(colPerPage,ncols)
main = paste(main," usedCol= ",colPerPage)
}
p = xyplot(val~time|subj+comp, data=x,type="l",
layout = c(colPerPage,nrows),main=main)
# http://r-project.markmail.org/thread/rcztoawll5kduw4x
page = 1
for (fromCol in seq(1,ncols,by=colPerPage)){
toCol = min(fromCol+colPerPage-1,ncols)
showCol = toCol %% colPerPage
skip = rep(FALSE,colPerPage)
if (showCol != 0) skip[(showCol+1):colPerPage] = TRUE
print(update(p[fromCol:toCol],skip=skip,sub=page))
page = page +1
}
}
# Test
testFrame = expand.grid(adjustCol=c(FALSE,TRUE),
nsubj=c(5,11,13),colPerPage=c(5,9,14) )
pdf(file="multpage.pdf")
for (i in 1:nrow(testFrame)) {
test = testFrame[i,]
dt = expand.grid(time=1:20,comp=LETTERS[1:3],subj=letters[1:test$nsubj])
dt$val = rnorm(nrow(dt))
with (test, xyPaged(dt,adjustCol, colPerPage,
main=paste("nsubj=",test$nsubj, " requestedCol=
",colPerPage)))
}
dev.off()
--
View this message in context:
http://r.789695.n4.nabble.com/Layout-of-mulitpage-conditioned-lattice-plots-
tp3094581p3095284.html
Sent from the R help mailing list archive at Nabble.com.
------------------------------
Message: 56
Date: Mon, 20 Dec 2010 21:35:27 +1100
From: Jim Lemon <jim@bitwrit.com.au>
To: fransiepansiekevertje <fransiepansiekevertje@digipsy.nl>
Cc: R-help@r-project.org
Subject: Re: [R] barplot: width of label
Message-ID: <4D0F316F.90501@bitwrit.com.au>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
On 12/20/2010 02:13 AM, fransiepansiekevertje wrote:> Hello,
> I try to make barplots with rather wide labels. A simplified example of
> this:
>
> x<- c(12, 33, 56, 67, 15, 66)
> names(x)<- c('Richard with a long surname','Minnie with a
long
> name,'Albert','Helen','Joe','Kingston')
> barplot(x, las = 2)
>
> Now the label 'Richard with a long surname' is too long to fit
beneath the
> bars. A simple solution would be enlarge the space for the labels by
> positioning the bar region higher. But I cannot find how to do this.
Please> Help!
>
Hi Frans,
Does this do what you want?
library(plotrix)
barp(x,names.arg=names(x),staxx=TRUE)
Jim
------------------------------
Message: 57
Date: Mon, 20 Dec 2010 08:36:02 -0200
From: Andre Nathan <andre@digirati.com.br>
To: r-help@r-project.org
Subject: [R] contourplot help
Message-ID: <1292841362.8045.8.camel@andre.mz.digirati.com.br>
Content-Type: text/plain; charset="UTF-8"
Hello
I'm using the following call to create a contourplot:
library(lattice)
m <- as.matrix(read.table("data.txt"))
contourplot(m[,3] ~ m[,2] * -m[,1],
at = c(1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1),
scales = list(x = list(log = 10,
labels = c("1", "10",
"100"),
at = c(1, 10, 100)),
y = list(labels = c("14", "12",
"10", "8",
"6", "4",
"2")),
at = c(-14, -12, -10, -8, -6, -4,
-2)),
labels = c(expression(10^-6), expression(10^-5),
expression(10^-4), expression(10^-3),
expression(10^-2), expression(10^-1),
expression(10^0)),
xlim = c(0.75, 10^2),
xlab = "Out-degree", ylab = "In-degree")
Which gives the the output in the file below
http://ompldr.org/vNm4xag/contour.eps
As it can be seen, the level labels are not displayed nicely because
there's not enough room for them. Also, the 10^-1 label is not
displayed.
Is there a way for me to hardcode the position of each label? I tried
setting labels = F and then calling text() for each one, but that
doesn't work.
If that's not possible, one option would be to color each level line
differently and then add a legend. Is it possible to do that?
Finally, how can I remove the tick marks from the top and right axes?
Thanks,
Andre
------------------------------
Message: 58
Date: Mon, 20 Dec 2010 11:48:51 +0100 (CET)
From: "Anne-Christine Mupepele" <anne-chr.afs@web.de>
To: r-help@r-project.org
Subject: [R] For-loop
Message-ID:
<1161484091.1743851.1292842131649.JavaMail.fmail@mwmweb052>
Content-Type: text/plain; charset=UTF-8
Hi,
I have the following problem:
I have a data.frame with 36 sample sites (colums) for which I have
covariates in 3 categories: Area, Month and River. Each Area consists of 3
rivers, which were sampled over 3 month. Now I want to fuse River 1-3 for
one area in one month. To get a data.frame with 12 colums.
I am trying to do a "for loop" (which may be a complicated solution,
but I
don't see an easier way), which is not working, apparently because a[,ij] or
a[,c(i,j)] is not working as a definition of the matrix with a double
condition in the colums.
How can I make it work or what would be an easier solution?
Thank you for your help,
Anne
data=data.frame(matrix(1:99,nrow=5,ncol=36))
colnames(data)=c(paste("plot",1:36))
cov=data.frame(rep(1:3,12),c(rep("Jan",12),rep("Feb",12),rep("Mar",12)),rep(
c(1,1,1,2,2,2,3,3,3,4,4,4),3))
dimnames(cov)=list(colnames(data),c("River","Month","Area"))
###loop###
a=matrix(nrow=dim(data)[1],ncol=length(levels(factor(cov$Month)))*length(lev
els(factor(cov$Area))))
for(i in 1:length(levels(factor(cov$Month))))
{
for(j in 1:length(levels(factor(cov$Area))))
{
a[,ij]=as.numeric(rowSums(data[,factor(cov$Month)==levels(factor(cov$Month))
[i]&factor(cov$Area)==levels(factor(cov$Area))[j]]))
}
}
------------------------------
_______________________________________________
R-help@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.
End of R-help Digest, Vol 94, Issue 20
**************************************
[[alternative HTML version deleted]]