[R] Custom Sort on a Table object

2011-06-06 Thread Galen Moore
Greetings - 

 

I've got the following table (the result of a two-way table operation):

 

  f m

  0 to 5  11.328000  6.900901

  15 to 24 6.100570  5.190058

  25 to 34 9.428707  6.567280

  35 to 4410.462158  7.513270

  45 to 54 7.621988  5.692905

  5 to 14  6.502741  6.119663

  55 to 64 5.884737  4.319905

  65 to 74 5.075606  4.267810

  75 to 84 4.702020  3.602362

  85 and over  4.75  3.877551

 

Which I'd like to sort so that the column of rownames (which represent age
bands) and their corresponding f and m values appear in logical order.  I've
tried a bunch of things; merging with a separate df bearing Age Bands paired
with a sequence number, stripping out row vectors and rbind-ing a new df,
etc., all to no avail.  It seems to be very difficult to spin a table object
into a data frame without being stuck with the tables rownames!

 

I haven't yet tried writing to an external file and then reading it back (so
as to get R to forget that it's a Table object), and then merging on the
group bands to pull in a sequence vector upon which to do an order().  Seems
like it should be easier. 

 

Many thanks,

 

Galen 


[[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.


Re: [R] Adding a line to a beside=TRUE barplot

2011-06-03 Thread Galen Moore
Many thanks, it works beautifully!  I'd not caught the use of colMeans() as
the path to useable x-coordinates, and was fixated on the existing
bargraph's x-vector for this purpose.

Ref your Q, I guess I had the lattice reference in there as a vestige of
other attempts to get the job done with barchart i/o barplot.

Galen

-Original Message-
From: Peter Ehlers [mailto:ehl...@ucalgary.ca] 
Sent: Friday, June 03, 2011 02:45
To: galen.a.mo...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Adding a line to a beside=TRUE barplot

On 2011-06-02 13:09, Galen Moore wrote:
 Greetings –



 Grateful for any help on this one:



 In the following demo code, I am trying to get the points in the line 
 to appear over the same x-axis labels as are used by the paired Bars.  
 It appears, however, that R/lattice ignores the x-axis points used by 
 the bars and plots the x points for the line at ½ points.



 Can you help me tweak this code so that the nth bump in the line 
 appears over the same nth pair of bars?  I’m open to any options 
 besides lattice/barplot.



 library(lattice)

 aa- abs(rnorm(c(1:10)))*5

 bb- abs(rnorm(c(1:10)))*5

 cc- abs(rnorm(c(1:10)))*5



 dd- as.matrix(cbind(aa, bb))



 barplot(t(dd), beside=TRUE, ylim=c(0,10))

 lines(cc)

What's lattice doing here? barplot() is not a lattice function.

Careful reading of the help page for barplot and study of the examples will
lead you to:

   mp - barplot(t(dd), beside=TRUE)
   mp
   lines(colMeans(mp), cc, col=2, lwd=3)


Peter Ehlers




 Many thanks,



 Galen


   [[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.


[R] Adding a line to a beside=TRUE barplot

2011-06-02 Thread Galen Moore
Greetings – 

 

Grateful for any help on this one:

 

In the following demo code, I am trying to get the points in the line to
appear over the same x-axis labels as are used by the paired Bars.  It
appears, however, that R/lattice ignores the x-axis points used by the bars
and plots the x points for the line at ½ points.

 

Can you help me tweak this code so that the nth bump in the line appears
over the same nth pair of bars?  I’m open to any options besides
lattice/barplot. 

 

library(lattice)

aa - abs(rnorm(c(1:10)))*5

bb - abs(rnorm(c(1:10)))*5

cc - abs(rnorm(c(1:10)))*5

 

dd - as.matrix(cbind(aa, bb))

 

barplot(t(dd), beside=TRUE, ylim=c(0,10))

lines(cc)

 

Many thanks,

 

Galen


[[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.


Re: [R] DateTime Math in R - POSIXct

2011-05-31 Thread Galen Moore
Many thank, Jeff  – 

a)  I am indeed on Windoze, and found the time zone directory under: C:\Program 
Files\R\R-2.13.0\share\zoneinfo
b)  I applied Sys.setenv(TZ=America/Denver) to my real program, and the 
numerical and POSIXct output representations of my date/times now work, thanks! 
I've avoided converting back to numeric from POSIXct in my real program.  I 
left my calc'd variables in numeric mode so as to avoid POSIXct surprises.
c)  Oddly, however, my dummy/demo code below still shows some screwy behavior, 
even with Sys.setenv(TZ=America/Denver) applied.  I'm guessing that the 
apparent reversion to GMT occurs in my statement  dateN - as.numeric(dateP) 
below is merely the internal representation to which you refer.   I don't 
understand why POSIXct slaps another hour on dateN on conversion to dateP, nor 
why R generates the MDT timezone value (not listed and not valid tz 
designation).

Thanks again for your help, and grateful for any further hints.  I'd really 
like to be not confused by this approach to datetime arithmetic in R.

- Galen

 Sys.setenv(TZ=America/Denver)
 
 tstamp - 2011-05-22 11:45:00
 mode(tstamp)
[1] character
 
 dateP - as.POSIXct(tstamp, origin=1970-01-01)
 mode(dateP)
[1] numeric
 dateP
[1] 2011-05-22 11:45:00 MDT
 # Denver is currently on MDT, and note that R generates the MDT designation,
 # even though it is not listed in C:\Program Files\R\R-2.13.0\share\zoneinfo
 
 dateN - as.numeric(dateP)
 dateN
[1] 1306086300
 # 1306086300  returns 5/22/2011 17:45:00 when converted manually
 
 dateP2 - as.POSIXct(dateN, origin=1970-01-01)
 dateP2
[1] 2011-05-22 18:45:00 MDT
 # Where does the extra hour come from?


From: Jeff Newmiller [mailto:jdnew...@dcn.davis.ca.us] 
Sent: Tuesday, May 31, 2011 00:43
To: galen.a.mo...@gmail.com; 'David Winsemius'
Cc: r-help@r-project.org
Subject: Re: [R] DateTime Math in R - POSIXct

MDT is not a valid timezone specification... it is falling back on your default 
timezone in your working case.

a) learn what valid timezone specifications are on your operating system. I 
suspect you are using Windows (hint: we should not have to guess), so look for 
the timezone directory in your R installation directory. I would guess that you 
want America/Denver. Note that MST and MST7MDT are also possibilities , but 
they are generally deprecated as being too imprecise in the global context (who 
is to say MDT doesn't mean Mongolian Daylight Time).

b) I strongly recommend using the default timezone or overriding it with 
Sys.setenv(TZ= your timezone spec) rather than using the tz arguments. The 
frustration factor is much lower.

c) if you properly import all of your time values into POSIXct, their internal 
representations will always be referenced to UTC (GMT). It is only on input and 
output (such as printing to the console) or conversion to POSIXlt that the 
timezone is considered.
---
Jeff Newmiller The . . Go Live...
DCN:jdnew...@dcn.davis.ca.us Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
--- 
Sent from my phone. Please excuse my brevity.
Galen Moore galen.a.mo...@gmail.com wrote:
Thank you, David.

I am not subtracting the seconds to convert POSIXct's GMT to MDT, and don't
understand why I should need to.

Any hints, however, as to why  
dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)  
returns a date in the correct tz in my first instance below, yet 

dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)  
decides to revert to GMT in my second instance below?


Thanks,

Galen


-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: Monday, May 30, 2011 20:31
To: galen.a.mo...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] DateTime Math in R - POSIXct


On May 30, 2011, at 10:20 PM, Galen Moore wrote:

 Greetings -



 I'm battling
 g
POSIXct, as per the code below.  My input is actually an 
 XL file, but the weird results below correctly model what I am seeing 
 in my program.



 Before I punt and use lubridate or timeDate, could anyone please help 
 me understand why POSIXct forces my variable back to GMT?



 I suspect that I'm not properly coding the tzone value, but it does 
 not throw an error as-is.





 tstamp - 2011-05-22 11:45:00 MDT

 mode(tstamp)

 [1] character



 dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)

 mode(dateP)

 [1] numeric

 dateP

 [1] 2011-05-22 11:45:00 MDT



 dateN -
as.numeric(dateP)

 dateN

 [1] 1306086300


So now the internal representation is referenced to GMT



 dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)

 dateP2

And you are telling the function to add as many hours offset as needed for
the difference between GMT and MD T.

--
David.

 [1] 2011-05-22 18:45

[R] DateTime Math in R - POSIXct

2011-05-30 Thread Galen Moore
Greetings - 

 

I'm battling POSIXct, as per the code below.  My input is actually an XL
file, but the weird results below correctly model what I am seeing in my
program.

 

Before I punt and use lubridate or timeDate, could anyone please help me
understand why POSIXct forces my variable back to GMT?

 

I suspect that I'm not properly coding the tzone value, but it does not
throw an error as-is.

 

 

 tstamp - 2011-05-22 11:45:00 MDT

 mode(tstamp)

[1] character

 

 dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)

 mode(dateP)

[1] numeric

 dateP

[1] 2011-05-22 11:45:00 MDT

 

 dateN - as.numeric(dateP)

 dateN

[1] 1306086300

 

 dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)

 dateP2

[1] 2011-05-22 18:45:00 MDT

 

Many thanks.

 

Galen Moore


[[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.


Re: [R] DateTime Math in R - POSIXct

2011-05-30 Thread Galen Moore
I think that has something to do with it.  I'll keep chewing on things and
see what I can make work.

I've reached a point where I get the right answer for the wrong reasons
;-)

Many thanks,

Galen

-Original Message-
From: bill.venab...@csiro.au [mailto:bill.venab...@csiro.au] 
Sent: Monday, May 30, 2011 21:22
To: galen.a.mo...@gmail.com; r-help@r-project.org
Subject: RE: [R] DateTime Math in R - POSIXct

Perhaps because the timezone is specified as a character string and not a
date-time object complete with timezone.

From the help filr for as.POSIXct.numeric:

origin:a date-time object, or something which can be coerced by
as.POSIXct(tz=GMT) to such an object. 

Note the coercion.

Bill Venables.


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Galen Moore
Sent: Tuesday, 31 May 2011 12:20 PM
To: r-help@r-project.org
Subject: [R] DateTime Math in R - POSIXct

Greetings - 

 

I'm battling POSIXct, as per the code below.  My input is actually an XL
file, but the weird results below correctly model what I am seeing in my
program.

 

Before I punt and use lubridate or timeDate, could anyone please help me
understand why POSIXct forces my variable back to GMT?

 

I suspect that I'm not properly coding the tzone value, but it does not
throw an error as-is.

 

 

 tstamp - 2011-05-22 11:45:00 MDT

 mode(tstamp)

[1] character

 

 dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)

 mode(dateP)

[1] numeric

 dateP

[1] 2011-05-22 11:45:00 MDT

 

 dateN - as.numeric(dateP)

 dateN

[1] 1306086300

 

 dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)

 dateP2

[1] 2011-05-22 18:45:00 MDT

 

Many thanks.

 

Galen Moore


[[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.

__
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.


Re: [R] DateTime Math in R - POSIXct

2011-05-30 Thread Galen Moore
Thank you, David.

I am not subtracting the seconds to convert POSIXct's GMT to MDT, and don't
understand why I should need to.

Any hints, however, as to why  
dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)  
returns a date in the correct tz in my first instance below, yet 

dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)  
decides to revert to GMT in my second instance below?


Thanks,

Galen


-Original Message-
From: David Winsemius [mailto:dwinsem...@comcast.net] 
Sent: Monday, May 30, 2011 20:31
To: galen.a.mo...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] DateTime Math in R - POSIXct


On May 30, 2011, at 10:20 PM, Galen Moore wrote:

 Greetings -



 I'm battling POSIXct, as per the code below.  My input is actually an 
 XL file, but the weird results below correctly model what I am seeing 
 in my program.



 Before I punt and use lubridate or timeDate, could anyone please help 
 me understand why POSIXct forces my variable back to GMT?



 I suspect that I'm not properly coding the tzone value, but it does 
 not throw an error as-is.





 tstamp - 2011-05-22 11:45:00 MDT

 mode(tstamp)

 [1] character



 dateP - as.POSIXct(tstamp, origin=1970-01-01, tzone=MDT)

 mode(dateP)

 [1] numeric

 dateP

 [1] 2011-05-22 11:45:00 MDT



 dateN - as.numeric(dateP)

 dateN

 [1] 1306086300


So now the internal representation is referenced to GMT



 dateP2 - as.POSIXct(dateN, origin=1970-01-01, tzone=MDT)

 dateP2

And you are telling the function to add as many hours offset as needed for
the difference between GMT and MD T.

--
David.

 [1] 2011-05-22 18:45:00 MDT


David Winsemius, MD
West Hartford, CT

__
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.


Re: [R] Trying to perform an inner loop a random number of times

2011-04-28 Thread Galen Moore
Thanks very much, Jim.

I was incorrectly thinking that re-setting the variable feeding iter[i]
would re-set iter[i] as well, I understand clearly now that it would and
should not.

Re-initialization of my vectors immediately before the start of the inner
loop did the trick, and my program now works perfectly.

