Skip to content

how to eliminate first row in datafile created in do loop

4 messages · Ian Gow, Graves, Gregory

#
I am trying to create a routine that would take a time series and
generate monthly 25%tile and 75%tile limits based on 12 calendar months.
I have succeeded to create a do loop to do this, but can't figure out
how to initiate the receiving datafile (in this case "limit.list")
without sticking an initial set of zeroes in.  I'd like to be able to
insert a new column "i.value" for each time "i" increments 1 thru 12, to
represent the 12 months, but I can't figure out how to join the value of
i to the "lower" and "upper" variables.  Close but no cigar here.

lower=0
upper=0
limit.list<-data.frame(lower,upper)
for(i in 1:12){
	s=subset(paleo,month==i);
        one.month=with(s, quantile(WBpaleo, c(.25, .75), na.rm=T));
        limit.list<-rbind(limit.list,one.month)  
}
limit.list

which gives me this:
lower    upper
1   0.00000  0.00000
2  25.51786 29.68823
3  26.09828 30.92770
4  27.75703 33.04016
5  29.53395 36.14273
6  30.89420 37.65727
7  29.27843 37.59690
8  27.50141 34.36265
9  26.40554 32.88533
10 25.13494 29.83829
11 23.48982 27.14612
12 22.88814 27.52782
13 24.51789 28.24871

How do I avoid having row 1 in my list?

I can delete the first row with:
limit.list = limit.list[-1,]

but that messes up my month variable
lower    upper
2  25.51786 29.68823
3  26.09828 30.92770
4  27.75703 33.04016
5  29.53395 36.14273
6  30.89420 37.65727
7  29.27843 37.59690
8  27.50141 34.36265
9  26.40554 32.88533
10 25.13494 29.83829
11 23.48982 27.14612
12 22.88814 27.52782
13 24.51789 28.24871
#
Gregory: Would setting limit.list <- NULL at the start do the trick? See
example below:

Analogue of your example:

lower <- 0
upper <- 0
limit.list<-data.frame(lower,upper)
for(i in 1:12) {
    some.data <- rnorm(2)
   lower <- min(some.data)
  upper <- max(some.data)
  one.month <- data.frame(lower, upper)
  limit.list <- rbind(limit.list, one.month)
}
limit.list

Approach that doesn't give zeroes at the start:

limit.list <- NULL
for(i in 1:12) {
    some.data <- rnorm(2)
  lower <- min(some.data)
  upper <- max(some.data)
  one.month <- data.frame(lower, upper)
  limit.list <- rbind(limit.list, one.month)
}
limit.list


-Ian
On 5/24/11 7:30 AM, "Graves, Gregory" <ggraves at sfwmd.gov> wrote:

            
#
Certainly closer, and I can get by with this ... but ... would like to
not have the "one.month"s .

limit.list<-NULL
for(i in 1:12){
	s=subset(paleo,month==i);
        one.month=with(s, quantile(WBpaleo, c(.25, .75), na.rm=T));
        one.month=c(as.character(i),one.month);
        limit.list<-rbind(limit.list,one.month)  
}
25%             75%            
one.month "1"  "25.51786389"   "29.68823492"  
one.month "2"  "26.09827974"   "30.9276985"   
one.month "3"  "27.757034405"  "33.040163045" 
one.month "4"  "29.53395155"   "36.1427293975"
one.month "5"  "30.894195075"  "37.657271835" 
one.month "6"  "29.27843098"   "37.59689852"  
one.month "7"  "27.5014142975" "34.36265367"  
one.month "8"  "26.4055425"    "32.88533117"  
one.month "9"  "25.1349430375" "29.838291435" 
one.month "10" "23.48982439"   "27.14611846"  
one.month "11" "22.88814497"   "27.52782293"  
one.month "12" "24.5178851225" "28.248711925"


-----Original Message-----
From: Ian Gow [mailto:iandgow at gmail.com] 
Sent: Tuesday, May 24, 2011 8:58 AM
To: Graves, Gregory; Nungesser, Martha; r-help at r-project.org
Cc: Kemp, Susan K SAJ; patrick_pitts at fws.gov
Subject: Re: [R] how to eliminate first row in datafile created in do
loop

Gregory: Would setting limit.list <- NULL at the start do the trick? See
example below:

Analogue of your example:

lower <- 0
upper <- 0
limit.list<-data.frame(lower,upper)
for(i in 1:12) {
    some.data <- rnorm(2)
   lower <- min(some.data)
  upper <- max(some.data)
  one.month <- data.frame(lower, upper)
  limit.list <- rbind(limit.list, one.month)
}
limit.list

Approach that doesn't give zeroes at the start:

limit.list <- NULL
for(i in 1:12) {
    some.data <- rnorm(2)
  lower <- min(some.data)
  upper <- max(some.data)
  one.month <- data.frame(lower, upper)
  limit.list <- rbind(limit.list, one.month)
}
limit.list


-Ian
On 5/24/11 7:30 AM, "Graves, Gregory" <ggraves at sfwmd.gov> wrote:

            
months.
to
of
#
Gregory:

My suggestion omitted the "i.value" column you'd mentioned, but the
version below includes it. Note, the row names are simpler in this variant
than in yours (I assume that's where the "one.months" are showing up).
+     some.data <- rnorm(2)
+   lower <- min(some.data)
+   upper <- max(some.data)
+   one.month <- data.frame(i.value=i, lower, upper)
+   limit.list <- rbind(limit.list, one.month)
+ }
i.value        lower      upper
1        1 -0.006352885  0.1894010
2        2 -1.457983914 -0.5069830
3        3 -0.106285443  0.4837501
4        4 -1.400419065  1.1374100
5        5 -1.332467138 -0.2266373
6        6  0.652510501  1.8829133
7        7 -1.630929209  0.1456977
8        8 -0.424020281  0.3734382
9        9  0.028015806  1.1767302
10      10  0.380922792  0.9623795
11      11  1.026495439  1.6750991
12      12  0.194633578  0.6910900
On 5/24/11 10:38 AM, "Graves, Gregory" <ggraves at sfwmd.gov> wrote: