Re: [R] RODBC Multiple Results

2020-03-30 Thread Law, Jason
Harold,

I have dealt with this problem over the years and after much experimenting, I 
can say that I *think* what you want is impossible. For RODBC, you need a 
stored proc that uses SET NOCOUNT ON and that only returns a single result set. 
You may want to try experimenting with the odbc package 
(https://github.com/r-dbi/odbc), although there it appears that it isn’t 
possible there yet, there is a pull request which adds this functionality for 
sqlServer. You may want to try hitting up the author of the pull request.

Regards,

Jason

From: R-help  On Behalf Of Bert Gunter
Sent: Wednesday, March 25, 2020 12:39 PM
To: Doran, Harold 
Cc: r-help@r-project.org
Subject: Re: [R] RODBC Multiple Results

⛔
The City's email systems have identified this email as potentially suspicious. 
Please click responsibly and be cautious if asked to provide sensitive 
information.

You might wish to post this on r-sig-db if you do not get a

satisfactory reply here.



Also, have you checked the databases task view on :

https://cran.r-project.org/ ?



Bert Gunter



"The trouble with having an open mind is that people keep coming along

and sticking things into it."

-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )



On Wed, Mar 25, 2020 at 9:54 AM Doran, Harold  wrote:

>

> I'm calling a stored procedure that returns multiple tables and my current 
> framework uses RODBC and sqlQuery() to communicate between R and the 
> databases we connect to. A new stored procedure returns multiple tables and I 
> found this on SO indicating that RODBC "may not" be able to retrieve multiple 
> tables.

>

> https://stackoverflow.com/questions/41494419/how-to-read-multiple-result-sets-returned-from-a-sql-server-stored-procedure-in

>

> I'd very much like to continue within the structure of code I have using 
> RODBC, so am asking if RODBC in fact *cannot* retrieve multiple tables as 
> noted in the SO responses or if anyone has insight on how it might be 
> feasible within the context of RODBC using  sqlQuery()?

>

> Thanks

> Harold

>

>

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



__

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.


Re: [R] One Dimensional Monte Carlo Simulation

2017-08-01 Thread Law, Jason
Tony,

I’m not sure what exactly you’re trying to do, but you're not really taking 
advantage of vectorization in your R code. I've tried to clean it up a little. 
The clamped lognormal is almost always 0 or L? That seems a little odd. You 
seem to be using the inverse cdf method of drawing samples. That's not 
necessary in R for standard probability distributions. You may want to do a 
little more investigating of basic programming tasks in R before you dig into a 
complex simulation.

scale   <- 15.08707 
shape   <-  0.8592507 
lambda.risk <-  1.75 
L   <-  7.5e5 
R   <-  2.5e6

# Generate n random poisson with rate = lambda.risk
frequency <- function(n) rpois(n,lambda.risk)

# clamp a numeric to 0, L
clamp <- function(x, min, max) pmin(max, pmax(min, x))

# Generate lognormal shifted by R
severity <- function(n) rlnorm(n,scale,shape)-R

clamp(severity(100), 0, L) # Lognormal shifted left by R, and then clamped 
between 0 and L? Almost always equal to 0 or L

sim <- function(breaks = 7){
  freq   <- frequency(1)
  i  <- freq > 1:7
  sev<- clamp(severity(sum(i)), 0, L)
  claims <- rep(0, 7)
  claims[i] <- sev
  min(22.5e6,sum(claims)) 
}

hist(iterations <- replicate(1, sim()), breaks = 20)



From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of HUL-Anthony 
Egerton
Sent: Friday, July 14, 2017 7:45 PM
To: r-help@r-project.org
Subject: Re: [R] One Dimensional Monte Carlo Simulation

Further to my email below, I have just realised that I forgot to include the 
specification of L and R.

Hence, the code needs to include the following additional lines at the start;-

L<-7.5e6
R<-2.5e6

Apologies for any confusion caused!

Best regards,

Tony


> On 12 Jul 2017, at 10:03 AM, HUL-Anthony Egerton 
>  wrote:
> 
> I am trying to code a basic Monte Carlo Simulation in R where a Poisson 
> distribution generates a frequency output that is then input into a Lognormal 
> distribution, which produces a different, independent severity for each 
> incidence. The individual incidences are then summed to produce an aggregate 
> amount per period/iteration. 
> 
> Here is the code;-
> 
> scale<-15.08707 
> shape<-0.8592507 
> lambda.risk<-1.75 
> L<-7.5e5 
> R<-2.5e6 
> iterations<-replicate(1,{ 
>  claims1<-0 
>  claims2<-0 
>  claims3<-0 
>  claims4<-0 
>  claims5<-0 
> claims6<-0 
>  claims7<-0 
>  claims<-c(claims1,claims2,claims3,claims4,claims5,claims6,claims7)
>  freq<-(qpois(runif(1),lambda.risk)) 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if(freq>=1){claims1<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if(freq>=2){claims2<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=3) {claims3<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=4) {claims4<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=5) {claims5<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=6) {claims6<-sev} 
>  sev<-pmin(L,pmax(0,(qlnorm(runif(1),scale,shape)-R))) 
>  if (freq>=7) {claims7<-sev} 
>  claims<-c(claims1,claims2,claims3,claims4,claims5,claims6,claims7) 
>  min(22.5e6,sum(claims)) 
> })
> 
> I am new to R, but am sure that there must be a simpler way to code this 
> process. 
> 
> Furthermore, as the Poisson lambda increases, there is a need to include 
> provision for potentially more incidences, which will require a manual 
> expansion of the code to claims8, claims9 etc.
> 
> Can you assist, please?
> 
> Best regards,
> 
> Anthony A H Egerton MA MBA ACII
> �
> HUNTINGTON UNDERWRITING LIMITED
> Labuan FT, Malaysia
> 
> 
> 
> CONFIDENTIALITY NOTE: 
>  
> The information contained in this email message may be legally privileged and 
> contain confidential information and is intended only for the use of the 
> individual or entity to whom it is addressed. If the reader of this message 
> is not the intended recipient, you are hereby notified that any 
> dissemination, distribution or copy of this message is strictly prohibited. 
> If you have received this email in error, please immediately delete this 
> message.
> 
> Begin forwarded message:
> 
>> From: mailto:r-help-requ...@r-project.org
>> Date: 8 July 2017 at 6:00:02 PM SGT
>> To: mailto:r-help@r-project.org
>> Subject: R-help Digest, Vol 173, Issue 8
>> Reply-To: mailto:r-help@r-project.org
>> 
>> Send R-help mailing list submissions to
>>    mailto:r-help@r-project.org
>> 
>> To subscribe or unsubscribe via the World Wide Web, visit
>>    https://stat.ethz.ch/mailman/listinfo/r-help
>> or, via email, send a message with subject or body 'help' to
>>    mailto:r-help-requ...@r-project.org
>> 
>> You can reach the person managing the list at
>>    mailto:r-help-ow...@r-project.org
>> 
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of R-help digest..."
>> 
>> 
>> Today's Topics:
>> 
>>   1. R 

Re: [R] Converting time zones in R using metadata file information of video files, help needed.

2015-11-23 Thread Law, Jason
The "lubridate" package will help simplify these time zone conversions. It 
provides two simple functions with_tz and force_tz that conceptually make 
things simpler.

library(lubridate)
> x <- as.POSIXct("2015-06-22 01:53:28", 'Europe/Berlin')
> with_tz(x, 'America/Toronto')
[1] "2015-06-21 19:53:28 EDT"

HTH,

J

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Ronny Steen
Sent: Sunday, November 22, 2015 2:52 AM
To: r-help@r-project.org
Subject: [R] Converting time zones in R using metadata file information of 
video files, help needed.

Hi,

I have video files (FAT) that are taken in a different timezone than my current 
location. The modification date/time in the metafila data of the video file 
shows the time video was taken, although in the current timezone of my 
computer, if I understand right.

I wish to convert the date/time to the origin. The video was taken in London, 
Ontario Canada at 2015-06-21 07:53:28, when looking at the metadata of the file 
on my computer (timezone "Europe/Berlin") it says modification date "2015-06-22 
01:53:28". Hence, there is a 6 hour difference between the two time-zones

I use the script provided here:
http://blog.revolutionanalytics.com/2009/06/converting-time-zones.html

pb.txt <- "2015-06-22 01:53:28" #modification date as shown on my computer 
(timezone
"Europe/Berlin")

pb.date <- as.POSIXct(pb.txt, tz="Europe/London")

pb.date <- as.POSIXct(pb.txt, tz="America/Toronto")#timezone of origin video 
camera location

format(pb.date, tz=local.time,usetz=TRUE) #formats the mtime to data and time 
when the video was taken

[1] "2015-06-22 07:53:28 CEST" # the time is correct but the date is wrong as 
it has added 6 hours rather than subtracting.

I find working with different time-zones to be difficult, I hope I managed to 
formulate an understandable question.

Regards,

Kes

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

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


Re: [R] help using extrafont package | R graphics

2014-04-30 Thread Law, Jason
On my system, the name of the Garamond font file is GARA.TTF. 

Thus,
font_import(pattern = 'GARA') 

will work, but 
font_import(pattern=gara) 

won't.  Unfortunately, font_import seems to fail rather ungracefully when there 
is no match to a pattern.

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Evan Cooch
Sent: Saturday, April 26, 2014 1:55 PM
To: r-help@r-project.org
Subject: [R] help using extrafont package | R graphics

Greetings --

Submitted this a little while ago -- for some reason, still being held up by 
the moderator. Trying again...


For a host of reasons, I need to use/embed Garamond font with various R 
graphics for a particular publication. I've figured out how to more or 
less get there from here, using the following sequence:


library(extrafont)

Then, I need to import the system fonts (Windoze box, R 3.1.0).

So, I use

font_import()

But, this takes a *huge* amount of time, and often throws errors as it 
chokes on various fonts (I have probably 250+ fonts installed). I only 
want *one* font (Garamond). But, for the life of me, I can't figure out 
how to get font_import to select only the single font I want. In theory

font_import(paths = NULL, recursive = TRUE, prompt = TRUE, pattern = NULL)

as defaults, where pattern is a regex that font filenames must match.

The file name for Garamong is gara.ttf, so I tried

font_import(pattern=gara)

R responds with 'Importing fonts make take a few minutes, depending on 
the...etc, etc'.
Continue? [y/n]

Hit 'y', and am presented with

Scanning ttf files in C:\Windows\Fonts ...
Extracting .afm files from .ttf files...
Error in data.frame(fontfile = ttfiles, FontName = , stringsAsFactors 
= FALSE) :
   arguments imply differing number of rows: 0, 1

I have no idea what to do with this.

Suggestions/pointers to the obvious welcome. And I thought futzing with 
fonts in LaTeX was fun! ;-)


[[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] Change data format query

2014-02-19 Thread Law, Jason
library(reshape2)
data.melt - melt(data, id.vars = c('BH_ID', 'BH_Name', 'Pack_Name')) 
dcast(dm, BH_Name ~ Pack_Name)


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of drruddy gmail
Sent: Wednesday, February 19, 2014 7:18 AM
To: r-help@r-project.org
Subject: [R] Change data format query


# Data manipulation problem #

Please my Git repo at https://github.com/markruddy/RAD.git

Running RStudio 0.97.248

The dataset RYA13Report_transect_AB.csv is in a sort of 'longform' at the 
moment. Which I would like to change so that:

1. Each Pack_Name is a column
2. Each BH_Name is a row
3. Top-mOD are variables of Pack_Name

I've played around with reshape but can't fathom it out. 

Any help appreciated.

Mark

[[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] creating an equivalent of r-help on r.stackexchange.com ? (was: Re: Should there be an R-beginners list?)

2014-02-04 Thread Law, Jason
Clint and Liviu,

Stackoverflow also has rss feeds available, if you prefer being pushed the 
information that way.  For the R tagged questions it's here: 
http://stackoverflow.com/feeds/tag/r.  Since some e-mail clients double as feed 
readers, you may be able to read the feed from your e-mail client.  Otherwise, 
it does mean another application.

Regards,

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Liviu Andronic
Sent: Monday, February 03, 2014 11:24 PM
To: Clint Bowman
Cc: r-help@r-project.org; Bert Gunter
Subject: Re: [R] creating an equivalent of r-help on r.stackexchange.com ? 
(was: Re: Should there be an R-beginners list?)

Dear Clint,


On Tue, Feb 4, 2014 at 1:27 AM, Clint Bowman cl...@ecy.wa.gov wrote:
 Liviu,

 Thanks for the excellent description of the advantages of SE.
 However, there is a significant fraction of the population that
 prefers that information be pushed out to them rather than having to
 pull it to them. The best system is one that accommodates both equally well.

It's not exactly the same as in a mail client, but you also have a push-like 
interface on SE, sort of:
http://stackoverflow.com/questions/tagged/r
- The 'Newest' tab displays all recent questions, sorted in chronological order 
with latest on top; it gets refreshed automatically, as in a mail client 
(hence, push-like)
- The 'Active' tab displays all questions with recent activity (question asked, 
answered or commented upon)
- You also have the very useful 'Unanswered' tab, which allows to identify 
questions that haven't yet received useful advice

Another push-like element in SE is that once you ask a question or answer, any 
subsequent comments on your post will be notified to you either in the web 
interface or by email. This helps keep discussions alive.

Regards,
Liviu



 Clint

 Clint BowmanINTERNET:   cl...@ecy.wa.gov
 Air Quality Modeler INTERNET:   cl...@math.utah.edu
 Department of Ecology   VOICE:  (360) 407-6815
 PO Box 47600FAX:(360) 407-7534
 Olympia, WA 98504-7600

 USPS:   PO Box 47600, Olympia, WA 98504-7600
 Parcels:300 Desmond Drive, Lacey, WA 98503-1274

 On Tue, 4 Feb 2014, Liviu Andronic wrote:

 Dear Don and Bert,
 Allow me to address some of your concerns below.


 On Mon, Feb 3, 2014 at 9:56 PM, Bert Gunter gunter.ber...@gene.com
 wrote:

 I find SO's voting for posting business especially irritating. I
 wish merely to post or to read the posts of others without being
 subjected to some kind of online pseudo game and ratings
 competition. That alone keeps me away. But Don said it better.

 On SO voting is irrelevant for either posting a question or an answer.
 *Anyone* (with an account) can ask a question, and *anyone* can
 answer a question. Their system of privileges is explained here:
 http://askubuntu.com/help/privileges . But to summarize:
 - if you're interested only in giving help, then the only really
 relevant threshold is 10 and 50 votes (removing some new user
 restrictions and allowing you to comment on posts, respectively)
 - if you're interested only in seeking  help, then all thresholds are
 irrelevant really

 All other thresholds are relevant only if you're interested in
 contributing to the organization of information, or in moderating
 this whole forum-slash-wiki thingy. And as a note, given the quality
 of your answers on r-help, Bert, I have no doubt that you will clock
 upwards 50 upvotes in a couple of hours or so.


 I realize that I may be out of step with the masses here, and the
 masses should certainly decide. Hopefully I won't be around if/when
 they decide that R-help should go.

 The proposal is not necessarily to close down r-help. From the myriad
 lists it currently has, R Core could keep only r-help and r-devel,
 and encourage new users to seek help on r.stackexchange.com. The
 scope of r-help could be redefined.


 On Mon, Feb 3, 2014 at 12:42 PM, MacQueen, Don macque...@llnl.gov
 wrote:

 - They waste copious amounts of screen space on irrelevant things
 such as votes, the number of views, the elapsed time since
 something or other happened, fancy web-page headers, and so on. Oh,
 and advertisements. The Mathematica stackexchange example given in
 a link in one of the emails below
 (http://mathematica.stackexchange.com/) illustrates these
 shortcomings -- and it's not the worst such example.


 Well, I've seen my fair share of advertisements on Gmail, Yahoo Mail
 or what have you. I know some use dedicated clients, but not all do.
 (And sofar I haven't noticed one single intrusive or distracting ad
 on
 SE.)

 As for the number of votes, this is actually the most useful bit of
 this QA interface: it allows for the best questions (or most often
 asked) to stand out from all the noise. And it allows for the best
 answers (or those most authoritative) to stand 

Re: [R] Plotting multiple trends on one graph

2013-11-25 Thread Law, Jason
Natalie,

I'm assuming this is some kind of passive animal sampling?  Instream PIT tags 
for fish?  In that case, you can get what I think you want using ggplot2 and 
something like this:

dat$TagID - as.factor(dat$TagID)
dat$Station - as.factor(dat$Station)
dat$Station2 - as.numeric(dat$Station)

ggplot(dat, aes(datetime, Station2, colour = TagID)) + geom_line() + 
scale_y_continuous(breaks = 1:nlevels(dat$Station), labels = 
levels(dat$Station))

This assumes that the stations are equidistant.  If you have actual distances 
between your sampling stations and would like your graphs to reflect that you 
can easily modify the code above so that you plot the distances on the y axis 
and then label the axis using scale_y_continuous.

Using facet_wrap or facet_grid will let you separate the animals into different 
plots.  

Regards,

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
jason@portlandoregon.gov

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Duncan Mackay
Sent: Sunday, November 24, 2013 4:06 PM
To: 'Natalie Houghton McNair'
Cc: R
Subject: Re: [R] Plotting multiple trends on one graph

Hi Natalie

Here is an option using lattice. I think below will get you some way to what 
you want

This is your data formatted. in future please dput your data  as your data was 
scrambled.
# dput(dat)
dat - structure(list(TagID = c(4926L, 4926L, 4926L, 4926L, 4926L, 4926L, 
4926L, 4926L, 4926L, 4926L, 4926L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 
4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L, 4929L), Station = c(KLB, 
MS01, MS02, MS03, MS04, MS05, MS06, MS07, MS08, MS09, MS10, 
KLB, MS01, MS02, MS03, MS04, MS05, MS06, MS07, MS08, MS09, 
MS10, MS11, MS12, MS13), datetime = c(12/21/2012 1:52, 12/21/2012 
2:38,
12/21/2012 3:48, 12/21/2012 4:19, 12/21/2012 4:34, 12/21/2012 5:01,
12/21/2012 6:54, 12/21/2012 7:21, 12/21/2012 10:23, 12/21/2012 12:16,
12/21/2012 14:38, 12/21/2012 1:08, 12/21/2012 2:12, 12/21/2012 3:33,

12/21/2012 3:59, 12/21/2012 4:13, 12/21/2012 5:00, 12/21/2012 6:52,
12/21/2012 7:32, 12/21/2012 10:16, 12/21/2012 11:43, 12/21/2012 14:02,
12/22/2012 2:50, 12/22/2012 5:04, 12/22/2012 13:59), gspd_mps = c(NA, 
0.851, 0.629, 0.86, 1.131, 0.9, 0.798, 0.853, 0.694, 0.6, 0.647, NA, 0.611, 
0.563, 1.04, 1.082, 0.475, 0.796, 0.563, 0.809, 0.783, 0.657, 0.326, 0.709, 
0.688)), .Names = c(TagID, Station, datetime, gspd_mps), class = 
data.frame, row.names = c(NA,
-25L))

# factor of id
dat[,1] - factor(dat[,1])
# convert to datetime
x - paste(dat[,3])
x - strptime(x, %m/%d/%Y %H:%M)

I have added a few extra formatting options but I will leave you to format the 
x labels as an exercise.

# lattice plot conditioned by station
library(lattice)
xyplot(gspd_mps ~ as.POSIXct(x)|Station, dat,
   as.table = TRUE,
   layout = c(1,14),
   groups = TagID,
   strip = FALSE,
   type = c(p,g),
   par.settings = list(strip.background = list(col = transparent),
   superpose.symbol = list(cex = c(1,0.7),
   col = c(red,blue),
   pch = c(20,3))),
   strip.left = strip.custom(par.strip.text = list(cex = 0.65) ),
   scales = list(x = list(alternating = FALSE,
  rot = 90)),
   auto.key = TRUE
   )

# using latticeExtra conditioned by station and tag
library(latticeExtra)
useOuterStrips(strip  = strip.custom(factor.levels = paste(TagID,
unique(dat$TagID)),
 par.strip.text = list(cex = 0.85)),
   strip.left = strip.custom(horizontal = TRUE,
 par.strip.text = list(cex = 0.75)),
 strip.left.lines = 2, xyplot(gspd_mps 
~ as.POSIXct(x)|TagID*Station, dat,
   as.table = TRUE,
   scales = list(x = list(alternating = FALSE,
  rot = 90)),
   type = c(p,g)
   )
) ## useOuterStrips

useOuterStrips(strip  = strip.custom(factor.levels = paste(TagID,
unique(dat$TagID)),
 par.strip.text = list(cex = 0.85)),
   strip.left = strip.custom(horizontal = TRUE,
 par.strip.text = list(cex = 0.75)),
 strip.left.lines = 2, xyplot(gspd_mps 
~ as.POSIXct(x)|TagID*Station, dat,
   as.table = TRUE,
   scales = list(x = list(alternating = FALSE,
  rot = 90)),
   panel = function(x,y, ...){
 panel.grid(h = 0, v= -1)
 panel.xyplot(x,y,...)
   }
   )
) ## useOuterStrips

HTH
Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: 

Re: [R] spsurvey analysis

2013-11-01 Thread Law, Jason
I use the spsurvey package a decent amount.  The cont.cdftest function bins the 
cdf in order to perform the test which I think is the root of the problem.  
Unfortunately, the default is 3 which is the minimum number of bins.

I would contact Tom Kincaid or Tony Olsen at NHEERL WED directly to ask about 
this problem.

Another option would be to take a different analytical approach (e.g., a mixed 
effects model) which would allow you a lot more flexibility.

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason@portlandoregon.gov


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Tim Howard
Sent: Friday, November 01, 2013 7:49 AM
To: r-help@r-project.org
Subject: [R] spsurvey analysis

All,
I've used the excellent package, spsurvey, to create spatially balanced samples 
many times in the past. I'm now attempting to use the analysis portion of the 
package, which compares CDFs among sub-populations to test for differences in 
sub-population metrics. 
 
- My data (count data) have many zeros, following a negative binomial or even 
zero-inflated negative binomial distribution.
- Samples are within polygons of varying sizes
- I want to test whether a sample at time 1 is different from a sample at time 
2. Essentially the same sample areas and number of samples.

The problem:
- cont.cdftest  throws a warning and does not complete for most (but not all) 
species sampled. Warning message: The combined number of values in at least 
one class is less than five. Action: The user should consider using a smaller 
number of classes.

- There are plenty of samples in my two time periods (the dummy set below: 
Yr1=27, Yr2=31 non-zero values). 
 
My Question:
Why is it throwing this error and is there a way to get around it?



Reproduceable example (change path to spsurvey sample data), requires us to use 
spsurvey to generate sample points:

### R code tweaked from vignettes 'Area_Design' and 'Area_Analysis'
library(spsurvey)
### Analysis set up
setwd(C:/Program Files/R/R-3.0.2/library/spsurvey/doc)
att - read.dbf(UT_ecoregions)
shp - read.shape(UT_ecoregions)

set.seed(4447864)

# Create the design list
Stratdsgn - list(Central Basin and Range=list(panel=c(PanelOne=25), 
seltype=Equal),
  Colorado Plateaus=list(panel=c(PanelOne=25), 
seltype=Equal),
  Mojave Basin and Range=list(panel=c(PanelOne=10), 
seltype=Equal),
  Northern Basin and Range=list(panel=c(PanelOne=10), 
seltype=Equal),
  Southern Rockies=list(panel=c(PanelOne=14), 
seltype=Equal),
  Wasatch and Uinta Mountains=list(panel=c(PanelOne=10), 
seltype=Equal),
  Wyoming Basin=list(panel=c(PanelOne=6), seltype=Equal))

# Select the sample design for each year
Stratsites_Yr1 - grts(design=Stratdsgn, DesignID=STRATIFIED,
   type.frame=area, src.frame=sp.object,
   sp.object=shp, att.frame=att, stratum=Level3_Nam, 
shapefile=FALSE)

Stratsites_Yr2 - grts(design=Stratdsgn, DesignID=STRATIFIED,
   type.frame=area, src.frame=sp.object,
   sp.object=shp, att.frame=att, stratum=Level3_Nam, 
shapefile=FALSE)

#extract the core information, add year as a grouping variable, add a plot ID 
to link with dummy data
Yr1 - cbind(pltID = 1001:1100, Stratsites_Yr1@data[,c(1,2,3,5)], grp = Yr1)
Yr2 - cbind(pltID = 2001:2100, Stratsites_Yr2@data[,c(1,2,3,5)], grp = Yr2)  
   
sitedat - rbind(Yr1, Yr2)

# create dummy sampling data. Lots of zeros!
bn.a - rnbinom(size = 0.06, mu = 19.87, n=100) bn.b - rnbinom(size = 0.06, mu 
= 20.15, n=100) dat.a - data.frame(pltID = 1001:1100, grp = Yr1,count = 
bn.a) dat.b - data.frame(pltID = 2001:2100, grp = Yr2,count = bn.b) dat - 
rbind(dat.a, dat.b)


## Analysis begins here

data.cont - data.frame(siteID = dat$pltID, Density=dat$count) sites - 
data.frame(siteID = dat$pltID, Use=rep(TRUE, nrow(dat))) subpop - 
data.frame(siteID = dat$pltID, 
All_years=(rep(allYears,nrow(dat))),
Year = dat$grp)
design - data.frame(siteID = sitedat$pltID,
wgt = sitedat$wgt,
xcoord = sitedat$xcoord,
ycoord = sitedat$ycoord)
framesize - c(Yr1=888081202000, Yr2=888081202000)

## There seem to be pretty good estimates CDF_Estimates - cont.analysis(sites, 
subpop, design, data.cont, 
popsize = list(All_years=sum(framesize),
Year = as.list(framesize)))

print(CDF_Estimates$Pct)

## this test fails
CDF_Tests - cont.cdftest(sites, subpop[,c(1,3)], design, data.cont,
   popsize=list(Year=as.list(framesize)))
warnprnt()

## how many records have values greater than zero, by year?   Probably 

Re: [R] Selecting maximums between different variables

2013-10-17 Thread Law, Jason
See ?pmax for getting the max for each year.

do.call('pmax', oil[-1])

Or equivalently:

pmax(oil$TX, oil$CA, oil$AL, oil$ND)

apply and which.max will give you the index:

i - apply(oil[-1], 1, which.max)

which you can use to extract the state:

names(oil[-1])[i]

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Tim Umbach
Sent: Thursday, October 17, 2013 9:49 AM
To: r-help@r-project.org
Subject: [R] Selecting maximums between different variables

Hi there,

another beginners question, I'm afraid. Basically i want to selct the maximum 
of values, that correspond to different variables. I have a table of oil 
production that looks somewhat like this:

oil - data.frame( YEAR = c(2011, 2012),
   TX = c(2, 3),
   CA = c(4, 25000),
   AL = c(2,
21000),

   ND = c(21000,6))

Now I want to find out, which state produced most oil in a given year. I tried 
this:

attach(oil)
last_year = oil[ c(YEAR == 2012), ]
max(last_year)

Which works, but it doesnt't give me the corresponding values (i.e. it just 
gives me the maximum output, not what state its from).
So I tried this:

oil[c(oil == max(last_year)),]
and this:
oil[c(last_year == max(last_year)),]
and this:
oil[which.max(last_year),]
and this:
last_year[max(last_year),]

None of them work, but they don't give error messages either, the output is 
just NA. The problem is, in my eyes, that I'm comparing the values of 
different variables with each other. Because if i change the structure of the 
dataframe (which I can't do with the real data, at least not with out doing it 
by hand with a huge dataset), it looks like this and works
perfectly:

oil2 - data.frame (
  names = c('YEAR', 'TX', 'CA', 'AL', 'ND'),
  oil_2011 = c(2011, 2, 4, 2, 21000),
  oil_2012 = c(2012, 3, 25000, 21000, 6)
  )
attach(oil2)
oil2[c(oil_2012 == max(oil_2012)),]

Any help is much appreciated.

Thanks, Tim Umbach

[[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] Plot time series data irregularly hourly-spaced

2013-10-16 Thread Law, Jason
 You just need the date, otherwise how would it know what time comes first?  In 
strptime(), a date is being assumed.

Try this:

testtime-c(20:00:00,22:10:00,22:20:00,23:15:00,23:43:00,00:00:00,00:51:00,01:00:00)
testday - rep(Sys.Date() - c(1,0), times = c(5,3))
plot(as.POSIXct(paste(testday, testtime)), var)

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Charles Novaes de Santana
Sent: Wednesday, October 16, 2013 2:58 PM
To: r-help@r-project.org
Subject: [R] Plot time series data irregularly hourly-spaced

Dear all,

I have a time series of data that I would like to represent in a plot. But I am 
facing some problems to do it because the time is represented in hours, it 
can start in one day and end in another day, and it is not regularly spaced.

My problem is that when I plot my data, my X-axis always starts from the lower 
values of my time data. For example, I would like to plot data that starts at 
20:00:00 and ends at 01:00:00, but R considers that 01:00:00 is lower than 
21:00:00 and my plot is kind of crossed over time.

Please try this example to see it graphically:

testtime-c(20:00:00,22:10:00,22:20:00,23:15:00,23:43:00,00:00:00,00:51:00,01:00:00)
var-runif(length(testtime),0,1)
plot(strptime(testtime,format=%H:%M:%S),var,type=b,xlab=Time,ylab=Var)

In this case, I would like to have a plot that starts at 20:00:00 and ends at 
01:00:00.

Does anybody know how to make R understand that 00:00:00 comes after 20:00:00 
in this case? Or at least does anybody know a tip to make a plot with this kind 
of X-axis?

Thanks for your time and thanks in advance for any help.

Best regards,

Charles
--
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

[[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] Transposing the output of 'table'

2013-10-07 Thread Law, Jason
If you want a dataframe rather than a matrix, I often use the as.data.frame 
method for table objects.  See ?table for the documentation.  You can even 
nicely name the dimensions and frequency.

OBJECT - sample(4, 20, TRUE)

as.data.frame(table(var1 = OBJECT), responseName = 'frequency')

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Dennis Fisher
Sent: Sunday, October 06, 2013 10:31 AM
To: r-h...@stat.math.ethz.ch
Subject: [R] Transposing the output of 'table'

R 3.0.1
OS X

Colleagues,

If I execute the command:
table(OBJECT)
the output might look like:
  1   2 
 25 336 

I would like it to appear as:
1   25
2   336

I can accomplish this with:
TABLE   - table(OBJECT)
data.frame(names(TABLE), as.numeric(TABLE))

However, I bet that a more clever approach exists?  Any takers?

Dennis


Dennis Fisher MD
P  (The P Less Than Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-866-PLessThan (1-866-753-7784)
www.PLessThan.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.

__
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] function on columns of two arrays

2013-08-20 Thread Law, Jason
library(abind)
library(plyr)
c - abind(a,b, along = 4) 
results - alply(c, c(2,3), function(x) lm(x[,2] ~ x[,1]))
ldply(results, function(x) summary(x)$coef)

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Folkes, Michael
Sent: Tuesday, August 20, 2013 12:34 AM
To: r-help@r-project.org
Subject: [R] function on columns of two arrays

I've spent a bit too long searching the help history and attempting to apply 
some logic to the following:
I have two 3D arrays each with same dim. I wish to run lm on the respective 
columns of each array, preferably without loops.
We often hear chatter that sometimes apply() won't be faster just use a for 
loop I'd like to test this one...
I just can't seem to wrap my brain around use of mapply on this task and am 
more surprised that I'm not finding a solution out there already.


a - array(1:60,dim=5:3)
b - a*3+10
lm(b[,1,1]~a[,1,1])
#and repeat for all rows and columns...

thanks in advance.
Michael

___
Michael Folkes
Salmon Stock Assessment
Canadian Dept. of Fisheries  Oceans 
Pacific Biological Station

[[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] replace multiple values in vector at once

2013-07-12 Thread Law, Jason
In the plyr package there are also the functions revalue and mapvalues:

library(plyr)
x - c(a, b, c)
revalue(x, c(a = A, c = C))
mapvalues(x, c(a, c), c(A, C))

mapvalues works on numeric, character and factor.

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Trevor Davies
Sent: Friday, July 12, 2013 2:57 PM
To: r-help@r-project.org
Subject: Re: [R] replace multiple values in vector at once

I always think that replying to your own r-help feels silly but it's good to 
close these things out.

here's my hack solution:

x1-merge(data.frame(A=x),data.frame(A=c('x','y','z'),B=c(1,2,2)),by='A')[,2]

Well that works and should for my more complex situation.  If anyone has 
something a little less heavy handed I'd live to hear it.

Have a great weekend.


On Fri, Jul 12, 2013 at 2:18 PM, Trevor Davies davies.tre...@gmail.comwrote:


 I'm trying to find a function that can replace multiple instances of 
 values or characters in a vector in a one step operation.  As an 
 example, the vector:

 x - c(rep('x',3),rep('y',3),rep('z',3))

  x
 [1] x x x y y y z z z

 I would simply like to replace all of the x's with 1's, y:2  z:3 (or 
 other characters).
 i.e:
  x
 [1] 1 1 1 2 2 2 3 3 3

 Of course, I'm aware of the replace function but this obviously gets a 
 little unwieldy when there are :
 x-replace(x,x=='x',1)
 x-replace(x,y=='x',2)
 x-replace(x,z=='x',3)

 but I can't figure out how to do it in a one stop operation.  My real 
 needs is more complex obviously.  This is one of those seemingly 
 simple r-operations that should be obvious but I'm coming up empty on this 
 one.

 Thanks for the help.
 Trevor


[[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] String based chemical name identification

2013-07-03 Thread Law, Jason
Might be better off using a web service like ChemSpider to do the matching for 
you http://www.chemspider.com/AboutServices.aspx?.  The idea that you can 
identify the synonyms by name is probably optimistic unless they are exact 
matches.

Here's some python code that seems to make it pretty easy: 
https://github.com/mcs07/ChemSpiPy.  Search the names, extract the InChI for 
the best match and then you can match them in R via the InChI.  Might require 
some fixing by hand afterwards.

HTH,

Jason Law

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Zsurzsa Laszlo
Sent: Wednesday, July 03, 2013 7:28 AM
To: r-help@r-project.org
Subject: [R] String based chemical name identification

The problem is the following:

I have two big databases one look like this:

  2-Methyl-4-trimethylsilyloxyoct-5-yne   Benzoic acid, methyl ester   Benzoic
acid, 2-methyl-, methyl ester   Acetic acid, phenylmethyl ester
 2,7-Dimethyl-4-trimethylsilyloxyoct-7-en-5-yne   etc.

The second one looks like this:

 Name: D-Tagatose 1,6-bisphosphate  Name: 1-Phosphatidyl-D-myo-inositol;:
1-Phosphatidyl-1D-myo-inositol;: 1-Phosphatidyl-myo-inositol;:
Phosphatidyl-1D-myo-inositol;: (3-Phosphatidyl)-1-D-inositol;:
1,2-Diacyl-sn-glycero-3-phosphoinositol;: Phosphatidylinositol  Name:
Androstenedione;: Androst-4-ene-3,17-dione;: 4-Androstene-3,17-dione  Name:
Spermine;: N,N'-Bis(3-aminopropyl)-1,4-butanediamine  Name: H+;: Hydron  Name:
3-Iodo-L-tyrosine  etc.

Both of them have more then 3000 lines. Matching their name by hand is not an 
option because I don't know chemistry.

*Possible solution I came up with*:

Go through all the names of the first database and then try to match with the 
other one. I'm using *regexec *and *strsplit *functions for the matching. 
Basically I split the name into small chunks and try to get some hit in the 
other database.

I can supply code If needed but I did not want to spam in the first mail.


Any solution is welcome! It can be in pseudo-cod also or in any type of logical 
arguing. It does not matter.


Laszlo-Andras Zsurzsa

Msc. Informatics, Technical University Munchen

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

2013-06-25 Thread Law, Jason
Not sure what you're trying to do, but it looks like most of what you're 
attempting to do in the code can be done just using vectors rather than loops, 
at least the inner loop.  For example:

k - 1.15
l - exp((1 / k) * (7.16 - 0.44 + 0.12 - 0.016))
z - (log(1 / p) * l)^k

See ifelse for how to do the if tests on a vector.  In addition, much of the 
code in your loops doesn't vary with the loop indices and can be moved outside 
your loop (e.g., setting k = 1.15).  If you really want/need to use loops, 
you'll have to initialize the vectors/matrices within your loop with some value:

z - numeric(1000)

Finally, you have some plain syntax errors: 

p[i]=[i+1].

That's not valid R code; '[' is the extraction operator, see help('[').  I'm 
not sure what you're trying to do there.  Perhaps:

p[i] - p[i + 1]

HTH,

Jason Law



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of bett kimutai
Sent: Tuesday, June 25, 2013 1:32 PM
To: r-help@r-project.org
Subject: [R] Loops

Dear All,


I  have spent most of my time trying to figure out how to simulate the number 
of breaks in a pipe using Monte Carlo simulation.
i have 20,000 individual pipes that i have to run, and for each pipe i have to 
run 1000 times while checking some conditions and therefore, i have to use a 
nested loop.
what i would like to have as a final result is a matrix table with with all the 
individual pipe elements and the simulated runs here is the loop that i tried 
to create x=2 y=matrix(x, z)
p=runif(1000)
for(j in 1:2) {
for(i in 1:1000) {
       k=1.15
    l=exp((1/k)*(7.16-0.44+0.12-0.016))
            
    z[i]=(log(1/p[i])*l)^k

    if (z[i] =684)
    {
        k1=0.504
        l1=exp((1/k)*(8.01-1.5+0.35+0.45))
        z1[i]=(log(1/p[i])*l1)^k1
    if (z1[i] =684)
    {
                k2=0.43
        l2=exp((1/k2)*(9.55-2.45+0.40+0.65))
        z2[i]=(log(1/p[i])*l2)^k2
        p[i]=[i+1]
                break()
            }
        }
    }
x[ j ]=[ j+1 ]
}
the last column of the table,  in addition to the simulated runs, i would like 
to have the summary of the means (for z=684) of individual row as this means 
will give me the number of breaks.
i will really appreciate if anyone who can help me figure out how to go about 
this. pardon me, I am new to R and programming.



Thank you in Advance,

Eliab
    [[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.
[[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] Reshape or Plyr?

2013-04-22 Thread Law, Jason
Hi Bruce,

I work with a lot of similar data and have to do these types of things quite 
often.  I find it helps to keep to vectorized code as much as possible.  That 
is, do as many of the calculations as possible outside of the aggregation code. 
 Here's one way:

library(reshape2)
# stick to a variable naming convention and you'll avoid a lot of simple code 
errors
names(d) - gsub('_', '.', tolower(names(d)), fixed = T)
dm   - melt(d, measure.var = c('ai', 'survey.time'))
results  - dcast(dm, location.name + spec.code ~ variable, fun.aggregate = 
sum)
results$ra - results$ai / results$survey.time * 10

The output:

   location.name spec.code ai survey.time ra
1  079-f2p1-Acetuna  Buzz   872.8  1.0989011
2  079-f2p1-AcetunaEumspp   524.3  2.0576132
3  079-f2p1-Acetuna  Frag  1812.1 14.8760331
4  079-f2p1-AcetunaMolmol   112.1  0.8264463
5  079-f2p1-AcetunaMolspp  2872.8  3.8461538
6  079-f2p1-AcetunaMyokea   112.2  0.8196721
7  079-f2p1-AcetunaNocalb  1024.3  4.1152263
8  079-f2p1-AcetunaPhyllo   436.4  1.0989011
9  079-f2p1-AcetunaPtedav   336.4  0.8241758
10 079-f2p1-AcetunaPtegym   636.4  1.6483516
11 079-f2p1-AcetunaPtepar   936.4  2.4725275
12 079-f2p1-AcetunaPteper   424.3  1.6460905
13 079-f2p1-AcetunaRhotum  3036.4  8.2417582
14 079-f2p1-AcetunaSacbil  1136.4  3.0219780
15 079-f2p1-AcetunaSaclep  3236.4  8.7912088

For a simple aggregation like this, reshape is simple and fast.  I tend to use 
plyr when things get more complicated.

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason@portlandoregon.gov

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Bruce Miller
Sent: Saturday, April 20, 2013 6:55 AM
To: r-help@r-project.org
Subject: [R] Reshape or Plyr?

H all,

I have relative abundance data from 100 sites.  This is from acoustic 
monitoring and usually the data is for 2-3 nights but in some cases my be 
longer like months or years for each location..
The data output from my management data base is proved by species by night for 
each location so data frame would look like this below. What I need to do is 
sum the Survey_time by Spec_Code for each location name and divide summed AI 
values for each Spec_code by the summed Survey time to adjust for unit effort 
then standardize it all by *10 to represent the relative abundance by survey 
hour to 10 hours. How best to do this?  
Using Plyr or reshape?

Location name   SPEC_CODE   Start_Day   Survey_Time AI  Std AI
079-f2p1-AcetunaBuzz2/14/2012   12.11   0.8264463
079-f2p1-AcetunaBuzz2/14/2012   12.11   0.8264463
079-f2p1-AcetunaEumspp  2/14/2012   12.11   0.8264463
079-f2p1-AcetunaFrag2/14/2012   12.118  14.87603
079-f2p1-AcetunaMolspp  2/14/2012   12.15   4.132231
079-f2p1-AcetunaMolspp  2/14/2012   12.15   4.132231
079-f2p1-AcetunaPhyllo  2/14/2012   12.12   1.652893
079-f2p1-AcetunaPtedav  2/14/2012   12.11   0.8264463
079-f2p1-AcetunaPtegym  2/14/2012   12.11   0.8264463
079-f2p1-AcetunaPtepar  2/14/2012   12.12   1.652893
079-f2p1-AcetunaRhotum  2/14/2012   12.16   4.958678
079-f2p1-AcetunaSacbil  2/14/2012   12.16   4.958678
079-f2p1-AcetunaSaclep  2/14/2012   12.111  9.090909
079-f2p1-AcetunaBuzz2/15/2012   12.12   1.652893
079-f2p1-AcetunaBuzz2/15/2012   12.12   1.652893
079-f2p1-AcetunaMolmol  2/15/2012   12.11   0.8264463
079-f2p1-AcetunaMolspp  2/15/2012   12.17   5.785124
079-f2p1-AcetunaMolspp  2/15/2012   12.17   5.785124
079-f2p1-AcetunaNocalb  2/15/2012   12.16   4.958678
079-f2p1-AcetunaPhyllo  2/15/2012   12.11   0.8264463
079-f2p1-AcetunaPtedav  2/15/2012   12.11   0.8264463
079-f2p1-AcetunaPtegym  2/15/2012   12.14   3.305785
079-f2p1-AcetunaPtepar  2/15/2012   12.14   3.305785
079-f2p1-AcetunaPteper  2/15/2012   12.13   2.479339
079-f2p1-AcetunaRhotum  2/15/2012   12.17   5.785124
079-f2p1-AcetunaSacbil  2/15/2012   12.12   1.652893
079-f2p1-AcetunaSaclep  2/15/2012   12.16   4.958678
079-f2p1-AcetunaBuzz2/16/2012   12.21   0.8196721
079-f2p1-AcetunaBuzz2/16/2012   12.21   0.8196721
079-f2p1-AcetunaEumspp  

Re: [R] Averaging Out many rows from a column AND funtion to string

2013-03-27 Thread Law, Jason
This completes in about a second on my system and uses the actual matrix 
dimensions you quote:

  nr - 153899
  nc - 3415
  keep - rnorm(nr * nc, 80, 20)
  dim(keep) - c(nr, nc)
  shrink.to - 1000
  system.time(
{
  idx - rep(1:shrink.to, length.out = nr)
  plot.me - sweep(rowsum(keep, idx), 1, tabulate(idx), , FUN = '/')
})
   user  system elapsed 
   0.920.000.9

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason@portlandoregon.gov


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Alaios
Sent: Wednesday, March 27, 2013 9:05 AM
To: PIKAL Petr; R help
Subject: Re: [R] Averaging Out many rows from a column AND funtion to string

Well my true matrix is 

of


num [1:153899, 1:3415]

that I want to convert to something like

num [1:1000, 1:3415] (keeping column number the same).

I only give subsets here to allow others to run the code at their computer

Thanks a lot

Regards
Alex




 From: PIKAL Petr petr.pi...@precheza.cz

Sent: Wednesday, March 27, 2013 4:45 PM
Subject: RE: [R] Averaging Out many rows from a column AND funtion to string



Hi
 
 system.time({
+ keep-matrix(data=rnorm(900,80,20),nrow=30,ncol=30)
+ ShrinkTo-500
+ idx-cut(1:nrow(keep), ShrinkTo)
+ keep.ag-aggregate(keep, list(idx), mean)
+ PlotMe-as.matrix(keep.ag[,-1])
+ })
   user  system elapsed
  29.23    0.14   29.37 
  
 
It takes 30 seconds when using 30 rows. It is not enough time to get a cup 
of tee, which I do not consider ages. Maybe split lapply approach or 
data.matrix or  **ply could be quicker but I do not consider worth spending 
hours to elaborate some solution which will spare 20 sec computing time.
 
Regards
Petr 
 

Sent: Wednesday, March 27, 2013 4:12 PM
To: PIKAL Petr; R help
Subject: Re: [R] Averaging Out many rows from a column AND funtion to string
 
I have fixed it like that:


keep-matrix(data=rnorm(900,80,20),nrow=30,ncol=30)
ShrinkTo-500
idx-cut(1:nrow(keep), ShrinkTo)
keep.ag-aggregate(keep, list(idx), mean)
PlotMe-data.matrix(keep.ag[2:ShrinkTo])

The only problem is that takes ages to finish. Would it be possible to 
convert it to something like lapply?

Regards
Alex
 
 



From:PIKAL Petr petr.pi...@precheza.cz

Sent: Wednesday, March 27, 2013 4:02 PM
Subject: RE: [R] Averaging Out many rows from a column AND funtion to string


Hi
 
 str(as.matrix(keep.ag[,-1]) )  # does look like numeric
num [1:10, 1:30] 75.1 93 79 81.7 76.3 ...
- attr(*, dimnames)=List of 2
  ..$ : NULL
  ..$ : chr [1:30] V1 V2 V3 V4 ...
 
Please read and follow what was recommended.
 
quote
 
use as.matrix(data.frame) on numeric part
  ^^^
 
aggregate produces data frame with its first column being your idx variable, 
which is factor. Trying to convert it whole to matrix results in character 
matrix. You need to exclude first column from conversion
 
And please can you explain how mean(rnorm(whatever)) shall be integer?
 
Regards
Petr
 

Sent: Wednesday, March 27, 2013 3:01 PM
To: PIKAL Petr; R help
Subject: Re: [R] Averaging Out many rows from a column AND funtion to string
 
see inline
 
 



From:PIKAL Petr petr.pi...@precheza.cz

Sent: Wednesday, March 27, 2013 1:50 PM
Subject: RE: [R] Averaging Out many rows from a column AND funtion to string
 
Hi
 

Sent: Wednesday, March 27, 2013 11:46 AM
To: PIKAL Petr; R help
Subject: Re: [R] Averaging Out many rows from a column AND funtion to string
 
see inline
 



From:PIKAL Petr petr.pi...@precheza.cz

Sent: Wednesday, March 27, 2013 11:24 AM
Subject: RE: [R] Averaging Out many rows from a column AND funtion to string

Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- 
 project.org] On Behalf Of Alaios
 Sent: Wednesday, March 27, 2013 9:13 AM
 To: R help
 Subject: [R] Averaging Out many rows from a column AND funtion to 
 string
 
 Dear all,
 1) I have a very large matrix of
 str(keep)
  num [1:153899, 1:3415] -98.6 -95.8 -96.4 -95.8 -98 ...
 that  I would like to reduce its size to something like
 
 str(keep)
  num [1:1000, 1:3415] -98.6 -95.8 -96.4 -95.8 -98 ... or anything 
 similar in size as this is a matrix that needs plotting (so is ok if 
 it is 1000 row, 995, or 1123)
 
 I think what I need here is a way of selecting multiple rows and 
 averaging per column (notice that the column number should stay the
 same)

Make an index variable and aggregate values according it

Something like
idx-cut(1:153899, 153)
keep.ag-aggregate(keep, list(idx), mean)

1) Thanks that returned a data frame.. How I can have a matrix at the end?
use as.matrix(data.frame) on numeric part
 
a bit of my code that you can re run. I convert a 

Re: [R] Plot cumulative sums of rainfall per year

2013-03-26 Thread Law, Jason
Try this:

library(plyr)
library(ggplot2)
library(lubridate)
data-read.csv(http://dl.dropbox.com/u/4236038/test_cumu.csv;)
data$Date - as.Date(data$Date)
cumu - ddply(data,.(year(Date)),transform, cumRain = cumsum(Rainfall))
ggplot(cumu, aes(x = yday(Date), y = cumRain, color = factor(year(Date + 
geom_line()

You'll have to work on the date axis.

Jason Law
Statistician
City of Portland, Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason@portlandoregon.gov


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Martin Labadz
Sent: Monday, March 25, 2013 10:44 PM
To: r-help@r-project.org
Subject: [R] Plot cumulative sums of rainfall per year

Hi @all,

I am biting my nails with the following problem:

I have a data set of daily rainfall measurements for the last 20 years. What I 
want to do is calculate the daily cumulative sum of rainfall but only for every 
year which means that the cumulative sum has to be reset each year. After the 
calculations I want to plot each year of cumulative rainfall as a separate line 
in one graph preferably using ggplot with the x-axis showing the julian day 1 
to 365 (366) and the y-axis showing the cumulative values.

I have the following code:

library(plyr)
library(ggplot2)
data-read.csv(http://dl.dropbox.com/u/4236038/test_cumu.csv;)
data$year - as.numeric(format(as.Date(data$Date), format=%Y)) 
ddply(data,.(year),transform,cumRain = cumsum(Rainfall))-cumu ggplot(cumu, 
aes(Date,cumRain))+geom_point()

What it does it perfectly calculates the cumulative sum of the rainfall and 
resets the sum at the beginning of each year but I cannot plot the cumulative 
sum of rainfall in a way that each year is represented by a separate line in 
one graph such as in this example: 
http://dl.dropbox.com/u/4236038/example_cumulative_rainfall.png

Any help would be highly appreciated.

Thanks,
Martin

__
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] R-help Digest, Vol 121, Issue 5

2013-03-05 Thread Law, Jason
On R 2.15.2 and ArcGIS 9.3.1, it works for me in ArcCatalog but you have to 
follow the particulars here:

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Accessing_delimited_text_file_data

For example:

write.table(test, '***.tab', sep = '\t', row.names = F)

The extension .tab and sep = '\t' are required for text files.  Didn't test 
row.names=T but I wouldn't count on that working either.

Jason Law
Statistician
City of Portland, Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
503-823-1038
jason@portlandoregon.gov

-Original Message-
Date: Mon, 04 Mar 2013 10:48:39 -0500
From: Duncan Murdoch murdoch.dun...@gmail.com
To: Kerry kernichol...@yahoo.com
Cc: r-help@r-project.org r-help@r-project.org
Subject: Re: [R] Mysterious issues with reading text files from R in
ArcGIS and Excel
Message-ID: 5134c257.6020...@gmail.com
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 04/03/2013 10:09 AM, Kerry wrote:
 It seems within the last ~3 months Ive been having issues with writing text 
 or csv files from a R data frame.  The problem is multifold and it is hard to 
 filter  out what is going on and where the problem is.  So, Im hoping someone 
 else has come across this and may provide insight.

I think you need to provide a simple example for us to try, either by 
putting a small example of one of your files online for us to download, 
or (better) by giving us self-contained code to duplicate the problem.

You might also get better help (especially about ArcGIS) on the 
R-sig-Geo mailing list: https://stat.ethz.ch/mailman/listinfo/r-sig-geo.

Duncan Murdoch




 My current settings for R:
 R version 2.15.2 (2012-10-26)
 Platform: x86_64-w64-mingw32/x64 (64-bit)
 locale:

 [1] LC_COLLATE=Swedish_Sweden.1252  LC_CTYPE=Swedish_Sweden.1252
 LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C
 [5] LC_TIME=Swedish_Sweden.1252

 attached base packages:
 [1] tcltk stats graphics  grDevices utils datasets  methods   base

 other attached packages:
 [1] adehabitat_1.8.11 shapefiles_0.6foreign_0.8-51tkrplot_0.0-23
 ade4_1.5-1

 loaded via a namespace (and not attached):
 [1] tools_2.15.2

 I am using Microsoft Excel 2010 and ArcGIS 10.1sp1 for Desktop

 Basically, no matter what data frame I am working on, when I export it to a 
 text file to be use in Excel or ArcGIS problems arise.  Im not sure if it is 
 R or these other programs, maybe forums for ArcGIS might be more appropriate, 
 but this problem only occurs when I use tables that have been produced from 
 an R session.

 When I try to open a text file in Excel, either I get an error message stating
 The file you are trying to open is in a different format than specified by 
 the file extension.  Verify that the file is not corrupted and is from a 
 trusted source.
 Followed by
 Excel has detected that 'file.txt' is a SYLK file, but cannot load it.  
 Either the file has errors or is not a SYLK file format.  Click OK to open 
 the file in a different format
 Then the file opens


 Otherwise, the file opens fine the first time through - and looks ok. I 
 can't figure out what Im doing different between the two commands of 
 write.table as they are always written the same:
 write.csv(file, file = D:/mylocations/fileofinterest.csv) or 
 write.table(file, file = D:/mylocations/fileofinterest.txt)
 Sometimes I will try to add sep = , or sep = ; but these don't make a 
 difference (which I didn't figure they would).

 The other program I use is ArcGIS and bringing in a txt file from R is really 
 messing things up as 2 new columns of information are typically added and 
 date/time data is usually lost with txt files, but not with csv files.

 For instance - a text file that looks like this in Excel:
  id   x   ydateR1dmedR1dmean R1error 
 R2error
 1 F07001 1482445 6621768 2007-03-05 10:00:53 2498.2973 2498.2973   FALSE   
 FALSE
 2 F07001 1481274 6619628 2007-03-05 12:00:41  657.1029  657.1029FALSE   
 FALSE
 3 F07001 1481279 6619630 2007-03-05 14:01:12  660.3569  660.3569FALSE   
 FALSE
 4 F07001 1481271 6619700 2007-03-05 16:00:39  620.1397  620.1397FALSE   
 FALSE

   in ArcGIS now looks like this:

 Field1idid_Xid_YxydateR1dmedR1dmean R1errorR2errorOBJECTID *
 1F07001118.0818119.485541e+01514824456621768NA2498.297272498.29727FALSEFALSE1
 2F07001118.0818119.485541e+01514812746619628NA657.102922657.102922FALSEFALSE2
 3F07001118.0818119.485541e+01514812796619630NA660.356911660.356911FALSEFALSE3
 4F07001118.0818119.485541e+01514812716619700NA620.139702620.139702FALSEFALSE4
 5F07001118.0818119.485541e+01514808496620321NA378.186792378.186792FALSEFALSE5

 Where did id_X and id_Y come from?? What are they??
 What happened to the Date column???  Why does the date column show up when I 
 use write.csv but not write.table?

 Thank you for your help.

 ~K
   [[alternative HTML version deleted]]

Re: [R] Install package automatically if not there?

2010-06-24 Thread Law, Jason
 
Something like:

if (!require(pkg)){
install.packages(pkg)
}

Jason

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Ralf B
Sent: Thursday, June 24, 2010 12:26 PM
To: r-help@r-project.org
Subject: [R] Install package automatically if not there?

Hi fans,

is it possible for a script to check if a library has been installed?
I want to automatically install it if it is missing to avoid scripts to crash 
when running on a new machine...

Ralf

__
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] Summing data based on certain conditions

2010-04-01 Thread Law, Jason
This should do the trick:

data$date - as.Date(data$date, '%m/%d/%Y') 
data$month - format(data$date, '%Y-%m')
by(data$rammday, data$month, sum)

Hope that helps,

Jason


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Stephan Kolassa
Sent: Wednesday, March 31, 2010 2:31 PM
To: Steve Murray
Cc: r-help@r-project.org
Subject: Re: [R] Summing data based on certain conditions

?by may also be helpful.

Stephan


Steve Murray schrieb:
 Dear all,
 
 I have a dataset of 1073 rows, the first 15 which look as follows:
 
 data[1:15,]
 date year month day rammday thmmday
 1   3/8/1988 1988 3   81.430.94
 2  3/15/1988 1988 3  152.860.66
 3  3/22/1988 1988 3  225.063.43
 4  3/29/1988 1988 3  29   18.76   10.93
 5   4/5/1988 1988 4   54.492.70
 6  4/12/1988 1988 4  128.574.59
 7  4/16/1988 1988 4  16   31.18   22.18
 8  4/19/1988 1988 4  19   19.67   12.33
 9  4/26/1988 1988 4  263.141.79
 10  5/3/1988 1988 5   3   11.516.33
 11 5/10/1988 1988 5  105.642.89
 12 5/17/1988 1988 5  17   37.46   20.89
 13 5/24/1988 1988 5  249.869.81
 14 5/31/1988 1988 5  31   13.008.63
 15  6/7/1988 1988 6   70.430.00
 
 
 I am looking for a way by which I can create monthly totals of rammday 
 (rainfall in mm/day; column 5) by doing the following:
 
 For each case where the month value and the year are the same (e.g. 3 and 
 1988, in the first four rows), find the mean of the the corresponding rammday 
 values and then times by the number of days in that month (i.e. 31 in this 
 case).
 
 Note however that the number of month values in each case isn't always the 
 same (e.g. in this subset of data, there are 4 values for month 3, 5 for 
 month 4 and 5 for month 5). Also the months will of course recycle for the 
 following years, so it's not simply a case of finding a monthly total for 
 *all* the 3s in the whole dataset, just those associated with each year in 
 turn.
 
 How would I go about doing this in R?
 
 Any help will be gratefully received.
 
 Many thanks,
 
 Steve
 
 
 
 _
 We want to hear all your funny, exciting and crazy Hotmail stories. 
 Tell us now
 
 __
 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.

__
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] create string of comma-separated content of vector

2009-05-19 Thread Law, Jason
See ?toString

x - 0:10
toString(x)

See ?sQuote for cases where the vector is a character and needs to be quoted.

Jason Law
Statistician
City of Portland
Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203-5452
jason@bes.ci.portland.or.us

 Hi,

 how do I create a string of the comma-separated content of a vector?

 I've got the vector i with several numeric values as content:
str(i)
 num 99

 and want to create a SQL statement to look like the following where
 the part '(2, 4, 6, 7)' should be
 the content of the vector i:
 select * from  [biomass_data$] where site_no in (2, 4, 6, 7)

 Here my approach (which doesn't work):
 site_all_data =  sqlQuery(channel, select * from  [biomass_data$]
 where site_no in (,paste(i,sep=,),) )


 sorry for spaming so much today to the mailing list...

 -Katharina

 --
 Time flies like an arrow, fruit flies like bananas.

__
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] R crash with ATLAS precompiled Rblas.dll on Windows XP Core2 Duo

2008-07-08 Thread Law, Jason
I noticed a problem using R 2.7.1 on Windows XP SP2 with the precompiled
Atlas Rblas.dll.  Running the code below causes R to crash.  I started R
using Rgui --vanilla and am using the precompiled Atlas Rblas.dll from
cran.fhcrc.org dated 17-Jul-2007 05:04 for Core2 Duo.

The code that causes the crash:

x - rnorm(100)
y - rnorm(100)
z - rnorm(100)
loess(z ~ x * y)

loess(z ~ x) does not cause a crash using the Atlas BLAS and neither does
running the above code with the Rblas.dll that came with R 2.7.1.  In
addition, the code runs fine using the Atlas BLAS under R 2.6.2.

The windows error information that is printed to the screen when R closes:

AppName: rgui.exeAppVer: 2.71.45970.0ModName: rblas.dll
ModVer: 2.51.42199.0 Offset: 000501cc


sessionInfo returns:

R version 2.7.1 (2008-06-23) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

I checked the R FAQ, R for Windows FAQ, and the README associated with the
Atlas BLAS on CRAN and couldn't find any information related to possible
crash causes.  I've used the ATLAS BLAS for about 6 months on this machine
(it's a new machine) with R 2.6.2.

Using debug(stats:::simpleLoess), I've found that the crash occurs on the
first iteration of the line:

z - .C(R_loess_raw, as.double(y), as.double(x), 
as.double(weights), as.double(robust), as.integer(D), 
as.integer(N), as.double(span), as.integer(degree), 
as.integer(nonparametric), as.integer(order.drop.sqr), 
as.integer(sum.drop.sqr), as.double(span * cell), 
as.character(surf.stat), fitted.values = double(N), 
parameter = integer(7), a = integer(max.kd), 
xi = double(max.kd), vert = double(2 * D), vval = double((D
+ 
  1) * max.kd), diagonal = double(N), trL = double(1), 
delta1 = double(1), delta2 = double(1), as.integer(surf.stat
== 
  interpolate/exact))

After that, I'm kind of stuck in terms of tracking it down.

Thanks for any input,

Jason Law
City of Portland, OR

__
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] Different axis limits for each facet in ggplot2

2008-03-25 Thread Law, Jason
Does Hadley's response to the following post still hold for the most recent
version of ggplot2?

http://tolstoy.newcastle.edu.au/R/e2/help/07/07/21347.html#21379qlink2

I'm trying to accomplish in ggplot2 what the relation component of
scales does in lattice, e.g.,

stripplot(yield ~ variety | site, data = barley, scales = list(y =
list(relation='free')))

Thanks,

Jason Law
Statistician
City of Portland, Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203 -5452
[EMAIL PROTECTED]

__
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] Help rewriting looping structure?

2007-12-06 Thread Law, Jason
This will give you the percents in the same order as your original data (as
this is what your original code did)

apply(tdat, 2,
function(x) {
o - order(x)
oldo - order(o)
prc - cumsum(x[o]) / sum(x)
prc[oldo]
})

Jason Law
Statistician
City of Portland, Bureau of Environmental Services
Water Pollution Control Laboratory
6543 N Burlington Avenue
Portland, OR 97203 -5452
[EMAIL PROTECTED]



-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of TLowe
Sent: Thursday, December 06, 2007 10:28 AM
To: r-help@r-project.org
Subject: Re: [R] Help rewriting looping structure?



Thank you all.  That's exactly what I was looking for.



TLowe wrote:
 
 Hey Folks,
 
 Could somebody help me rewrite the following code?
 
 I am looping through all records across 5 fields to calculate the
 cumulative
 percentage of each record (relative to each individual field).
 
 Is there a way to rewrite it so I don't have to loop through each
 individual
 record?
 
 # tdat is my data frame
 # j is my field index
 # k is my record index
 # tsum is the sum of all values in field j
 # tmp is a vector containing the values in field j
 # tdat[k,paste(cpct,j,sep=)] creates new fields cpct1,...,cpct5

 
 
 for(j in 1:5) {
   tsum- sum(tdat[,j]);
   for(k in 1:nrow(tdat)) {
 td- tdat[k,j];
 tmp-tdat[,j];
 # sum values = to current value and divide by the total sum
 tdat[k,paste(cpct,j,sep=)]- sum(tmp[tmp = td]) / tsum;
   }
 }
 
 
 Thanks,
 TLowe
 

-- 
View this message in context:
http://www.nabble.com/Help-rewriting-looping-structure--tf4957267.html#a1419
8294
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.

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