What I sent was a forum-friendly version of my real program, which generates
testing data for a study on space utilization and inferred preference in a
small (100) parking lot.

Each iteration of the outer (sp) loop  represents a parking space (a fixed
quantity), and each iteration of the inner loop creates the times in/out for
the current space (hence the desire to run it a random number of times).

In case of use to others:
http://stackoverflow.com/questions/1882734/what-is-your-favorite-r-debugging
-trick
http://www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf

Thanks again, 

Galen


-Original Message-
From: jim holtman [mailto:jholt...@gmail.com] 
Sent: Wednesday, April 27, 2011 21:32
To: galen.a.mo...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Trying to perform an inner loop a random number of times

What exactly are you trying to accomplish?  A little debugging might show
you what the problem is.  I put the following statement in the loop right
after the assignment to 'newrow':

print(newrow)

What I saw is that you never reset 'iter' and once it reached a maximum
length, you were continuing to use it so all the dataframes has the same
size after a point.  You can see it in your data in that all the last ones
have a length of 3.

So your loop is doing exactly what you 'wrote' it to do.  If you are not
getting the results you expect, then learn how to debug.  I put a
'browser()' call in you loop and this let me see what was going on.
You need to learn to instrument your scripts so that you can trace the
progress and see if it is working correctly.

On Wed, Apr 27, 2011 at 10:33 PM, Galen Moore galen.a.mo...@gmail.com
wrote:
 Grateful for any hints as to why I'm not getting the inner loop to 
 cycle the expected number of times.



 Code and one run's results below.



 Thanks,



 Galen





 # source(looptest.r)

 sp-numeric()

 iter-numeric()

 rn-numeric()

 ds-data.frame(sp, iter, rn)



 for (sp in c(1:6)) {

 +            i-1

 +            while (i = 5) {

 +                     rn-0

 +                     rn-round(runif(1, 1, 5))

 +                     if (i  rn) { break }

 +

 +                     else {

 +                         iter[i]-i

 +                         newrow-data.frame(sp, iter, rn)

 +                         i-( i + 1)

 +                         }

 +                  }

 +                  ds-rbind(ds, newrow)

 +             }

 print(ds)

   sp iter rn

 1   1    1  3   sp #1 should appear 3x, not 2x

 2   1    2  3

 3   2    1  5   sp #2 should appear 5x, not 3x

 4   2    2  5

 5   2    3  5

 6   3    1  5   sp #3 should appear 5x, not 3x

 7   3    2  5

 8   3    3  5

 9   4    1  4   sp #4 should appear 4x, not 3x

 10  4    2  4

 11  4    3  4

 12  5    1  3   this actually works as expected, but not 2x as with 
 sp #1

 13  5    2  3

 14  5    3  3

 15  6    1  5   sp #6 should appear 5x, not 3x

 16  6    2  5

 17  6    3  5




        [[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.




--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
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.


[R] Trying to perform an inner loop a random number of times

2011-04-27 Thread Galen Moore
Grateful for any hints as to why I'm not getting the inner loop to cycle the
expected number of times.

 

Code and one run's results below.

 

Thanks,

 

Galen 

 

 

 # source(looptest.r)

 sp-numeric()

 iter-numeric()

 rn-numeric()

 ds-data.frame(sp, iter, rn)

 

 for (sp in c(1:6)) {

+i-1

+while (i = 5) {

+ rn-0

+ rn-round(runif(1, 1, 5))

+ if (i  rn) { break }

+ 

+ else {

+ iter[i]-i

+ newrow-data.frame(sp, iter, rn)

+ i-( i + 1)

+ }

+  }

+  ds-rbind(ds, newrow)

+ }

 print(ds)

   sp iter rn

1   11  3   sp #1 should appear 3x, not 2x

2   12  3   

3   21  5   sp #2 should appear 5x, not 3x

4   22  5

5   23  5

6   31  5   sp #3 should appear 5x, not 3x

7   32  5

8   33  5

9   41  4   sp #4 should appear 4x, not 3x

10  42  4

11  43  4

12  51  3   this actually works as expected, but not 2x as with sp #1

13  52  3

14  53  3

15  61  5   sp #6 should appear 5x, not 3x

16  62  5

17  63  5

  


[[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.


[R] Trouble Passing a for loop variable (iteration #) to a data frame

2011-04-25 Thread Galen Moore
Greetings - 

 

I am working on a piece of code to simulate vehicle times in and out in each
of a number of parking spaces.  At this stage, my code basically does what
it is supposed to do but for the sequential number of each new parking event
for a given space (i.e., the index of the loop variable).  Instead of
passing the index of the loop variable (iter) to the data frame, it passes
the value of the total number of iterations.  Eventually, the number of
iterations (parking events in a given space) will be determined by an
rnorm() fcn, so I am not looking for a process that locks-in the number of
iterations.  The total eventual data set size is small-ish, so I'm not
worried about speed.

 

I'm sure my problem lies somehow in my setup of the data frames and my
rbind() fcns, but a great many attempts and several hours of searching
online have not yet brought success.

 

Can you please suggest what I need to do to get the iteration # to appear in
the iter vector and therefore to the data frame?

 

Many thanks,

 

Galen 

 

 

 # fabdata3.r

 # fabricate sample data

 

 #declare the mean duration

 dur-.04

 

 #declare the stdDev of the rnorm() fcn (duration)

 varc-.01

 

 sp-numeric()

 iter-numeric()

 ti-numeric()

 to-numeric()

 actdur-numeric()

 #newline-data.frame(sp, iter, ti, to, actdur)

 ds-data.frame(sp, iter, ti, to, actdur)

 

 # BEGIN OUTER LOOP

 for (sp in c(1:3)) {

+ 

+ct-1

+# BEGIN INNER LOOP

+ 

+x - seq(1, 4, by=1)

+for (i in seq(along=x)) {

+ 

+ if (i == 1)   {

+  ti[1]-((.33+rnorm(1, dur, varc)))

+  to[1]-((ti[1]+rnorm(1, dur, varc)))

+  actdur[1]-(to[1]-ti[1])

+  iter-x[1]

+ 

+  }

+  

+ else {

+ # set subsequent time-ins to prev to + a rand#

+ ti[i]-((to[i-1]+rnorm(1, dur, varc)))

+ # calculate each event's time-out

+ to[i]-((ti[i]+rnorm(1, dur, varc)))

+ # calculate each event's actual duration

+ actdur[i]-(to[i]-ti[i])

+ iter-x[i]

+ 

+ }

+  spStep-paste(sp Value is , sp)

+  print(spStep)

+  iterStep-paste(iter Value is , iter)

+  print(iterStep)

+  

+ newrow-data.frame(sp, iter, ti, to, actdur)

+ ct=ct+1

+  }  #END INNER LOOP

+  ds-rbind(ds, newrow)

+ }

[1] sp Value is  1

[1] iter Value is  1

[1] sp Value is  1

[1] iter Value is  2

[1] sp Value is  1

[1] iter Value is  3

[1] sp Value is  1

[1] iter Value is  4

[1] sp Value is  2

[1] iter Value is  1

[1] sp Value is  2

[1] iter Value is  2

[1] sp Value is  2

[1] iter Value is  3

[1] sp Value is  2

[1] iter Value is  4

[1] sp Value is  3

[1] iter Value is  1

[1] sp Value is  3

[1] iter Value is  2

[1] sp Value is  3

[1] iter Value is  3

[1] sp Value is  3

[1] iter Value is  4

 print(ds)

   sp itertito actdur

1   14 0.3600055 0.4123550 0.05234955

2   14 0.4343887 0.4640804 0.02969170

3   14 0.5240268 0.5622272 0.03820032

4   14 0.5945877 0.6436489 0.04906118

5   24 0.3694827 0.4166405 0.04715775

6   24 0.4609841 0.4968517 0.03586767

7   24 0.5357721 0.5735439 0.03777185

8   24 0.6207077 0.6512799 0.03057217

9   34 0.3801887 0.4122605 0.03207179

10  34 0.4440002 0.4916171 0.04761685

11  34 0.5380228 0.5791549 0.04113214

12  34 0.6087923 0.6291451 0.02035284

 # END OUTER LOOP

   


[[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.


Re: [R] Trouble Passing a for loop variable (iteration #) to a data frame

2011-04-25 Thread Galen Moore
Thank you very much, Peter.  The  iter[i] = x[i] solution worked
perfectly.

Galen

-Original Message-
From: Peter Langfelder [mailto:peter.langfel...@gmail.com] 
Sent: Monday, April 25, 2011 17:07
To: galen.a.mo...@gmail.com
Cc: r-help@r-project.org
Subject: Re: [R] Trouble Passing a for loop variable (iteration #) to a data
frame

You need to index the variable iter: instead of iter = x[i], say

iter[i] = x[i].

But a better solution is to simply say

iter = x

at the beginning and don't update it in the loop.

The way your code is written, iter just holds the last x[i], and the last
x[i] at the end of each loop is 4.

Peter

On Mon, Apr 25, 2011 at 3:29 PM, Galen Moore galen.a.mo...@gmail.com
wrote:
 Greetings -



 I am working on a piece of code to simulate vehicle times in and out 
 in each of a number of parking spaces.  At this stage, my code 
 basically does what it is supposed to do but for the sequential number 
 of each new parking event for a given space (i.e., the index of the 
 loop variable).  Instead of passing the index of the loop variable 
 (iter) to the data frame, it passes the value of the total number of 
 iterations.  Eventually, the number of iterations (parking events in a 
 given space) will be determined by an
 rnorm() fcn, so I am not looking for a process that locks-in the 
 number of iterations.  The total eventual data set size is small-ish, 
 so I'm not worried about speed.



 I'm sure my problem lies somehow in my setup of the data frames and my
 rbind() fcns, but a great many attempts and several hours of searching 
 online have not yet brought success.



 Can you please suggest what I need to do to get the iteration # to 
 appear in the iter vector and therefore to the data frame?



 Many thanks,



 Galen





 # fabdata3.r

 # fabricate sample data



 #declare the mean duration

 dur-.04



 #declare the stdDev of the rnorm() fcn (duration)

 varc-.01



 sp-numeric()

 iter-numeric()

 ti-numeric()

 to-numeric()

 actdur-numeric()

 #newline-data.frame(sp, iter, ti, to, actdur)

 ds-data.frame(sp, iter, ti, to, actdur)



 # BEGIN OUTER LOOP

 for (sp in c(1:3)) {

 +

 +            ct-1

 +            # BEGIN INNER LOOP

 +

 +            x - seq(1, 4, by=1)

 +            for (i in seq(along=x)) {

 +

 +                         if (i == 1)   {

 +                          ti[1]-((.33+rnorm(1, dur, varc)))

 +                          to[1]-((ti[1]+rnorm(1, dur, varc)))

 +                          actdur[1]-(to[1]-ti[1])

 +                          iter-x[1]

 +

 +                          }

 +

 +                         else {

 +                         # set subsequent time-ins to prev to + a 
 + rand#

 +                         ti[i]-((to[i-1]+rnorm(1, dur, varc)))

 +                         # calculate each event's time-out

 +                         to[i]-((ti[i]+rnorm(1, dur, varc)))

 +                         # calculate each event's actual duration

 +                         actdur[i]-(to[i]-ti[i])

 +                         iter-x[i]

 +

 +                         }

 +                          spStep-paste(sp Value is , sp)

 +                          print(spStep)

 +                          iterStep-paste(iter Value is , iter)

 +                          print(iterStep)

 +

 +                         newrow-data.frame(sp, iter, ti, to, actdur)

 +                         ct=ct+1

 +                  }  #END INNER LOOP

 +                  ds-rbind(ds, newrow)

 +             }

 [1] sp Value is  1

 [1] iter Value is  1

 [1] sp Value is  1

 [1] iter Value is  2

 [1] sp Value is  1

 [1] iter Value is  3

 [1] sp Value is  1

 [1] iter Value is  4

 [1] sp Value is  2

 [1] iter Value is  1

 [1] sp Value is  2

 [1] iter Value is  2

 [1] sp Value is  2

 [1] iter Value is  3

 [1] sp Value is  2

 [1] iter Value is  4

 [1] sp Value is  3

 [1] iter Value is  1

 [1] sp Value is  3

 [1] iter Value is  2

 [1] sp Value is  3

 [1] iter Value is  3

 [1] sp Value is  3

 [1] iter Value is  4

             print(ds)

   sp iter        ti        to     actdur

 1   1    4 0.3600055 0.4123550 0.05234955

 2   1    4 0.4343887 0.4640804 0.02969170

 3   1    4 0.5240268 0.5622272 0.03820032

 4   1    4 0.5945877 0.6436489 0.04906118

 5   2    4 0.3694827 0.4166405 0.04715775

 6   2    4 0.4609841 0.4968517 0.03586767

 7   2    4 0.5357721 0.5735439 0.03777185

 8   2    4 0.6207077 0.6512799 0.03057217

 9   3    4 0.3801887 0.4122605 0.03207179

 10  3    4 0.4440002 0.4916171 0.04761685

 11  3    4 0.5380228 0.5791549 0.04113214

 12  3    4 0.6087923 0.6291451 0.02035284

 # END OUTER LOOP




        [[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