Re: [R] matching a sequence in a vector?

2012-02-14 Thread Petr Savicky
On Wed, Feb 15, 2012 at 02:17:35PM +1000, Redding, Matthew wrote:
> Hi All,
> 
> 
> I've been trawling through the documentation and listserv archives on this 
> topic -- but
> as yet have not found a solution.  I'm sure this is pretty simple with R, but 
> I cannot work out how without
> resorting to ugly nested loops.
> 
> As far as I can tell, grep, match, and %in% are not the correct tools.
> 
> Question:
> given these vectors --
> patrn <- c(1,2,3,4)
> exmpl <- c(3,3,4,2,3,1,2,3,4,8,8,23,1,2,3,4,4,34,4,3,2,1,1,2,3,4)
> 
> how do I get the desired answer by finding the occurence of the pattern and 
> returning the starting indices:
> 6, 13, 23

Hi.

If the pattern is not too long, try

  m <- length(patrn)
  n <- length(exmpl)
  ind <- seq.int(length=n-m+1)
  occur <- rep(TRUE, times=n-m+1)
  for (i in seq.int(length=m)) {
  occur <- occur & (patrn[i] == exmpl[ind + i - 1])
  }
  which(occur)

  [1]  6 13 23

Hope this helps.

Petr Savicky.

__
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] Spline Question

2012-02-14 Thread Hasan Diwan
Rolf,

On 14 February 2012 21:26, Rolf Turner  wrote:
>    What did you actually *do* to create your spline?
>    Did you use spline() or splinefun()?  And if not, why not?

Yes... I used the spline() function to get a list of points that
should go though every point in the dataset, but it does not go
through each point, just most of them.

Is this what it is supposed to do? If so, my understanding of the
maths underlying it are incorrect -- this is entirely possible as
well.
-- 
Sent from my mobile device
Envoyait de mon portable

__
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] Splitting a data list into smaller data lists

2012-02-14 Thread Jeff Newmiller

Comments below.

On Tue, 14 Feb 2012, lawonga wrote:


Hi, I am new at R and have been looking for a guide on how to do this with no
avail. I have a data set similar to this:

X   Y
1   2
3   3
4   9
2   4
6   3
9   1
1   7
0   2
5   6
3   8

and I need it to be split into something like this:

X   Y
1   2
3   3
4   9
2   4
6   3

X  Y
9   1
1   7
0   2
5   6
3   8

I am thinking I need to write a loop because my data set is much larger than
this (1000) and I need it all organized into smaller data sets of 5's. Can
someone point me in the write direction?


"Right" direction?

Type
  ?split
and
  ?":"
and
  ?"%/%"
and
  ?nrow
at the R command line

Read the "Introduction to R" (I2R) focusing on vector indexing.  Whenever 
possible you should prefer to use vector indexing instead of a for loop 
(there are certainly reasons to use for loops, but in most cases where 
they might be appropriate there are iterator functions like lapply that 
lead to more succinct and possibly faster code).


To use the resulting list, you will probably want to read
  ?lapply
and/or
  ?"[["

although the discussion of lists in I2R may be more digestible.

Depending on how you want to process data in the bigger picture, you may 
find the plyr package helpful.


---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k

__
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] Spline Question

2012-02-14 Thread Rolf Turner


David:

Why do you say the OP wants a 503rd or 504th degree polynomial?
He/she wants an interpolating spline.

But that is precisely what "spline()" or "splinefun()" would give him/her.

So I don't understand the problem either!!!

To the OP:

What did you actually *do* to create your spline?
Did you use spline() or splinefun()?  And if not, why not?

Do read the posting guide!

cheers,

Rolf

On 15/02/12 15:37, David Winsemius wrote:


On Feb 14, 2012, at 4:14 PM, Hasan Diwan wrote:


dput(sensor.sample)

structure(c(1328565718.65, 1328566608.9, 1328566162.65, 1328566571.1,

snipped 5 pages of data
0.909395426580465, 0.806793813827552, 0.474927337412093, 
-0.0383057034947468,
-0.454526719533217, -0.187379201707524, 0.249983084438962, 
0.632832546743065,
-0.986399675507447, -0.970631383787144), .Dim = c(504L, 2L), 
.Dimnames = list(

NULL, c("sensors.sample", "")))

What I'd like to do is get a spline through all the points.


On the face of it that seems to be an extraordinary request. So (not 
for the first time)  I must not be understanding something. Are you 
really asking for assistance in constructing a 503rd or 504th  degree 
polynomial? And if you are, then why pose the question on a 
statistics-oriented mailing list. We only deal in approximations to 
the Truth.





I'm
thinking the spline function is my best approach. But a plot of the
spline and the points shows it does not cross every one perfectly. Do
I need to specify a particular method for this, or am I using the
wrong approach? Thanks in advance! -- H
--
Sent from my mobile device
Envoyait de mon portable





__
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] matching a sequence in a vector?

2012-02-14 Thread Redding, Matthew
Hi All,


I've been trawling through the documentation and listserv archives on this 
topic -- but
as yet have not found a solution.  I'm sure this is pretty simple with R, but I 
cannot work out how without
resorting to ugly nested loops.

As far as I can tell, grep, match, and %in% are not the correct tools.

Question:
given these vectors --
patrn <- c(1,2,3,4)
exmpl <- c(3,3,4,2,3,1,2,3,4,8,8,23,1,2,3,4,4,34,4,3,2,1,1,2,3,4)

how do I get the desired answer by finding the occurence of the pattern and 
returning the starting indices:
6, 13, 23

Suggestions very much appreciated!

Kind regards,




Matt Redding, Ph.D.
Principal Scientist
Geochemist/Soil Chemist
Queensland Primary Industries & Fisheries
DEEDI
PO Box 102, Toowoomba, 4350, Qld
ph: 0746 881372
fax: 0746 881192


DISCLAIMER**...{{dropped:15}}

__
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] Using readBin to read binary "unformatted" output files from Fortran?

2012-02-14 Thread Jooil Kim
Hello, 

I'm wondering if I can get some help with reading Fortran binary "unformatted" 
output files into R.

The Fortran output files were generated in Ubuntu 10.04 LTS using gfortran4.4, 
on a 32bit Intel Core 2 Duo 3.16 GHz machine, with little-endian and record 
marker lengths equal to 4. 

The machine I'm currently trying to read this Fortran output file is a Macbook 
Pro running Lion 10.7.3, on a 2.26 GHz Intel Core 2 Duo with 4Gb of ram. I'm 
running R 2.14.1.

Based on whatever information I could gather from the web, I've been trying out 
the following code (the name of the Fortran output file is "header", located in 
the current working directory).

> to.read <- file("header", "rb")
> readBin(to.read, "integer", n=2, size = 4, endian = "little")

Unfortunately, these commands return empty.

Am I on the right track here? Is what I'm trying to accomplish here even 
technically possible?

Any help would be greatly appreciated.

Thanks in advance,

Jooil

-- 
Jooil Kim
Postdoc Fellow
Scripps Institution of Oceanography, UC San Diego
Mailing address:
9500 Gilman Drive # 0244
La Jolla, CA 92093-0244, USA
For FedEx/UPS:
8675 Discovery Way, Vaughan Hall Rm. 447
La Jolla, CA 92037, USA



[[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] Splitting a data list into smaller data lists

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 8:02 PM, lawonga wrote:

Hi, I am new at R and have been looking for a guide on how to do  
this with no

avail. I have a data set similar to this:

X   Y
1   2
3   3
4   9
2   4
6   3
9   1
1   7
0   2
5   6
3   8


If by dataset you mean a daaframe named (for discussion purposes)  
"dfrm",  then this will give you the first five row


dfrm[1;5, ]

And this will give you the rest:

dfrm[ -(1:5), ]




and I need it to be split into something like this:

X   Y
1   2
3   3
4   9
2   4
6   3

X  Y
9   1
1   7
0   2
5   6
3   8

I am thinking I need to write a loop because my data set is much  
larger than
this (1000) and I need it all organized into smaller data sets of  
5's. Can

someone point me in the write direction?


Oh  So you want to split into packets of 5 rows each?

try:

split( dfrm, 1:NROW(dfrm) %/% 5 )

--

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] [R-SIG-Mac] txtStart creates a NULL file

2012-02-14 Thread Simon Urbanek

On Feb 14, 2012, at 8:31 PM, R. Michael Weylandt wrote:

> Like others in the referenced thread, I can reproduce this on Mac OS X
> 10.5.8 with R 2.14.1 using R.app but it works just fine from the
> Terminal CLI so it's likely something in the GUI rather than R itself:
> I'll forward this to R-SIG-Mac and see if Simon (Urbanek) picks it up.
> 

Interestingly, this is a bug in R: R_ReplDLLdo1() fails to invoke top-level 
handlers (txtStart uses top-level handler to collect values of expressions but 
the handler is never called). This may be a one-liner fix but I'll have to 
double-check tomorrow since changes in the REPL code tend to have bad 
consequences.

Thanks,
Simon


> 
> On Tue, Feb 14, 2012 at 3:50 PM, Simon Kiss  wrote:
>> Hello all:
>> I'm trying to use the following code to get commands, comments and results 
>> to a .txt file.  It only appears to capture comments. When I comment those 
>> out with #, it creates a NULL file.
>> Someone seemed to have a similar problem with a mac GUI 
>> (https://stat.ethz.ch/pipermail/r-help/2010-September/253177.html) but the 
>> result seemed to be ambiguous. Is there a work-around? Reproducible code and 
>> sessioninfo are below.   The OS is Mac OS 10.6.8.
>> 
>> Yours truly, Simon Kiss
>> 
>> install.packages("HSAUR")
>> library(HSAUR)
>> library(TeachingDemos)
>> data("Forbes2000", package="HSAUR")
>> #This is a test of R output for the blind
>> txtStart('test.txt', commands=TRUE, results=TRUE)
>> txtComment('This command provides the mean profit in the data set')
>> mean(Forbes2000$profits, na.rm=TRUE)
>> txtComment('This command provides the standard deviation of the profits data 
>> set')
>> sd(Forbes2000$profits, na.rm=TRUE)
>> txtComment('This command provides the average profit by country')
>> aggregate(Forbes2000$profits, by=list(Forbes2000$country), function(x) 
>> mean(x, na.rm=TRUE))
>> txtStop()
>> 
>> 
>> SessionInfo()
>> 
>> R version 2.13.2 (2011-09-30)
>> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>> 
>> locale:
>> [1] en_CA.UTF-8/en_CA.UTF-8/C/C/en_CA.UTF-8/en_CA.UTF-8
>> 
>> attached base packages:
>> [1] stats graphics  grDevices utils datasets  methods   base
>> 
>> other attached packages:
>> [1] TeachingDemos_2.7
>> 
>> loaded via a namespace (and not attached):
>> [1] tools_2.13.2
>> 
>> __
>> 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-SIG-Mac mailing list
> r-sig-...@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
> 
> 

__
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] Warning When Importing SPSS Files XXXX

2012-02-14 Thread Joseph Gonzalez
Dear Forum,

I am absolutely new to R and have been trying to obtain a deeper
understanding of data acquisition using various files types.  I have been
using the following code to import an SPSS file.  The import appears to
work successfully, however I receive a warning in the console which I am
not entirely sure how to correct.  Could anyone please provide insight?
 Any guidance would be greatly appreciated.

human4<-read.spss("human4.sav", use.value.labels = TRUE, to.data.frame =
TRUE,
  max.value.labels = Inf, trim.factor.names = FALSE,
  trim_values = TRUE, reencode = NA, use.missings = to.data.frame)

Warning message:
In read.spss("human4.sav", use.value.labels = TRUE, to.data.frame = TRUE,  :
  human4.sav: Compression bias (0) is not the usual value of 100



Most Respectfully,

Joseph

-- 
Joseph B. Gonzalez
_

Joseph B. Gonzalez

Email: jbgonzale...@gmail.com

Cell: 404.201.1873

[[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] Splitting a data list into smaller data lists

2012-02-14 Thread lawonga
Hi, I am new at R and have been looking for a guide on how to do this with no
avail. I have a data set similar to this:

X   Y
1   2
3   3
4   9
2   4
6   3
9   1
1   7
0   2
5   6
3   8

and I need it to be split into something like this:

X   Y
1   2
3   3
4   9
2   4
6   3

X  Y
9   1
1   7
0   2
5   6
3   8

I am thinking I need to write a loop because my data set is much larger than
this (1000) and I need it all organized into smaller data sets of 5's. Can
someone point me in the write direction?

Thanks,
Andy

--
View this message in context: 
http://r.789695.n4.nabble.com/Splitting-a-data-list-into-smaller-data-lists-tp4389142p4389142.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Spline Question

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 4:14 PM, Hasan Diwan wrote:


dput(sensor.sample)

structure(c(1328565718.65, 1328566608.9, 1328566162.65, 1328566571.1,

snipped 5 pages of data
0.909395426580465, 0.806793813827552, 0.474927337412093,  
-0.0383057034947468,
-0.454526719533217, -0.187379201707524, 0.249983084438962,  
0.632832546743065,
-0.986399675507447, -0.970631383787144), .Dim = c(504L,  
2L), .Dimnames = list(

NULL, c("sensors.sample", "")))

What I'd like to do is get a spline through all the points.


On the face of it that seems to be an extraordinary request. So (not  
for the first time)  I must not be understanding something. Are you  
really asking for assistance in constructing a 503rd or 504th  degree  
polynomial? And if you are, then why pose the question on a statistics- 
oriented mailing list. We only deal in approximations to the Truth.





I'm
thinking the spline function is my best approach. But a plot of the
spline and the points shows it does not cross every one perfectly. Do
I need to specify a particular method for this, or am I using the
wrong approach? Thanks in advance! -- H
--
Sent from my mobile device
Envoyait de mon portable



--
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] how to test the random factor effect in lme

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 5:36 PM, Xiang Gao wrote:


Hi

I am working on a Nested one-way ANOVA. I don't know how to implement
R code to test the significance of the random factor


Have you read what the unofficial Mixed Model FAQ says about testing  
for significance on random effects?


http://glmm.wikidot.com/faq



My R code so far can only test the fixed factor :


That may be the intent of the authors. They may want to make it  
sufficiently  difficult so that an adequate barrier prevents the  
unwary from taking some "easy way out". You probably need to describe  
your study (assuming this is not an assigned homework exercise) in  
sufficient scientific detail and do so on the mixed-models mailing list.


--
David.



anova(lme(PCB~Area,random=~1|Sites, data = PCBdata))
   numDF denDF   F-value p-value
(Intercept) 112 1841.7845  <.0001
Area  1 44.9846  0.0894


Here is my data and my hand calculation.


PCBdata

  Area Sites PCB
1 A 1  18
2 A 1  16
3 A 1  16
4 A 2  19
5 A 2  20
6 A 2  19
7 A 3  18
8 A 3  18
9 A 3  20
10B 4  21
11B 4  20
12B 4  18
13B 5  19
14B 5  20
15B 5  21
16B 6  19
17B 6  23
18B 6  21

By hand calculation, the result should be:
Source  SS  DF  MS
Areas  18.00  118.00
Sites14.44  43.61
Error20.67  12  1.72
Total   53.11   17   ---


MSareas/MSsites = 4.99 --- matching the R output
MSsites/MSE = 2.10
Conclusion is that Neither of Areas nor Sites make differences.


My R code so far can only test the fixed effect :

anova(lme(PCB~Area,random=~1|Sites, data = PCBdata))
   numDF denDF   F-value p-value
(Intercept) 112 1841.7845  <.0001
Area  1 44.9846  0.0894



--
Xiang Gao, Ph.D.
Department of Biology
University of North Texas

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


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] txtStart creates a NULL file

2012-02-14 Thread R. Michael Weylandt
Like others in the referenced thread, I can reproduce this on Mac OS X
10.5.8 with R 2.14.1 using R.app but it works just fine from the
Terminal CLI so it's likely something in the GUI rather than R itself:
I'll forward this to R-SIG-Mac and see if Simon (Urbanek) picks it up.

Michael

On Tue, Feb 14, 2012 at 3:50 PM, Simon Kiss  wrote:
> Hello all:
> I'm trying to use the following code to get commands, comments and results to 
> a .txt file.  It only appears to capture comments. When I comment those out 
> with #, it creates a NULL file.
> Someone seemed to have a similar problem with a mac GUI 
> (https://stat.ethz.ch/pipermail/r-help/2010-September/253177.html) but the 
> result seemed to be ambiguous. Is there a work-around? Reproducible code and 
> sessioninfo are below.   The OS is Mac OS 10.6.8.
>
> Yours truly, Simon Kiss
>
> install.packages("HSAUR")
> library(HSAUR)
> library(TeachingDemos)
> data("Forbes2000", package="HSAUR")
> #This is a test of R output for the blind
> txtStart('test.txt', commands=TRUE, results=TRUE)
> txtComment('This command provides the mean profit in the data set')
> mean(Forbes2000$profits, na.rm=TRUE)
> txtComment('This command provides the standard deviation of the profits data 
> set')
> sd(Forbes2000$profits, na.rm=TRUE)
> txtComment('This command provides the average profit by country')
> aggregate(Forbes2000$profits, by=list(Forbes2000$country), function(x) 
> mean(x, na.rm=TRUE))
> txtStop()
>
>
> SessionInfo()
>
> R version 2.13.2 (2011-09-30)
> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
>
> locale:
> [1] en_CA.UTF-8/en_CA.UTF-8/C/C/en_CA.UTF-8/en_CA.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] TeachingDemos_2.7
>
> loaded via a namespace (and not attached):
> [1] tools_2.13.2
>
> __
> 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] Puzzling... puzzling... puzzling...

2012-02-14 Thread Michael
Thanks a lot guys!

On Mon, Feb 13, 2012 at 1:26 PM, William Dunlap  wrote:

> Replace the syntax List$Name with List[["Name"]]
> and see if things work better.
>
> '[[' does not do the partial matching that '$' does.
> E.g.,
>   x <- list(AB=10, BC=20, CD=30)
>   x$A # returns 10 because "A" is the initial part of exactly one name in
> x, "AB"
>   x[["A"]] # returns NULL
>
> However, if you have
>   y <- list(AB=1, AC=2, AD=3)
> then y$A will return NULL because there is not a unique partial
> match to "A" among the names of y.
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
> > -Original Message-
> > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
> On Behalf Of Michael
> > Sent: Monday, February 13, 2012 11:00 AM
> > To: r-help
> > Subject: [R] Puzzling... puzzling... puzzling...
> >
> >  Hi all,
> >
> > I made sure that it's "env$sRes1$nPositionsOptimizedM" that's correct...
> >
> > not the "env$sRes1$nPositionsOptimized"...
> >
> > But it seems both point to the same memory area...
> >
> > This is very dangerous because I have used naming conventions such as:
> >
> > MyLongVariableNameForA
> >  MyLongVariableNameForB
> >  MyLongVariableNameForC
> > ...
> > ...
> >
> > Then if internally they are actually the same thing then all my programs
> > messed up...
> >
> > Any thoughts?
> >
> > >env=new.env()
> >
> > >load("MyResults.rData", env)
> >
> > >identical(env$sRes1$nPositionsOptimized, env$sRes1$nPositionsOptimizedM)
> >
> > [1] TRUE
> >
> >   [[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] Color cells of a matrix as in Excel

2012-02-14 Thread baptiste auguie
A minimum code to plot a coloured matrix with text labels could be the
following:


library(grid)
library(scales)
library(RColorBrewer)


diverging_palette <- function(d = NULL, centered = FALSE, midpoint = 0,
  colors = brewer.pal(7,"PRGn")){

  half <- length(colors)/2

  if(!length(colors)%%2) stop("requires odd number of colors")

  values <-  if(centered) {
low <- seq(min(d), midpoint, length=half)
high <- seq(midpoint, max(d), length=half)
c(low[-length(low)], midpoint, high[-1])
  } else {
mabs <- max(abs(d - midpoint))
seq(midpoint-mabs, midpoint + mabs, length=length(colors))
  }

  gradient_n_pal(colors, values = values)

}


matrixGrob <- function(d){

  nc <- ncol(d)
  nr <- nrow(d)

  palet <- diverging_palette(d, center=FALSE)

  fill.matrix <- palet(d)
  dim(fill.matrix) <- dim(d)

  ## matrix of square tiles
  rg <- rasterGrob(fill.matrix, width=unit(1, "npc"), height=unit(1,
"npc"), interpolate = FALSE)
  ## position of the labels
  xy <- expand.grid(y = rescale(seq_len(nr), c(0.5/nr, 1 - 0.5/nr)   ),
x = rescale(seq_len(nc), c(0.5/nc, 1 - 0.5/nc))
)

  ## text
  tg <- textGrob(label=round(d, 3), x=xy$x, y= 1 - xy$y, def="npc")

  gTree(children=gList(rg, tg))
}


grid.matrix <- function(d)
  grid.draw(matrixGrob(d))

d <- cbind(x=rnorm(10), y=rnorm(10))
grid.newpage()
grid.matrix(d)

I don't think it would be very hard to adapt to your needs, basically
you need to add rownames on top, apply() the scale to each column
independently (as opposed to the full matrix at the moment) and
combine the results in a fill.matrix. Having one colour scale across
the matrix makes more sense, imho.


HTH,

b.


On 15 February 2012 11:21, John Nicholas  wrote:
> All,
>
> I frequently make spreadsheets in Excel in which I rank values in columns
> by stop-light colors (red is bad, yellow is OK, green is good).
>
> Image and heatmap expect a matrix in which all the data are in the same
> scale, but I frequently have different scales in different columns. ie.
> Column one runs from 1-10 while column 2 runs from 1-100. I thus need to
> define a separate color ramp for each column. In addition, sometimes the
> smaller numbers are colored green, while sometimes the larger numbers are
> preferred and colored green. I also want to print in each cell the numeric
> value, and I need to show row names. A crude example without the color:
>
> RowNames        Col1     Col2
> --
> Row1                   1          1
> Row2                   5         50
> Row3                  7.5       80
> Row4                  10        99
> ..
>
> Is there any R package that can do something similar? Can I create a plot
> that is a matrix of rectangles and get color and text that way?
>
> Any help is greatly appreciated.
>
> Thanks,
>
> John
>
>        [[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] Creating categories from a date-time object

2012-02-14 Thread jim holtman
Will this do it for you:

> tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-09-02 03:00'), 
> by='45 min')
> # get just the hours
> hours <- format(tmp, "%H")
> weekdays <- format(tmp, "%w") %in% c('1', '2', '3', '4','5')
> office <- (hours >= '08') & (hours < '18') & weekdays
> result <- data.frame(time = tmp, office = office)
> head(result, 40)
  time office
1  2011-08-01 13:00:00   TRUE
2  2011-08-01 13:45:00   TRUE
3  2011-08-01 14:30:00   TRUE
4  2011-08-01 15:15:00   TRUE
5  2011-08-01 16:00:00   TRUE
6  2011-08-01 16:45:00   TRUE
7  2011-08-01 17:30:00   TRUE
8  2011-08-01 18:15:00  FALSE
9  2011-08-01 19:00:00  FALSE
10 2011-08-01 19:45:00  FALSE
11 2011-08-01 20:30:00  FALSE
12 2011-08-01 21:15:00  FALSE
13 2011-08-01 22:00:00  FALSE
14 2011-08-01 22:45:00  FALSE
15 2011-08-01 23:30:00  FALSE
16 2011-08-02 00:15:00  FALSE
17 2011-08-02 01:00:00  FALSE
18 2011-08-02 01:45:00  FALSE
19 2011-08-02 02:30:00  FALSE
20 2011-08-02 03:15:00  FALSE
21 2011-08-02 04:00:00  FALSE
22 2011-08-02 04:45:00  FALSE
23 2011-08-02 05:30:00  FALSE
24 2011-08-02 06:15:00  FALSE
25 2011-08-02 07:00:00  FALSE
26 2011-08-02 07:45:00  FALSE
27 2011-08-02 08:30:00   TRUE
28 2011-08-02 09:15:00   TRUE
29 2011-08-02 10:00:00   TRUE
30 2011-08-02 10:45:00   TRUE
31 2011-08-02 11:30:00   TRUE
32 2011-08-02 12:15:00   TRUE
33 2011-08-02 13:00:00   TRUE
34 2011-08-02 13:45:00   TRUE
35 2011-08-02 14:30:00   TRUE
36 2011-08-02 15:15:00   TRUE
37 2011-08-02 16:00:00   TRUE
38 2011-08-02 16:45:00   TRUE
39 2011-08-02 17:30:00   TRUE
40 2011-08-02 18:15:00  FALSE
> View(result)


On Tue, Feb 14, 2012 at 2:35 PM, Jose Bustos Melo  wrote:
> Hello R-List,
>
> I have a question about recoding from a date time object.  I have tried using 
> as.POSIXct objects and Chron Objects, but I can get the what I want.
>
> I need to create a new variable from a date-time object, adding "Office Time" 
> for those events that happens between 08:00:00 to 18:00:00 and "Out of 
> Office" all the others, but not including weekends. I have created a fake 
> data.
>
> tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-09-02 03:00'), 
> by='45 min')
>
> Is there any valid way to do it? I have spend so much time without any good 
> luck and I don't have any good code to show!
> Thanks in advance!
> José
>
>        [[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?
Tell me what you want to do, not how you want to do it.

__
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] Color cells of a matrix as in Excel

2012-02-14 Thread jim holtman
Check out the XLConnect package for formatting a spreadsheet.

On Tue, Feb 14, 2012 at 5:21 PM, John Nicholas  wrote:
> All,
>
> I frequently make spreadsheets in Excel in which I rank values in columns
> by stop-light colors (red is bad, yellow is OK, green is good).
>
> Image and heatmap expect a matrix in which all the data are in the same
> scale, but I frequently have different scales in different columns. ie.
> Column one runs from 1-10 while column 2 runs from 1-100. I thus need to
> define a separate color ramp for each column. In addition, sometimes the
> smaller numbers are colored green, while sometimes the larger numbers are
> preferred and colored green. I also want to print in each cell the numeric
> value, and I need to show row names. A crude example without the color:
>
> RowNames        Col1     Col2
> --
> Row1                   1          1
> Row2                   5         50
> Row3                  7.5       80
> Row4                  10        99
> ..
>
> Is there any R package that can do something similar? Can I create a plot
> that is a matrix of rectangles and get color and text that way?
>
> Any help is greatly appreciated.
>
> Thanks,
>
> John
>
>        [[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?
Tell me what you want to do, not how you want to do it.

__
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] Color cells of a matrix as in Excel

2012-02-14 Thread Kenneth Frost
Hi, John-
Sorry if my last email was not clear.  In R I imagine you having two matrices. 
One with the raw values and one with values scaled or standardized by column. 
The matrix with the values scaled by column is used to make the heatmap (i.e. 
color the image) and the matrix with the raw values is then overlaid on the 
heatmap. The details of the image or heatmap functions escape me as I have not 
worked with them for several years. Without doing this myself (and without a 
lot of time to try it out), I cannot speak for its difficulty, although I don't 
imagine it being too tedious. It is likely that others will have good ideas and 
I have copied this back to the list to hopefully find out...
Ken       

On 02/14/12, John Nicholas   wrote:
> I can do that for the color ramp (although a bit tedious to do for each new 
> spreadsheet), but then how do I get the actual values printed in the cells?
> 
> 
> 
> 
> 
> On Tue, Feb 14, 2012 at 2:35 PM, Kenneth Frost  > wrote:
> 
> 
> 
> > Hi, John-
> > 
> 
> > Would it be possible to scale the values in each column to fall between 0 
> > and 1 (or standardize them) and use a single color ramp?
> > 
> 
> > Ken
> > 
> 
> > 
> > 
> 
> > On 02/14/12, John Nicholas   wrote:
> > 
> 
> > > All,
> > 
> 
> > >
> > 
> 
> > > I frequently make spreadsheets in Excel in which I rank values in columns
> > 
> 
> > > by stop-light colors (red is bad, yellow is OK, green is good).
> > 
> 
> > >
> > 
> 
> > > Image and heatmap expect a matrix in which all the data are in the same
> > 
> 
> > > scale, but I frequently have different scales in different columns. ie.
> > 
> 
> > > Column one runs from 1-10 while column 2 runs from 1-100. I thus need to
> > 
> 
> > > define a separate color ramp for each column. In addition, sometimes the
> > 
> 
> > > smaller numbers are colored green, while sometimes the larger numbers are
> > 
> 
> > > preferred and colored green. I also want to print in each cell the numeric
> > 
> 
> > > value, and I need to show row names. A crude example without the color:
> > 
> 
> > >
> > 
> 
> > > RowNames        Col1     Col2
> > 
> 
> > > --
> > 
> 
> > > Row1                   1          1
> > 
> 
> > > Row2                   5         50
> > 
> 
> > > Row3                  7.5       80
> > 
> 
> > > Row4                  10        99
> > 
> 
> > > ..
> > 
> 
> > >
> > 
> 
> > > Is there any R package that can do something similar? Can I create a plot
> > 
> 
> > > that is a matrix of rectangles and get color and text that way?
> > 
> 
> > >
> > 
> 
> > > Any help is greatly appreciated.
> > 
> 
> > >
> > 
> 
> > > Thanks,
> > 
> 
> > >
> > 
> 
> > > John
> > 
> 
> > >
> > 
> 
> > 
> > 
> > >       [[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] Wildcard for indexing?

2012-02-14 Thread Greg Snow
Note that you can also do logical comparisons with the results of grepl like:

grepl('^as', a) | grepl('^df',a)

For the given example it is probably simplest to do it in the regular
expression as shown, but for some more complex cases (or including
other variables) the logic with the output may be simpler.

On Tue, Feb 14, 2012 at 8:23 AM, Johannes Radinger  wrote:
>
>
>  Original-Nachricht 
>> Datum: Tue, 14 Feb 2012 10:18:33 -0500
>> Von: Sarah Goslee 
>> An: Johannes Radinger 
>> CC: R-help@r-project.org
>> Betreff: Re: [R] Wildcard for indexing?
>
>> Hi,
>>
>> You should probably do a bit of reading about regular expressions, but
>> here's one way:
>>
>> On Tue, Feb 14, 2012 at 10:10 AM, Johannes Radinger 
>> wrote:
>> > Hi,
>> >
>> >  Original-Nachricht 
>> >> Datum: Tue, 14 Feb 2012 09:59:39 -0500
>> >> Von: "R. Michael Weylandt" 
>> >> An: Johannes Radinger 
>> >> CC: R-help@r-project.org
>> >> Betreff: Re: [R] Wildcard for indexing?
>> >
>> >> I think the grep()-family (regular expressions) will be the easiest
>> >> way to do this, though it sounds like you might prefer grepl() which
>> >> returns a logical vector:
>> >>
>> >> ^[AB] # Starts with either an A or a B
>> >> ^A_ # Starting with A_
>> >>
>> >> a <-  c("A_A","A_B","C_A","BB","A_Asd"
>> >> grepl("^[AB]", a)
>> >> grepl("^A_")
>> >
>> > Yes grepl() is what I am looking for.
>> > is there also something like an OR statement e.g. if I want to
>> > select for elements that start with "as" OR "df"?
>>
>> > a <- c("as1", "bb", "as2", "cc", "df", "aa", "dd", "sdf")
>> > grepl("^as|^df", a)
>> [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
>>
>>
>> The square brackets match any of those characters, so are good
>> for single characters. For more complex patterns, | is the or symbol.
>> ^ marks the beginning.
>
> Thank you so much Sarah! I tried that | symbol intuitively, there was just a 
> problem with the quotation marks :(
>
> Now everything is solved...
>
> /johannes
>
>>
>> Sarah
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>
> --
>
> __
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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.


Re: [R] Plotting function image

2012-02-14 Thread ilai
In the absence of data

coords <- expand.grid(lat=1:5,long=1:5)
coords$z <- rnorm(25)
Coords<- unstack(coords,z~long)
image(as.matrix(Coords))


On Tue, Feb 14, 2012 at 10:36 AM, uday  wrote:
> I have some data set which has latitude, longitude and Z values.
> I would like to plot them on global map.
> I am thinking to use image
> image(x, y, z, zlim, xlim, ylim, col = heat.colors(12),
>      add = FALSE, xaxs = "i", yaxs = "i", xlab, ylab,
>      breaks, oldstyle = FALSE, useRaster = FALSE, ...)
>
> here I have x = longitude
>                       y= latitude
>                      z = z
>
> but z suppose to be matrix but my z values are  single array , could
> somebody please tell me how to convert these single array of z to matrix ?
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Plotting-function-image-tp4387796p4387796.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
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] testing for a distribution of probability

2012-02-14 Thread Greg Snow
All the distribution tests are rule out tests, i.e. they can tell you
if your data does not match a given distribution, but they can never
tell you that the data does come from a specific distribution.

Note also that the results of any of these studies may not be that
useful, for small sample sizes it is more important to rule out a
given distribution, but unless there is a huge difference you won't
have much power to do this.  For large sample sizes it is less
important because using a close distribution will generally give you
robust results, but you will have power to detect small, meaningless
differences.  So often your choice is between a meaningless answer to
a meaningful question or a meaningful answer to a meaningless
question.

What is more important and a better approach is to understand the
science behind the process that generated the data and use that
knowledge to find a distribution that is reasonable (even if not
exact) or to use techniques that make fewer assumptions about the
distribution if you cannot find something close enough to be
reasonable (e.g. bootstrap, permutation, other non-parametric,
simulations to determine cut-off values).



On Tue, Feb 14, 2012 at 4:21 AM, Bianca A Santini
 wrote:
> Hello!
> I have several variables. Each of them has a different distribution. I was
> thinking to use a Generalized Linear Model, glm(), but I need to introduce
> the family. Do you know if R has any tests for matching data to any
> distribution ( I am aware of shapiro.test).
>
> All the best,
>
>
> --
> BAS
>
>        [[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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] how to test the random factor effect in lme

2012-02-14 Thread Xiang Gao
Hi

I am working on a Nested one-way ANOVA. I don't know how to implement
R code to test the significance of the random factor

My R code so far can only test the fixed factor :

anova(lme(PCB~Area,random=~1|Sites, data = PCBdata))
numDF denDF   F-value p-value
(Intercept) 112 1841.7845  <.0001
Area  1 44.9846  0.0894


Here is my data and my hand calculation.

> PCBdata
   Area Sites PCB
1 A 1  18
2 A 1  16
3 A 1  16
4 A 2  19
5 A 2  20
6 A 2  19
7 A 3  18
8 A 3  18
9 A 3  20
10B 4  21
11B 4  20
12B 4  18
13B 5  19
14B 5  20
15B 5  21
16B 6  19
17B 6  23
18B 6  21

By hand calculation, the result should be:
Source  SS  DF  MS
Areas  18.00  118.00
Sites14.44  43.61
Error20.67  12  1.72
Total   53.11   17   ---


MSareas/MSsites = 4.99 --- matching the R output
MSsites/MSE = 2.10
Conclusion is that Neither of Areas nor Sites make differences.


My R code so far can only test the fixed effect :

anova(lme(PCB~Area,random=~1|Sites, data = PCBdata))
numDF denDF   F-value p-value
(Intercept) 112 1841.7845  <.0001
Area  1 44.9846  0.0894



-- 
Xiang Gao, Ph.D.
Department of Biology
University of North Texas

__
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] Color cells of a matrix as in Excel

2012-02-14 Thread Kenneth Frost
Hi, John-
Would it be possible to scale the values in each column to fall between 0 and 1 
(or standardize them) and use a single color ramp?
Ken

On 02/14/12, John Nicholas   wrote:
> All,
> 
> I frequently make spreadsheets in Excel in which I rank values in columns
> by stop-light colors (red is bad, yellow is OK, green is good).
> 
> Image and heatmap expect a matrix in which all the data are in the same
> scale, but I frequently have different scales in different columns. ie.
> Column one runs from 1-10 while column 2 runs from 1-100. I thus need to
> define a separate color ramp for each column. In addition, sometimes the
> smaller numbers are colored green, while sometimes the larger numbers are
> preferred and colored green. I also want to print in each cell the numeric
> value, and I need to show row names. A crude example without the color:
> 
> RowNamesCol1 Col2
> --
> Row1   1  1
> Row2   5 50
> Row3  7.5   80
> Row4  1099
> ..
> 
> Is there any R package that can do something similar? Can I create a plot
> that is a matrix of rectangles and get color and text that way?
> 
> Any help is greatly appreciated.
> 
> Thanks,
> 
> John
> 
>   [[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] Filling out a data frame row by row.... slow!

2012-02-14 Thread William Dunlap
If you must repeatedly append rows to a data.frame,
try making the dataset you are filling in a bunch
of independent vectors, perhaps in a new environment
to keep things organized, and expand each at the same time.
At the very end make a data.frame out of those vectors.
E.g., change the likes of

f0 <- function (nRow) 
{
incrSize <- 1
curSize <- 1
data <- data.frame(x = numeric(curSize), y = numeric(curSize), 
z = numeric(curSize))
for (i in seq_len(nRow)) {
if (i > curSize) {
data <- rbind(data, data.frame(x = numeric(incrSize), 
y = numeric(incrSize), z = numeric(incrSize)))
curSize <- nrow(data)
}
data[i, ] <- c(i + 0.1, i + 0.2, i + 0.3)
}
data[seq_len(nRow), , drop = FALSE]
}

to

f1 <- function (nRow) 
{
incrSize <- 1
curSize <- min(1, nRow)
data <- as.environment(list(x = numeric(curSize), y = numeric(curSize), 
z = numeric(curSize)))
for (i in seq_len(nRow)) {
if (i > curSize) {
curSize <- min(curSize + incrSize, nRow)
for (name in objects(data)) {
length(data[[name]]) <- curSize
}
}
data$x[i] <- i + 0.1
data$y[i] <- i + 0.2
data$z[i] <- i + 0.3
}
data.frame(as.list(data)) # use x=data$x, y=data$y, ... if order is 
important.
}

Here are some timing results for the above functions
> system.time(r1 <- f1(5000))
   user  system elapsed 
   0.130.000.14 
> system.time(r1 <- f1(15000))
   user  system elapsed 
   0.330.000.32 
> system.time(r1 <- f1(25000))
   user  system elapsed 
   0.510.000.47 
> 
> system.time(r0 <- f0(5000))
   user  system elapsed 
   5.230.025.13 
> system.time(r0 <- f0(15000))
   user  system elapsed 
  21.750.00   20.67 
> system.time(r0 <- f0(25000))
   user  system elapsed 
  87.310.01   86.00
> # results are same, except for the order of the columns
> all.equal(r0[, c("x","y","z")], r1[, c("x","y","z")])
[1] TRUE

For 2 million rows f1 is getting a little superlinear: 2e6/25000 * .5 = 40 
seconds, if time linear in nRow, but I get 55 s.
> system.time(r1 <- f1(2e6)) 
   user  system elapsed 
  52.193.81   54.69

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Peter Meilstrup
> Sent: Tuesday, February 14, 2012 1:47 PM
> To: r-help@r-project.org
> Subject: [R] Filling out a data frame row by row slow!
> 
> I'm reading a file and using the file to populate a data frame. The way the
> file is laid out, I need to fill in the data frame one row at a time.
> 
> When I start reading my file, I don't know how many rows I will need. It's
> on the order of a million.
> 
> Being mindful of the time expense of reallocation, I decided on a strategy
> of doubling the data frame size every time I needed to expand it ...
> therefore memory is never more than 50% wasted, and it should still finish
> in O(N) time.
> 
> But it still, somehow has an O(N^2) performance characteristic. It seems
> like just setting a single element is slow in a larger data frame as
> compared to a smaller one. Here is a toy function to illustrate,
> reallocating and filling in single rows in a data frame, and shows the
> slowdown:
> 
> populate.data.frame.test <- function(n=100, chunk=1000) {
>   i = 0;
>   df <- data.frame(a=numeric(0), b=numeric(0), c=numeric(0));
>   t <- proc.time()[2]
>   for (i in 1:n) {
> if (i %% chunk == 0) {
>   elapsed <- -(t - (t <- proc.time()[2]))
>   cat(sprintf("%d rows: %g rows per sec, nrows = %d\n", i,
> chunk/elapsed, nrow(df)))
> 
> }
> 
> ##double data frame size if necessary
> while (nrow(df)   df[max(i, 2*nrow(df)),] <- NA
>   cat(sprintf("Doubled to %d rows\n", nrow(df)));
> }
> 
> ##fill in one row
> df[i, c('a', 'b', 'c')] <- list(runif(1), i, runif(1))
>   }
> }
> 
> Is there a way to do this that avoids the slowdown? The data cannot be
> represented as a matrix (different columns have different types.)
> 
> Peter
> 
>   [[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.


[R] Color cells of a matrix as in Excel

2012-02-14 Thread John Nicholas
All,

I frequently make spreadsheets in Excel in which I rank values in columns
by stop-light colors (red is bad, yellow is OK, green is good).

Image and heatmap expect a matrix in which all the data are in the same
scale, but I frequently have different scales in different columns. ie.
Column one runs from 1-10 while column 2 runs from 1-100. I thus need to
define a separate color ramp for each column. In addition, sometimes the
smaller numbers are colored green, while sometimes the larger numbers are
preferred and colored green. I also want to print in each cell the numeric
value, and I need to show row names. A crude example without the color:

RowNamesCol1 Col2
--
Row1   1  1
Row2   5 50
Row3  7.5   80
Row4  1099
..

Is there any R package that can do something similar? Can I create a plot
that is a matrix of rectangles and get color and text that way?

Any help is greatly appreciated.

Thanks,

John

[[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] sequential sum

2012-02-14 Thread C Lin

Dan,
 
Thank you very much. That is exactly what I need. What a clever solution.
 
David, thanks for trying.
 
Thank you all. I would never be able to figure it out on my own. 
 
Lin

 

> From: dwinsem...@comcast.net
> To: nord...@dshs.wa.gov
> Date: Tue, 14 Feb 2012 14:28:21 -0500
> CC: r-help@r-project.org
> Subject: Re: [R] sequential sum
> 
> 
> On Feb 14, 2012, at 1:38 PM, Nordlund, Dan (DSHS/RDA) wrote:
> 
> >> -Original Message-
> >> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> >> project.org] On Behalf Of baccts
> >> Sent: Tuesday, February 14, 2012 9:04 AM
> >> To: r-help@r-project.org
> >> Subject: [R] sequential sum
> >>
> >> Dear R users,
> >>
> >> I am trying to sum number that exist in another vector up to i, then
> >> increment i and repeat.
> >> Sorry. It's hard to explain but basically I am trying to do the
> >> following:
> >> test <- c(1,2,3,4);
> >> test2 <- c(3,5,6,7,2,8,8,4,4);
> >> test3 <- c(10,20,30,40);
> >> tmp <- 0;
> >> for (i in 1:length(test)){
> >> tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
> >> }
> >>
> >> so when i = 1, tmp[i] = 0 because test[1]=1 is not in test2 and
> >> when i = 3, tmp[i] = 50 = test3[2] + test3[3] because test[2:3] is in
> >> test2
> >>
> >> Problem is test has 5000+ entries. How do I do the same thing without
> >> the
> >> loop?
> >>
> >> Thanks in advance.
> >>
> >
> > Your example data is not very extensive so I don't know if this 
> > solution is general enough. But, it does work with your data.
> 
> I had exactly that concern so tested my solution, which was a bit 
> different than yours:
> 
> test <- c(1,2,3,4,10, 5);
> test2 <- c(3,5,6,7,2,8,8,4,4);
> test3 <- c(10,20,30,40, 50, 60);
> tmp <- 0;
> for (i in 1:length(test)){
> tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
> }
> 
> > tmp
> [1] 0 20 50 90 90 150
> > tmp2 <- ifelse(test %in% test2, cumsum(test3[test %in% test2]), 0)
> > tmp2
> [1] 0 50 90 150 0 50 # was going to throw away.
> 
> 
> > tmp3 <- cumsum(ifelse(test %in% test2, test3, 0))
> > tmp3
> [1] 0 20 50 90 90 150
> 
> So yours is better at spanning the non-match entries in the same 
> manner as the for-loop.
> 
> 
> >
> >
> > Hope this is helpful,
> >
> > Dan
> >
> > Daniel J. Nordlund
> 
> 
> 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.
  
[[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] Strange plotting error

2012-02-14 Thread Duncan Murdoch

On 12-02-14 3:13 PM, Diviya Smith wrote:

Hi there,

I am trying to compute the autocorrelation in a dataset using R's acf
function. ACF automatically plots the results. This works well except in
some cases xlim doesnt work

data<- rnorm(2000,0,1)
acf(data,xlim=c(1,10))  # works - the plot starts at 1
acf(data,lag=100,xlim=c(1,100))   # this does not work and the plot still
starts at 0

Is there another way to specify the xlim or the starting value for x? Any
help would be most appreciated.



I think you misunderstand what "xlim" controls.  It sets the limit of 
the plot region that's guaranteed to be included, but the region is 
normally expanded.  It doesn't control what is plotted unless it falls 
outside the expanded region.


There are probably two ways to do what you want.  You can force the 
region not to expand, using xaxs="i" in the call.  This won't adjust the 
y axis to a smaller range; you'll need to do that yourself.


The other way is to manipulate the acf object to drop the lag 0 values; 
it is a pretty smart object, so you can do that using


x <- acf(data, lag=100, plot=FALSE)[1:100]  #only lags 1:100
plot(x)

Duncan Murdoch

__
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] Filling out a data frame row by row.... slow!

2012-02-14 Thread Peter Meilstrup
I'm reading a file and using the file to populate a data frame. The way the
file is laid out, I need to fill in the data frame one row at a time.

When I start reading my file, I don't know how many rows I will need. It's
on the order of a million.

Being mindful of the time expense of reallocation, I decided on a strategy
of doubling the data frame size every time I needed to expand it ...
therefore memory is never more than 50% wasted, and it should still finish
in O(N) time.

But it still, somehow has an O(N^2) performance characteristic. It seems
like just setting a single element is slow in a larger data frame as
compared to a smaller one. Here is a toy function to illustrate,
reallocating and filling in single rows in a data frame, and shows the
slowdown:

populate.data.frame.test <- function(n=100, chunk=1000) {
  i = 0;
  df <- data.frame(a=numeric(0), b=numeric(0), c=numeric(0));
  t <- proc.time()[2]
  for (i in 1:n) {
if (i %% chunk == 0) {
  elapsed <- -(t - (t <- proc.time()[2]))
  cat(sprintf("%d rows: %g rows per sec, nrows = %d\n", i,
chunk/elapsed, nrow(df)))

}

##double data frame size if necessary
while (nrow(df)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] Spline Question

2012-02-14 Thread Hasan Diwan
> dput(sensor.sample)
structure(c(1328565718.65, 1328566608.9, 1328566162.65, 1328566571.1,
1328566598.85, 1328565634.3, 1328566513.95, 1328565123.65, 1328565827.1,
1328566719.9, 1328565527.55, 1328565118.05, 1328565556.85, 1328565623.85,
1328565230.75, 1328566083.85, 1328566012.45, 1328566795.75, 1328565262.85,
1328566191.35, 1328565827.8, 1328566384.25, 1328565376.95, 1328566006.8,
1328566570.05, 1328565570, 1328565916.6, 1328566915.35, 1328565693.05,
1328566849.5, 1328565591.55, 1328565161.55, 1328566854.75, 1328566697.1,
1328566807.7, 1328565764.6, 1328565545.3, 1328566294.5, 1328566900.1,
1328566583.1, 1328566114.65, 1328565407.35, 1328566168.05, 1328565560.55,
1328565310.9, 1328565523.45, 1328566256.55, 1328566751.15, 1328565915.5,
1328565945.3, 1328565814.35, 1328566076.3, 1328565793.55, 1328565680.55,
1328566642.8, 1328565417.55, 1328566806.45, 1328566174.2, 1328566692.85,
1328565409.2, 1328566878.25, 1328565824, 1328566747.95, 1328566569.2,
1328566781.5, 1328566408.35, 1328566267.05, 1328566031.15, 1328566758.35,
1328565973.6, 1328566718, 1328566176.5, 1328565351.5, 1328565443.4,
1328565119.65, 1328565134.9, 1328566771, 1328565589, 1328566621.05,
1328565096.2, 1328566427.05, 1328566122.15, 1328565815.65, 1328566352.8,
1328566432, 1328566943.5, 1328566807.25, 1328565701.15, 1328566496.4,
1328565461.7, 1328565331.65, 1328565853.85, 1328565757.1, 1328566760.75,
1328565823.85, 1328566728.2, 1328566586.75, 1328565387.65, 1328565734.6,
1328565377.95, 1328566264.2, 1328565783.75, 1328566498.65, 1328565179.3,
1328565481.8, 1328565562.9, 1328565256.9, 1328566797.9, 1328566244.25,
1328565600.75, 1328566407.6, 1328566061.5, 1328566122.35, 1328565930.8,
1328565306.55, 1328565323.55, 1328565401.9, 1328565454.3, 1328566016.65,
1328566627.2, 1328566683.45, 1328566251.65, 1328565110.8, 1328565600.95,
1328565067.2, 1328565476.55, 1328565987.35, 1328566491.85, 1328566170.15,
1328565781.55, 1328566931, 1328565904.65, 1328566650.35, 1328565598.9,
1328566353.75, 1328566522.4, 1328565898.85, 1328565676.85, 1328566563.1,
1328565671.05, 1328565107.45, 1328565872.6, 1328565417.45, 1328565575.9,
1328565685.65, 1328566090.9, 1328566208.75, 1328566393.45, 1328565468.25,
1328565761.35, 1328566691.3, 1328565834.4, 1328565677.9, 1328566502.6,
1328566861.75, 1328566189.3, 1328565953.25, 1328566378.75, 1328565684.75,
1328565505.55, 1328565862.55, 1328565475.15, 1328566436.65, 1328566075.35,
1328566405, 1328565732.4, 1328566813.7, 1328566316.9, 1328565333.6,
1328565845.55, 1328565067.15, 1328565793.5, 1328566779.55, 1328565920.8,
1328565224.15, 1328566109.55, 1328565718.1, 1328565167.05, 1328565569.4,
1328565712, 1328565457.45, 1328565787.15, 1328566688.75, 1328565955.35,
1328565071.65, 1328566758.6, 1328566224.65, 1328566241.8, 1328565577.15,
1328565915.65, 1328566746.65, 1328566529.25, 1328565608, 1328566423.5,
1328566679.75, 1328566209.25, 1328565187.05, 1328565999.1, 1328565900.05,
1328565788.3, 1328565416.3, 1328565825.5, 1328566934.8, 1328565293.9,
1328566320.45, 1328566359.25, 1328566768.2, 1328565884, 1328566709.8,
1328565500.15, 1328565407.25, 1328565310.15, 1328566339.25, 1328565325.2,
1328565657.65, 1328566649.95, 1328565668.4, 1328566041.05, 1328565353.85,
132856.8, 1328565160.45, 1328566730.2, 1328565463.6, 1328565367.3,
1328565871.65, 1328566409.3, 1328565188.95, 1328565697.3, 1328566938.3,
1328566896.85, 1328565814.15, 1328565786.65, 1328565651.4, 1328566027.7,
1328565577.55, 1328565833.1, 1328565565.05, 1328566055.5, 1328565797.3,
1328566736.4, 1328565117.1, 1328565278.05, 1328565453.3, 1328566725.8,
1328566609.7, 1328565200.7, 1328566554.8, 1328565170.2, 1328565926.05,
1328566840.8, 1328565313.55, 1328566225.35, 1328565366.1, 1328565183.25,
1328566261.8, 1328565673.55, 1328566884.8, 1328566264.65, 1328565216.35,
1328566879.4, 1328566720, 1328565767.1, 1328566113.35, 1328565657.05,
1328566331.9, 1328565554.05, 1328565239.25, 1328565124.6, 1328565789.65,
1328566444.9, 1328565532.2, 1328566719.35, 1328565205.45, 1328566617.45,
1328565708.85, 1328566155.35, 1328565977.55, 1328566142, 1328566342.25,
1328565232.15, 1328566016.7, 1328565895.85, 1328566345.35, 1328566500.7,
1328566403.5, 1328566895.45, 1328565636.5, 1328565809.75, 1328565537.05,
1328566369.45, 1328566340.4, 1328565180, 1328565641.05, 1328565204,
1328566082.05, 1328566183.9, 1328566527.1, 132857.85, 1328566297.2,
1328566094.65, 1328565577.45, 1328566841.05, 1328566422.05, 1328565968.4,
1328566527.5, 1328565450.85, 1328565194.3, 1328565859.05, 1328565790.7,
1328566536.05, 132856.05, 1328566847.6, 1328566431.85, 1328565081.7,
1328565942.8, 1328566154.7, 1328565312.55, 1328565370.95, 1328565222.9,
1328566301.45, 1328565861.2, 1328565974.6, 1328566650.85, 1328566048.4,
1328565780.1, 1328566022.3, 1328566136, 1328566030.75, 1328566877.05,
1328566552.6, 1328565647.2, 1328565181.65, 1328565565.3, 1328566545.1,
1328565860.75, 1328566520.7, 1328565431.65, 1328566769.5, 1328565816.6,
1328565257.75, 1328566903.45, 1328565080.15, 1328565737.4, 1328566865.0

Re: [R] hexplom question(s)

2012-02-14 Thread Greg Snow
Assuming this is the hexplom function from the hexbin package (it is
best to be specific in case there are multiple versions of the
function you ask about), you can specify "lower.panel=function(...){}"
for a, and "as.matrix=TRUE" for c, for b I am not sure what exactly
you want to do, but look at the diag.panel.splom function in the
lattice package as a possible solution.

On Mon, Feb 13, 2012 at 5:16 PM, Debs Majumdar  wrote:
> Hi,
>
>    I am trying to use the --hexplom-- function to draw a scatterplot matrix.
>
> The following works for me: hexplom(~file[,1:4], xbins=15,  xlab="")
>
> However, I want to make some changes to the graph:
>
> a) I only want to print/draw only one-half of the plot. Is there anyway to 
> get rid of the plots in the lower triangular matrix?
>
> b) Is there anyway, I can overwrite the xlabels?
>
> c) Not very important, but the variables start from the bottom and goes up. 
> E.g. I am plotting 4 variables ans I have a 4x4 matrix for the plot. Is there 
> anyway I can reverse the diagonals? i.e I would like to list the variables 
> and axes on 1x1, 2x2, 3x3 and 4x4 rather than the default where it lists the 
> first variable on 4x1 follwed by 3x2, 2x3 and 1x4?
>
>
> Thanks,
>
> -Joey
>
>
> __
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538...@gmail.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] txtStart creates a NULL file

2012-02-14 Thread Simon Kiss
Hello all: 
I'm trying to use the following code to get commands, comments and results to a 
.txt file.  It only appears to capture comments. When I comment those out with 
#, it creates a NULL file.  
Someone seemed to have a similar problem with a mac GUI 
(https://stat.ethz.ch/pipermail/r-help/2010-September/253177.html) but the 
result seemed to be ambiguous. Is there a work-around? Reproducible code and 
sessioninfo are below.   The OS is Mac OS 10.6.8.

Yours truly, Simon Kiss

install.packages("HSAUR")
library(HSAUR)
library(TeachingDemos)
data("Forbes2000", package="HSAUR")
#This is a test of R output for the blind
txtStart('test.txt', commands=TRUE, results=TRUE)
txtComment('This command provides the mean profit in the data set')
mean(Forbes2000$profits, na.rm=TRUE)
txtComment('This command provides the standard deviation of the profits data 
set')
sd(Forbes2000$profits, na.rm=TRUE)
txtComment('This command provides the average profit by country')
aggregate(Forbes2000$profits, by=list(Forbes2000$country), function(x) mean(x, 
na.rm=TRUE))
txtStop()


SessionInfo()

R version 2.13.2 (2011-09-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/C/C/en_CA.UTF-8/en_CA.UTF-8

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

other attached packages:
[1] TeachingDemos_2.7

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

__
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] best subset selection on random effects model

2012-02-14 Thread ilai
I don't know of any package that will do it (or if violating the
marginality principle by having non-nested models even makes sense)
but you could always build your own search through all possible
models. Problem is for a large number of fixed effects this can take a
good bit of time...
Here is a piece of code I had from a few of years back. There may be
more elegant solutions that will reduce run time such as using
update.lme. Basically my code finds all possible combinations of model
terms with ?combn and than fits them.

require(nlme)
fm <- lme(distance ~age*Sex, random = ~ 1|Subject,data=Orthodont,method='ML')
Terms <- terms(fm)
todrop <-  1:length(attr(Terms,'term.labels'))
subs <- unlist(sapply(todrop,function(p)
combn(todrop,p,simplify=F)),recursive =F)
fm.subList <- lapply(subs[-length(subs)],function(s,...){
  newf<- formula(drop.terms(terms(fm),s,keep.response = TRUE))
  update(fm,newf)
})
names(fm.subList) <- sapply(fm.subList, function(x) paste('fm',attr(
terms(x),'term.labels'),sep='.'))
sort(sapply(fm.subList,BIC))   # 1st is best based on BIC

Cheers


On Tue, Feb 14, 2012 at 11:25 AM, Tao Zhang  wrote:
> The models are not nested. I would like to consider all the possible
> subsets.
> I hope to output a table, where each row of the table indicates a best
> subset of the fixed effects for a particular model size.
>
> Thank you,
> Tao
>
>
> 2012/2/13 ilai 
>>
>> The question is where do your models come from? Passing nested models
>> to ?anova.lme in nlme package or lme4 results in a likelihood ratio
>> test. Are you looking for something else/more ?
>>
>>
>> On Sun, Feb 12, 2012 at 8:02 PM, Tao Zhang  wrote:
>> > Hi,
>> >     I know leaps() computes the best subset selection for linear model,
>> > and the bestglm() computes the best subset selection for generalized
>> > linear
>> > model.
>> > Is there any package for best subset selection on random effects model,
>> > or
>> > mixed effects model?
>> >
>> > Thank you so much.
>> >
>> > Tao
>> >
>> >        [[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.


[R] Strange plotting error

2012-02-14 Thread Diviya Smith
Hi there,

I am trying to compute the autocorrelation in a dataset using R's acf
function. ACF automatically plots the results. This works well except in
some cases xlim doesnt work

data <- rnorm(2000,0,1)
acf(data,xlim=c(1,10))  # works - the plot starts at 1
acf(data,lag=100,xlim=c(1,100))   # this does not work and the plot still
starts at 0

Is there another way to specify the xlim or the starting value for x? Any
help would be most appreciated.

Thanks,
Diviya

[[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] execute array of functions

2012-02-14 Thread Bert Gunter
Not sure the replies you received make it clear, but the key point is
that functions are first class objects in R like any other objects
(vectors, lists, data frames) and can be assigned, collected into
structures, indexed, etc. in the usual way. Further, if the value of
any R expression is a function object, it can be called in the usual
way on an argument list by:

Some R_Expression(comma separated argument list)

e.g.
> {y <-function(x) sin(x)+10} (pi/2) ## pi is a known named constant in R
11

Your initial confusion below actually has nothing to do with this: you
have confused unquoted names of objects in R with quoted character
strings. So, e.g.

x <- 1:10
assigns a sequence of numeric values to the name x, which essentially
points to where those values are stored in memory.

typing
>x
at the command line would print the values (by default)

typing
>"x"
prints the character string "x"

-- Bert

On Tue, Feb 14, 2012 at 11:02 AM, Muhammad Rahiz
 wrote:
> Hi all,
>
> I'm trying to get the min and max of a sequence of number using a loop like
> the folllowing. Can anyone point me to why it doesn't work.
>
> Thanks.
>
> type    <- c("min","max")
> n       <- 1:10
> for (a in 1:2)    {
> print(type[a](n)) }
>
>
> --
> Muhammad
>
> __
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] execute array of functions

2012-02-14 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Nordlund, Dan (DSHS/RDA)
> Sent: Tuesday, February 14, 2012 11:24 AM
> To: r-help@r-project.org
> Subject: Re: [R] execute array of functions
> 
> > -Original Message-
> > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Muhammad Rahiz
> > Sent: Tuesday, February 14, 2012 11:03 AM
> > To: x.r-help
> > Subject: [R] execute array of functions
> >
> > Hi all,
> >
> > I'm trying to get the min and max of a sequence of number using a
> loop
> > like the folllowing. Can anyone point me to why it doesn't work.
> >
> > Thanks.
> >
> > type<- c("min","max")
> > n   <- 1:10
> > for (a in 1:2){
> > print(type[a](n)) }
> >
> >
> > --
> > Muhammad
> >
> 
> I am not sure why you are trying to use this approach in a loop, but
> you need to use the get() function if you want to do this.
> 
> type <- c("min","max")
> n <- 1:10
> for (a in 1:2)  {
>   print(get(type[a])(n))
> }
> 
> But, why not use min and max on n directly?
> 
> 

I probably should have pointed out that the reason your code doesn't work is 
that type is a vector of character strings with function names, not the actual 
functions.  Another approach would be to store the actual functions, rather 
than the names in type.  So something like 

type <- c(min,max)
n <- 1:10
for (a in 1:2){
  print(type[[a]](n)) 
}


But maybe someone more expert can tell you the 'best' or more R-ish way of 
accomplishing what you want. 

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


__
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] execute array of functions

2012-02-14 Thread Duncan Murdoch

On 14/02/2012 2:23 PM, Nordlund, Dan (DSHS/RDA) wrote:

>  -Original Message-
>  From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
>  project.org] On Behalf Of Muhammad Rahiz
>  Sent: Tuesday, February 14, 2012 11:03 AM
>  To: x.r-help
>  Subject: [R] execute array of functions
>
>  Hi all,
>
>  I'm trying to get the min and max of a sequence of number using a loop
>  like the folllowing. Can anyone point me to why it doesn't work.
>
>  Thanks.
>
>  type  <- c("min","max")
>  n <- 1:10
>  for (a in 1:2)  {
>  print(type[a](n)) }
>
>
>  --
>  Muhammad
>

I am not sure why you are trying to use this approach in a loop, but you need 
to use the get() function if you want to do this.

type<- c("min","max")
n<- 1:10
for (a in 1:2){
   print(get(type[a])(n))
}

But, why not use min and max on n directly?


Or put min and max into the type vector, i.e.

type <- list(min, max)
n <- 1:10
for (a in 1:2) print(type[[a]](n))

Note that I need the double brackets [[a]], because type is a list.  
(You can use type <- c(min, max) with the same result, but

I find using list() explicitly is easier to understand.)

Duncan Murdoch

__
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] execute array of functions

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 2:32 PM, Stavros Macrakis wrote:

That won't work because R has special rules for evaluating things in  
the

function position.  Examples:

*OK*

min(1:2)
"min"(1:2)
f<-min; f(1:2)
do.call(min,list(1:2))
do.call("min",list(1:2))  # do.call converts string->function


*Not OK*

("min")(1:2)  # string in function position is not  
converted

f<-"min"; f(1:2)   # ditto
f<- c("min","max");  f[1](1:2)  # ditto


What you need to do is make 'f' a list of *function values, *not a  
vector

of strings:

f<- c(min,max)


and then select the element of f with [[ ]] (select one element),  
not [ ]

(select sublist):

f[[1]](1:2)


Thus your example becomes

type<- c(min,max)
n   <- 1:10
for (a in 1:2){
print(type[[a]](n)) }

Another (uglier) approach is with do.call:

type<- c("min","max")
n   <- 1:10
for (a in 1:2){
print(do.call(type[a],list(n))) }


This is somewhat less ugly:

 funs <- c("min","max")
 for (a in funs)  {
   print(do.call(a, list(n))) }

[1] 1
[1] 10




Does that help?

-s

On Tue, Feb 14, 2012 at 14:02, Muhammad Rahiz
wrote:


Hi all,

I'm trying to get the min and max of a sequence of number using a  
loop

like the folllowing. Can anyone point me to why it doesn't work.

Thanks.

type<- c("min","max")
n   <- 1:10
for (a in 1:2){
print(type[a](n)) }


--
Muhammad

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


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.


[R] Creating categories from a date-time object

2012-02-14 Thread Jose Bustos Melo
Hello R-List,

I have a question about recoding from a date time object.  I have tried using 
as.POSIXct objects and Chron Objects, but I can get the what I want.

I need to create a new variable from a date-time object, adding "Office Time" 
for those events that happens between 08:00:00 to 18:00:00 and "Out of Office" 
all the others, but not including weekends. I have created a fake data.

tmp <- seq(as.POSIXct('2011-08-01 13:00'), as.POSIXct('2011-09-02 03:00'), 
by='45 min')

Is there any valid way to do it? I have spend so much time without any good 
luck and I don't have any good code to show!
Thanks in advance!
José

[[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] execute array of functions

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 2:23 PM, Nordlund, Dan (DSHS/RDA) wrote:


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
project.org] On Behalf Of Muhammad Rahiz
Sent: Tuesday, February 14, 2012 11:03 AM
To: x.r-help
Subject: [R] execute array of functions

Hi all,

I'm trying to get the min and max of a sequence of number using a  
loop

like the folllowing. Can anyone point me to why it doesn't work.

Thanks.

type<- c("min","max")
n   <- 1:10
for (a in 1:2){
print(type[a](n)) }


> n=1:10
> for (a in c(min,max)){
+ print(a(n)) }
[1] 1
[1] 10

> funs <- c(min,max)
> for (a in funs)  {
+ print(a(n)) }
[1] 1
[1] 10





--
Muhammad



I am not sure why you are trying to use this approach in a loop, but  
you need to use the get() function if you want to do this.


type <- c("min","max")
n <- 1:10
for (a in 1:2){
 print(get(type[a])(n))
}

But, why not use min and max on n directly?


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


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


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] execute array of functions

2012-02-14 Thread Stavros Macrakis
That won't work because R has special rules for evaluating things in the
function position.  Examples:

*OK*

min(1:2)
"min"(1:2)
f<-min; f(1:2)
do.call(min,list(1:2))
do.call("min",list(1:2))  # do.call converts string->function


*Not OK*

("min")(1:2)  # string in function position is not converted
f<-"min"; f(1:2)   # ditto
f<- c("min","max");  f[1](1:2)  # ditto


What you need to do is make 'f' a list of *function values, *not a vector
of strings:

f<- c(min,max)


and then select the element of f with [[ ]] (select one element), not [ ]
(select sublist):

f[[1]](1:2)


Thus your example becomes

type<- c(min,max)
n   <- 1:10
for (a in 1:2){
print(type[[a]](n)) }

Another (uglier) approach is with do.call:

type<- c("min","max")
n   <- 1:10
for (a in 1:2){
print(do.call(type[a],list(n))) }


Does that help?

 -s

On Tue, Feb 14, 2012 at 14:02, Muhammad Rahiz
wrote:

> Hi all,
>
> I'm trying to get the min and max of a sequence of number using a loop
> like the folllowing. Can anyone point me to why it doesn't work.
>
> Thanks.
>
> type<- c("min","max")
> n   <- 1:10
> for (a in 1:2){
> print(type[a](n)) }
>
>
> --
> Muhammad
>
> __**
> 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] sequential sum

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 1:38 PM, Nordlund, Dan (DSHS/RDA) wrote:


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
project.org] On Behalf Of baccts
Sent: Tuesday, February 14, 2012 9:04 AM
To: r-help@r-project.org
Subject: [R] sequential sum

Dear R users,

I am trying to sum number that exist in another vector up to i, then
increment i and repeat.
Sorry. It's hard to explain but basically I am trying to do the
following:
test <- c(1,2,3,4);
test2 <- c(3,5,6,7,2,8,8,4,4);
test3 <- c(10,20,30,40);
tmp <- 0;
for (i in 1:length(test)){
tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
}

so when i = 1, tmp[i] = 0 because test[1]=1 is not in test2 and
when i = 3, tmp[i] = 50 = test3[2] + test3[3] because test[2:3] is in
test2

Problem is test has 5000+ entries. How do I do the same thing without
the
loop?

Thanks in advance.



Your example data is not very extensive so I don't know if this  
solution is general enough.  But, it does work with your data.


I had exactly that concern so tested my solution, which was a bit  
different than yours:


test <- c(1,2,3,4,10, 5);
test2 <- c(3,5,6,7,2,8,8,4,4);
test3 <- c(10,20,30,40, 50, 60);
tmp <- 0;
for (i in 1:length(test)){
tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
}

> tmp
[1]   0  20  50  90  90 150
> tmp2 <- ifelse(test %in% test2, cumsum(test3[test %in% test2]), 0)
> tmp2
[1]   0  50  90 150   0  50  # was going to throw away.


> tmp3 <- cumsum(ifelse(test %in% test2, test3, 0))
> tmp3
[1]   0  20  50  90  90 150

So yours is better at spanning the non-match entries in the same  
manner as the for-loop.






Hope this is helpful,

Dan

Daniel J. Nordlund



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] execute array of functions

2012-02-14 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Muhammad Rahiz
> Sent: Tuesday, February 14, 2012 11:03 AM
> To: x.r-help
> Subject: [R] execute array of functions
> 
> Hi all,
> 
> I'm trying to get the min and max of a sequence of number using a loop
> like the folllowing. Can anyone point me to why it doesn't work.
> 
> Thanks.
> 
> type  <- c("min","max")
> n <- 1:10
> for (a in 1:2)  {
> print(type[a](n)) }
> 
> 
> --
> Muhammad
> 

I am not sure why you are trying to use this approach in a loop, but you need 
to use the get() function if you want to do this.

type <- c("min","max")
n <- 1:10
for (a in 1:2){
  print(get(type[a])(n)) 
}

But, why not use min and max on n directly?


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


__
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] Calculate mean between to classes for each ID

2012-02-14 Thread Patrick Connolly
On Tue, 14-Feb-2012 at 04:30AM -0800, Sana wrote:

|> I have a data set that looks like this:
|> ID   STRATA  G_F_Bl  Dkl N   g_pr_dklN_haG_ha
|> 20,103   2   1   5   5   0,002   250 0,5
|> 20,103   2   1   7   3   0,0038  150 0,57
|> 20,103   2   1   9   6   0,0064  300 1,92
|> 20,103   2   1   11  1   0,0095  50  0,475
|> 20,103   2   1   13  1   0,0133  50  0,665
|> 20,103   2   1   15  1   0,0177  50  0,885
|> 20,103   2   1   17  3   0,0227  150 3,405
|> 20,103   2   1   21  2   0,0346  100 3,46
|> 20,103   2   1   23  2   0,0415  100 4,15
|> 20,103   2   1   25  2   0,0491  100 4,91
|> 20,103   2   1   27  1   0,0573  50  2,865
|> 20,103   2   1   29  1   0,0661  50  3,305
|> 20,103   2   1   37  1   0,1075  50  5,375
|> 20,7 2   1   5   10  0,002   500 1
|> 20,7 2   1   7   7   0,0038  350 1,33
|> 20,7 2   3   7   1   0,0038  50  0,19
|> 20,7 2   1   9   7   0,0064  350 2,24
|> 20,7 2   1   11  1   0,0095  50  0,475
|> 20,7 2   1   13  1   0,0133  50  0,665
|> 20,7 2   1   15  3   0,0177  150 2,655
|> 20,7 2   1   19  2   0,0284  100 2,84
|> 20,7 2   1   21  3   0,0346  150 5,19
|> 20,7 2   1   23  2   0,0415  100 4,15
|> 
|> I'd like to calculate the mean  for g_pr_dkl   between Dkl for each ID.
|> Can anybode please help me?

?aggregate


|> 
|> Thanks,
|> Sana
|> 
|> --
|> View this message in context: 
http://r.789695.n4.nabble.com/Calculate-mean-between-to-classes-for-each-ID-tp4386842p4386842.html
|> Sent from the R help mailing list archive at Nabble.com.
|>  [[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.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___Patrick Connolly   
 {~._.~}   Great minds discuss ideas
 _( Y )_ Average minds discuss events 
(:_~*~_:)  Small minds discuss people  
 (_)-(_)  . Eleanor Roosevelt
  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.

__
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] execute array of functions

2012-02-14 Thread Muhammad Rahiz

Hi all,

I'm trying to get the min and max of a sequence of number using a loop 
like the folllowing. Can anyone point me to why it doesn't work.


Thanks.

type<- c("min","max")
n   <- 1:10
for (a in 1:2){
print(type[a](n)) }


--
Muhammad

__
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] svm with GRASS GIS

2012-02-14 Thread gab
Dear R Community-

I am a new user of R.  I am using R with GRASS GIS.
I would apply svm "on" raster data in GRASS.
Basically I have a raster with "areas training" and other three raster (each
represents a band of ASTER satellite image).
My goal is to classify, according to training areas, the 3 raster.
Trying to replicate the guides found on the net, I did the following:
# load raster
Training<-readRAST6("Training")
AST_L1B_1<-readRAST6("AST_L1B_1")
AST_L1B_2<-readRAST6("AST_L1B_2")
AST_L1B_3N<-readRAST6("AST_L1B_3N")
#and then
 model_ASTER <-
svm(Training_2006,AST_L1B_1,AST_L1B_2,AST_L1B_3N,type='C',kernel='linear')
#but
Errore in data.frame(y, x) : 
  arguments imply differing number of rows: 1857076, 1488

Thanks for any help

 


--
View this message in context: 
http://r.789695.n4.nabble.com/svm-with-GRASS-GIS-tp4388006p4388006.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] sequential sum

2012-02-14 Thread Nordlund, Dan (DSHS/RDA)
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of baccts
> Sent: Tuesday, February 14, 2012 9:04 AM
> To: r-help@r-project.org
> Subject: [R] sequential sum
> 
> Dear R users,
> 
> I am trying to sum number that exist in another vector up to i, then
> increment i and repeat.
> Sorry. It's hard to explain but basically I am trying to do the
> following:
> test <- c(1,2,3,4);
> test2 <- c(3,5,6,7,2,8,8,4,4);
> test3 <- c(10,20,30,40);
> tmp <- 0;
> for (i in 1:length(test)){
> tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
> }
> 
> so when i = 1, tmp[i] = 0 because test[1]=1 is not in test2 and
> when i = 3, tmp[i] = 50 = test3[2] + test3[3] because test[2:3] is in
> test2
> 
> Problem is test has 5000+ entries. How do I do the same thing without
> the
> loop?
> 
> Thanks in advance.
> 

Your example data is not very extensive so I don't know if this solution is 
general enough.  But, it does work with your data.

tmp <- cumsum(ifelse(test %in% test2, test3, 0))


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204


__
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] finding the subscript of a vector fulfiiling a given condition

2012-02-14 Thread Pete Brecknock

galemago wrote
> 
> Dear Forum,
> 
> I just recently started to work with R, I´d like to know if there is a way
> to give instructions/do operations related to values with different
> subscripts within a vector.
> Let´s assume I have a vector like this:
> A="368369370371   393394395"
> If I call "j" the subscripts of the vector I´d like to find the j value
> such that A(j+1)-A(j) is the maximum.
> This would tell me at which position of the vector I have the biggest gap.
> (the fifth in the example above)
> My problem is that I don´t find a way to define the vector subscripts as a
> variable, that I believe is what I need to solve this problem. Is that
> possible? 
> Thanks a lot, hope to come back often in this forum,
> G.
> 

How about ...

A <- c(368,369,370,371,393,394,395)

which.max(diff(A))

HTH

Pete


--
View this message in context: 
http://r.789695.n4.nabble.com/finding-the-subscript-of-a-vector-fulfiiling-a-given-condition-tp4387870p4388042.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] weights in glm() of logistic regression when response is proportion

2012-02-14 Thread Yinyan Guo
Dear R-list members, 
I would like to pose a question about the use of the glm() function for 
logistic regression when response is proportion range 0.0-1.0.
When logistic regression in glm (y proportion response) is used as: 
 disease.logr <- glm(y ~ x, weights = ntrial, family=binomial, ...)
My qestion1: Do weights  must be provided or can be omitted when response 
y is proportion?
question2: If weights can be omitted when y is proportion response, what 
methods are used in R (please point me to the reference of the method)?
Thank you very much in advance for your help.  I am looking forward to 
hearing from you.
Sincerely
Yinyan Guo
[[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] How to suppress the empty plots in xyplot (lattice)

2012-02-14 Thread ilai
> read ?xyplot
> It takes a skip argument:
>          ‘skip’: logical vector (default ‘FALSE’), replicated to be as
>              long as the number of panels (spanning all pages).  For
>              elements that are ‘TRUE’, the corresponding panel
>              position is skipped; i.e., nothing is plotted in that
>              position.  The panel that was supposed to be drawn there
>              is now drawn in the next available panel position, and
>              the positions of all the subsequent panels are bumped up
>              accordingly.  This is often useful for arranging plots in
>              an informative manner.
>
> Or a 'drop.unused.levels' argument, or a 'subset'.
>
> Any one of these would work .
>
> Cheers
>
> On Tue, Feb 14, 2012 at 10:50 AM, Jun Shen  wrote:
>> Thanks, Jeff,
>>
>> It did work in one way if I use
>>
>> xyplot(Y~X|as.factor(ID*PERIOD),data=...)
>>
>> But I would like to do something like
>>
>> xyplot(Y~X|as.factor(paste("ID=",ID)*paste("PERIOD=",PERIOD)),data=...)
>>
>> Then, it didn't work
>>
>> The error message:
>> Error in paste("ID=", ID) * paste("PERIOD=", PERIOD) :
>>  non-numeric argument to binary operator
>>
>> On Tue, Feb 14, 2012 at 11:08 AM, Jeff Newmiller
>> wrote:
>>
>>> Set up a single (factor) variable that identifies the combinations that
>>> exist, and plot using that variable.
>>> ---
>>> Jeff Newmiller                        The     .       .  Go Live...
>>> DCN:        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.
>>>
>>> Jun Shen  wrote:
>>>
>>> >Dear all,
>>> >
>>> >In a plot command like
>>> >
>>> >xyplot(Y~X|ID*PERIOD,data=...)
>>> >
>>> >xyplot will generate all the possible ID*PERIOD combinations. But not
>>> >all
>>> >of them have data in there. So I have a lot of empty plots. How can I
>>> >suppress those empty plots and ask xyplot only to generate plots
>>> >actually
>>> >with data. Thanks.
>>> >
>>> >Jun
>>> >
>>> >       [[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.

__
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] How to suppress the empty plots in xyplot (lattice)

2012-02-14 Thread Bert Gunter
What do you think

"A" * "B"

means?  Why? Have you read R's Introduction manual?

-- Bert

On Tue, Feb 14, 2012 at 9:50 AM, Jun Shen  wrote:
> Thanks, Jeff,
>
> It did work in one way if I use
>
> xyplot(Y~X|as.factor(ID*PERIOD),data=...)
>
> But I would like to do something like
>
> xyplot(Y~X|as.factor(paste("ID=",ID)*paste("PERIOD=",PERIOD)),data=...)
>
> Then, it didn't work
>
> The error message:
> Error in paste("ID=", ID) * paste("PERIOD=", PERIOD) :
>  non-numeric argument to binary operator
>
> On Tue, Feb 14, 2012 at 11:08 AM, Jeff Newmiller
> wrote:
>
>> Set up a single (factor) variable that identifies the combinations that
>> exist, and plot using that variable.
>> ---
>> Jeff Newmiller                        The     .       .  Go Live...
>> DCN:        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.
>>
>> Jun Shen  wrote:
>>
>> >Dear all,
>> >
>> >In a plot command like
>> >
>> >xyplot(Y~X|ID*PERIOD,data=...)
>> >
>> >xyplot will generate all the possible ID*PERIOD combinations. But not
>> >all
>> >of them have data in there. So I have a lot of empty plots. How can I
>> >suppress those empty plots and ask xyplot only to generate plots
>> >actually
>> >with data. Thanks.
>> >
>> >Jun
>> >
>> >       [[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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] save output of loop

2012-02-14 Thread uday
The dimensions of variables are unknown , they changes to every file. 

--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387804.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] save output of loop

2012-02-14 Thread uday
Hi it works fine 
thanks 

--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387806.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Plotting function image

2012-02-14 Thread uday
I have some data set which has latitude, longitude and Z values. 
I would like to plot them on global map. 
I am thinking to use image 
image(x, y, z, zlim, xlim, ylim, col = heat.colors(12),
  add = FALSE, xaxs = "i", yaxs = "i", xlab, ylab,
  breaks, oldstyle = FALSE, useRaster = FALSE, ...)

here I have x = longitude
   y= latitude 
  z = z 

but z suppose to be matrix but my z values are  single array , could
somebody please tell me how to convert these single array of z to matrix ? 



--
View this message in context: 
http://r.789695.n4.nabble.com/Plotting-function-image-tp4387796p4387796.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] sequential sum

2012-02-14 Thread baccts
Dear R users,

I am trying to sum number that exist in another vector up to i, then
increment i and repeat.
Sorry. It's hard to explain but basically I am trying to do the following:
test <- c(1,2,3,4);
test2 <- c(3,5,6,7,2,8,8,4,4);
test3 <- c(10,20,30,40);
tmp <- 0;
for (i in 1:length(test)){
tmp[i] <- sum(test3[which(test[1:i] %in% test2)]);
}

so when i = 1, tmp[i] = 0 because test[1]=1 is not in test2 and
when i = 3, tmp[i] = 50 = test3[2] + test3[3] because test[2:3] is in test2

Problem is test has 5000+ entries. How do I do the same thing without the
loop?

Thanks in advance. 

--
View this message in context: 
http://r.789695.n4.nabble.com/sequential-sum-tp4387670p4387670.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How to suppress the empty plots in xyplot (lattice)

2012-02-14 Thread Jun Shen
Thanks, Jeff,

It did work in one way if I use

xyplot(Y~X|as.factor(ID*PERIOD),data=...)

But I would like to do something like

xyplot(Y~X|as.factor(paste("ID=",ID)*paste("PERIOD=",PERIOD)),data=...)

Then, it didn't work

The error message:
Error in paste("ID=", ID) * paste("PERIOD=", PERIOD) :
  non-numeric argument to binary operator

On Tue, Feb 14, 2012 at 11:08 AM, Jeff Newmiller
wrote:

> Set up a single (factor) variable that identifies the combinations that
> exist, and plot using that variable.
> ---
> Jeff NewmillerThe .   .  Go Live...
> DCN:Basics: ##.#.   ##.#.  Live
> Go...
>  Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
> /Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
> ---
> Sent from my phone. Please excuse my brevity.
>
> Jun Shen  wrote:
>
> >Dear all,
> >
> >In a plot command like
> >
> >xyplot(Y~X|ID*PERIOD,data=...)
> >
> >xyplot will generate all the possible ID*PERIOD combinations. But not
> >all
> >of them have data in there. So I have a lot of empty plots. How can I
> >suppress those empty plots and ask xyplot only to generate plots
> >actually
> >with data. Thanks.
> >
> >Jun
> >
> >   [[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] different way for a for loop for several columns?

2012-02-14 Thread ilai
Inline

On Tue, Feb 14, 2012 at 3:16 AM, Nerak T  wrote:
> Dear Ilai,
>
>
>
> Thanks for your answer. I'm indeed kind of a beginner in R, starting
> to discover the endless possibilities in R. My goal for the moment is indeed
> to get rid of the use of loops and to see through the apply family (which
> seems to be an endless maze for the moment). (For some reason, the apply
> function doesn’t seems to be logical for my brains which prefer to think in
> a loop way)
>

?apply ?lapply ?tapply, etc. are just wrappers for building more
efficient loops. If you "think in loops" (which you shouldn't) you are
also thinking in "apply". The reason it may seem like an endless maze
is because you use different wrappers for looping over different
object classes and indices, but at the end, the call to apply() is
similar to calling for().
e.g. consider a matrix with dimensions n x p. To sum rows you could
for(i in 1:n) sum(matrix[i,])
But better apply(matrix, 1 , sum)  # where the 1 denotes the 1st
dimension (rows)
Same thing for  sum columns
for(j in 1:p) sum(matrix[,j])
But better apply(matrix, 2 , sum)  # where the 2 denotes the 2nd
dimension (columns)

The same "stuff" that goes in the loop can go to apply with small
syntax changes. e.g.

ll<- list(1:5,1:10,letters)
out<- list()
for(L in 1:3){
# ...
# ... a bunch of complicated functions/calculations
# ...
out[[ L ]] <- length( ll[[ L ]] )
}
out

Can be replaced with

lapply( ll, function(L) {
# ...
# ... a bunch of complicated function/calculations
# ...
length(L)
} )

This time use lapply since you are looping over L elements of the list ll.
>
>
> Your answer is really helpful.
> Something I found really interesting is your general comment. You say that I
> don’t need to declare variables? The reason I started to do this is because
> if I don’t, I get a message that the object is not found.

???

xx<- c(0,10,100)   # declare xx
print(xx)
rnorm(3,xx)  # use it
rm(xx) # remove xx
rnorm(3, xx<- c(0,10,100))   define and use it "at the same time"
print(xx)

> If I create a data
> frame before the calculation with the right amount of rows, it seems to
> work. But if there is a way not to have to make them before, would be great?

Only in loops. What happens is you need to create a "storage" for the
result of your loop, since the objects created in the loop are
overwritten at each step:
for(i in 1:10) cat( i, '\n' )
i # i =  10 everything before was overwritten

> Because most of the time, I don’t need that column that I created (but
> couldn't create an empty data frame with right dimensions to solve the
> problem)…

See my lapply example for creating "empty" storage.

>
>> Last, in R you want to avoid loops as much as possible especially for
>> large data sets. Operations are performed on objects so 1:4 + 1 is
>> equivalent to for(i in 1:4) i+1

> This part I don’t understand… where do you put that “ 1:4 + 1 ” ?
>

You don't put it anywhere, it was in answer to your comment: " but
it’s not that I have created a function that has to be applied on a
whole column, calculations are done for the different rows…"

So, no! calculations are done on the object (which has some dimension
or is a list), only in rare cases do you need to loop over each
element (or dimension) of the object itself.

>
> Many thanks, I’m trying to learn as much as possible to be able to use R
> more efficient so I really appreciate your help.

Pleasure. Good luck !

>
>
>
> Kind regards,
>
> Nerak
>
>
>
>
>
>
>
>
>
>> Date: Mon, 13 Feb 2012 23:43:29 -0700
>> Subject: Re: [R] different way for a for loop for several columns?
>> From: ke...@math.montana.edu
>> To: nera...@hotmail.com
>
>>
>> Nerak,
>> Your example could have been done without a loop at all (at least this
>> calculation), or as you already know by calling one of the apply
>> family functions which are more efficient (but are still "loops"):
>>
>> test<- data.frame(
>>
>> Date=c(1980,1980,1980,1980,1981,1981,1981,1981,1982,1982,1982,1982,1983,1983,1983,1983),
>> C = c(0,0,0,0,5,2,0,0,0,15,12,10,6,0,0,0),
>> B = c(0,0,0,0,9,6,2,0,0,24,20,16,2,0,0,0),
>> F = c(0,0,0,0,6,5,1,0,0,18,16,12,10,5,1,0)
>> )
>> test.2 <- test[,-1] > 1
>> aggregate(test.2, list(test$Date), sum)
>>
>> # See ?aggregate for more details. it also has a time series method
>> which may be useful for you.
>>
>> A general comment. if you are or will be using R a bit more, it may
>> benefit you to study the manuals or find a good basic tutorial. You
>> seem to be applying the conventions of some other programming language
>> and that's slowing you down. e.g. you don't need to declare variables,
>> so all this stuff before your loop is unnecessary:
>> > Year<-data.frame(Date)
>> > test.1<-data.frame(c(1980:1983))
>> > test.4<-data.frame(c(1:4))
>> Also 1:4 is equivalent to data.frame(c(1:4)) without the extra attributes.
>>
>> Last, in R you want to avoid loops as much as possible especially for
>> large data sets. Operations are performed on obje

Re: [R] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread Yihui Xie
I just looked at Jeff's tutorials; they are great! I no longer see
instructions on noweb or Sweave.sty, so I highly recommend them as
well. I will add links in the official manual. Thanks, Jeff!

Regards,
Yihui
--
Yihui Xie 
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Tue, Feb 14, 2012 at 11:28 AM, Jeff Laake  wrote:
> On 2/14/2012 6:51 AM, Rainer M Krug wrote:
>>
>> -BEGIN PGP SIGNED MESSAGE-
>> Hash: SHA1
>>
>> On 14/02/12 13:41, ATANU wrote:
>>>
>>> I am using R 2.14.1. I am trying to configure Lyx with Sweave. I
>>> have read articles but I found none to be complete  and I cannot
>>> import sweave document in lyx . Can anyone please help me with a
>>> stepwise procedure how to configure Lyx with Sweave so that I can
>>> run my R-code chunks from LYX ,the output being a pdf.?
>>
>>
>> I would really recommend to ask this question on the lyx mailing list
>> (http://www.lyx.org/MailingLists)
>>
>> See you there,
>>
>> Rainer
>>
>>
> You can find some instructions that I wrote for installation and a
> mini-tutorial at https://github.com/NMML/R-User-Meetings.  While there are
> many TeX editors around I find LyX to be a big time saver for table creation
> amongst others. Also, it allows you to use LaTeX without remembering all of
> the syntax and still allows you to use LaTeX code as you learn.  LyX
> incorporates a Track Changes feature which is useful because non Tex
> collaborators can use it for edits with no knowledge of TeX.   Rainer is
> correct that this is better on the LyX mailing list, but maybe this will be
> to let R users know what is out there to get them into Sweave/LaTeX.  It has
> certainly been useful for me.
>
> --jeff
>
>
> __
> 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] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread Yihui Xie
The only (yes, only) documentation you should look at now is the
official manual of Sweave in LyX, which you can find under the
examples directory of your LyX installation for LyX 2.0.2; it will
appear under the Help menu after LyX 2.0.3 (not released yet, but will
be soon). If you are still unable to find it, please go to:
https://github.com/downloads/yihui/lyx/sweave.pdf It is a compiled
version of the manual.

All other instructions, IMHO, are both hackish and outdated. We have
been working hard on the Sweave module since LyX 2.0 so that you
basically only need one step to use Sweave in LyX (configure PATH).
BTW, lyx-us...@lists.lyx.org may be a better mailing list to go.

Regards,
Yihui
--
Yihui Xie 
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA



On Tue, Feb 14, 2012 at 9:03 AM, David Winsemius  wrote:
>
> On Feb 14, 2012, at 9:44 AM, Duncan Murdoch wrote:
>
>> On 14/02/2012 7:41 AM, ATANU wrote:
>>>
>>> I am using R 2.14.1. I am trying to configure Lyx with Sweave. I have
>>> read
>>> articles but I found none to be complete  and I cannot import sweave
>>> document in lyx . Can anyone please help me with a stepwise procedure how
>>> to
>>> configure Lyx with Sweave so that I can run my R-code chunks from LYX
>>> ,the
>>> output being a pdf.?
>>
>>
>> I've never used Lyx, but I've written instructions for several different
>> editors (Winedt, TeXWorks, TeXShop) and you might find
>> those helpful if you need to do it yourself.  See
>>
>>
>> http://www.umanitoba.ca/statistics/seminars/2011/3/4/duncan-murdoch-using-sweave-R/
>>
>
> Thank you for that Duncan;
>
> I thought the question was overly broad and not containing needed
> information, but it is having welcome side-effects. Your tutorial on TexShop
> integration with Sweave looks very promising. I'm a LaTeX noob, but on my
> Mac it seems to be the most "welcoming" of the many LaTeX editors available.
>
>
> --
> 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.

__
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] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread Jeff Laake

On 2/14/2012 6:51 AM, Rainer M Krug wrote:

-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 14/02/12 13:41, ATANU wrote:

I am using R 2.14.1. I am trying to configure Lyx with Sweave. I
have read articles but I found none to be complete  and I cannot
import sweave document in lyx . Can anyone please help me with a
stepwise procedure how to configure Lyx with Sweave so that I can
run my R-code chunks from LYX ,the output being a pdf.?


I would really recommend to ask this question on the lyx mailing list
(http://www.lyx.org/MailingLists)

See you there,

Rainer


You can find some instructions that I wrote for installation and a 
mini-tutorial at https://github.com/NMML/R-User-Meetings.  While there 
are many TeX editors around I find LyX to be a big time saver for table 
creation amongst others. Also, it allows you to use LaTeX without 
remembering all of the syntax and still allows you to use LaTeX code as 
you learn.  LyX incorporates a Track Changes feature which is useful 
because non Tex collaborators can use it for edits with no knowledge of 
TeX.   Rainer is correct that this is better on the LyX mailing list, 
but maybe this will be to let R users know what is out there to get them 
into Sweave/LaTeX.  It has certainly been useful for me.


--jeff

__
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] Writing R-scripts

2012-02-14 Thread Jeff Newmiller
A) You should (re)read the posting guidelines, and pay particular attention to 
supplying reproducible examples. (Not your whole script.. just a cut down 
version that others can run that exhibits the problem.)

B) Perhaps you should reconsider the task you have set yourself. You seem to be 
writing an interpreter of sorts... R is already an interpreter. Why not write a 
line or two in R, calling on libraries and/or sourcing a file of your own 
convenience functions as needed instead of encoding your wishes in arcane 
"testtype" values?
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Cem Girit  wrote:

>Hello Jim,
>
>   Thank you. This will really help me to get the result I need.  But I
>am having an error message when I run the code although "testtype" is
>set to
>2 in the calling function to run the Dunnet test. What am I doing
>wrong?
>
>> pt<-parametric.tests(testtype=2, resp, groups, vehicle="Control",
>alpha=0.05)
>Error in clean.args(resp, group, vehicle, alpha, lmat, testtype) : 
>  unused argument(s) (testtype)
>
>library(plotrix)
>
>parametric.tests<-function(testtype, resp, group, vehicle, alpha,
>lmat){
>
>result<-do.call(deparse(substitute(testtype)), 
> clean.args(resp, group, vehicle, alpha, lmat, testtype))
>return(result)
>}
>
>if (testtype==1){
>
>###
>
>## resp:  response variable, must be numeric and vector
>## group: group id for resp, numeric or character
>## alpha: CL 0.05 or 0.01
>
>## Bonferroni
> bonferroni.test <- function(resp, group, alpha) 
>{
>.
>
> result <- data.frame(label=label, estimate=estimate, alpha=alpha,
>p.value=pval, significance=sig)
> return(result)
>}
>} else if (testtype==2){
>
>###
>## resp:  response variable, must be numeric and vector
>## group: group id for resp, numeric or character
>## alpha: CL 0.05 or 0.01
>## vehicle: label for the reference group (for example, vehicle =
>"Control"
>if the lable for the ## reference group is 'Control')
>
>## Dunnett 
>dunnett.test <- function(resp, group, alpha, vehicle)
>{
>
>
> result <- data.frame(label=label, estimate=estimate, alpha=alpha,
>lower=lower, upper=upper, p.value=pval, significance=sig)
> return(result)
>}
>}
>
>Cem
>
>-Original Message-
>From: Jim Lemon [mailto:j...@bitwrit.com.au] 
>Sent: Tuesday, February 14, 2012 3:32 AM
>To: Cem Girit
>Cc: r-help@r-project.org
>Subject: Re: [R] Writing R-scripts
>
>On 02/14/2012 08:59 AM, Cem Girit wrote:
>> Hello,
>>
>>  This is my first attempt to write a script in R. The program below
>is 
>> intended to do some parametric tests on group data. There are 
>> subroutines for each type of test. The call to the
>"parametric.tests", 
>> routine sets the argument "testtype" for the test to be used. How can
>
>> I transfer the calculated values (in "result" below) in each routine 
>> to the calling parametric.tests routine?
>>
>Hi Cem,
>You may find it easier to use the do.call method and the clean.args
>function:
>
>library(plotrix)
>parametric.tests<-function(testfun,arglist) {
>  result<-do.call(deparse(substitute(testfun)),
>   clean.args(arglist,testfun))
>  return(result)
>}
>parametric.tests(mean,list(x=1:5,na.rm=TRUE,foo="?"))
>
>If the function called ("testfun") is available, and the argument
>"arglist"
>contains sufficient arguments for it to run, it will return the value
>of the
>function. The reason for using "clean.args" is in case you are passing
>a
>fixed list of arguments, all of which may not be appropriate for any of
>the
>functions that you want to pass as "testfun".
>
>Jim
>
>__
>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] save objects of own function to workspace

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 10:59 AM, Marion Wenty wrote:


Thank you very much for your help, David!

Now I have got an object which I can work with.

You mentioned that I took two steps to create the new column names.
I had tried doing it in one step but couldn't find out how. Could  
you help me with that, as well?


Thank you very much!

Marion


2012/2/14 David Winsemius 

On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:

Dear R-helpers,

I created an own function which looks like this

s_elternmz <- function(Var="balt")

   {
 Dg_a<-mz[,c("asbhh","apkz",Var)]
 colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
 colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")


The above line seemed to be the first change to make _m


 mz_int<-merge(mz,Dg_a,by=c("asbhh","bpkzm"),all.x=T)
 colnames(Dg_a)[colnames(Dg_a)=="bpkzm"]<-"bpkzv"

colnames(Dg_a)[colnames(Dg_a)==paste(Var,"_m",sep="")]<- 
paste(Var,"_v",sep="")


Then this line changed _m to become _v. Why not change it to  
_v in the first place?




Why take two steps to change that column name?








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] Access to OpenBLAS

2012-02-14 Thread Dirk Eddelbuettel

On 14 February 2012 at 09:01, Scott Raynaud wrote:
| My IT people have set up a Kubuntu box with an RWkard front
| end.  I have OpenBLAS set up as a shared BLAS but I'm not
| sure how to get R to see it.  A.3.1 of the installation docs talks
| about it but I'm not clear if I need a option on my startup line or
| if I need to find a config file.  The BLAS is is in:
|  
| /usr/lib/openblas-base
|  
| on my machine. Recommendations?

This is clearly a question for the R-SIG-Debian list dedicated to R on both
Debian and Ubuntu.

In short, this should just work as BLAS libraries (from the distribution) are
plug-and-play. You can verify via the ldd command.

Dirk

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx

__
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] Access to OpenBLAS

2012-02-14 Thread Prof Brian Ripley

On 14/02/2012 17:01, Scott Raynaud wrote:

My IT people have set up a Kubuntu box with an RWkard front
end.  I have OpenBLAS set up as a shared BLAS but I'm not
sure how to get R to see it.  A.3.1 of the installation docs talks
about it but I'm not clear if I need a option on my startup line or
if I need to find a config file.  The BLAS is is in:

/usr/lib/openblas-base

on my machine. Recommendations?

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


AFAIK OpenBLAS is a version of the Goto BLAS, so the comments about that 
apply.  You should be able to link it as a shared BLAS.  It is not a 
configurable option when running R, nor do you need to re-configure R.


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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] How to suppress the empty plots in xyplot (lattice)

2012-02-14 Thread Jeff Newmiller
Set up a single (factor) variable that identifies the combinations that exist, 
and plot using that variable.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Jun Shen  wrote:

>Dear all,
>
>In a plot command like
>
>xyplot(Y~X|ID*PERIOD,data=...)
>
>xyplot will generate all the possible ID*PERIOD combinations. But not
>all
>of them have data in there. So I have a lot of empty plots. How can I
>suppress those empty plots and ask xyplot only to generate plots
>actually
>with data. Thanks.
>
>Jun
>
>   [[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] cumsum function to determine plankton phenology

2012-02-14 Thread David L Carlson
Is it necessary to match the Excel results? If not, you could just use
approx or approxfun to do a linear interpolation of the cumulative sums at
each of the three points. It would be simpler, faster, and easier to
understand. Taking mean(.15-datayear$cumsum) is giving you the mean of all
the differences, not just the two smallest absolute values.

--
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352



-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Heather Anne Wright
Sent: Tuesday, February 14, 2012 10:35 AM
To: r-help@r-project.org
Subject: [R] cumsum function to determine plankton phenology

Apologies for the empty email earlier!

I have species abundance data sampled at a weekly frequency or
sometimes monthly depending on the year.
The goal is to identify the dates in an annual cycle in which the
cumulative abundance of a species reaches some threshold.

Here's an example of the data for 1 species over an annual period:
"mc_pheno" is the object created from this data:

zoo_date sp
1/8/1988 13.2
1/20/1988 11.6
2/3/1988 8.7
2/17/1988 10.7
3/3/1988 2.5
3/21/1988 2.2
4/12/1988 0.6
5/2/1988 0.3
5/10/1988 0.6
5/25/1988 0.8
6/8/1988 0.8
6/28/1988 3.8
7/5/1988 11.2
7/20/1988 9.3
8/3/1988 31.1
8/17/1988 50.9
9/6/1988 225
9/15/1988 172.6
10/18/1988 752.1
11/3/1988 28
11/15/1988 32.8
11/30/1988 10.6
12/14/1988 2.6

I want to obtain the start, middle and end dates of cumulative
abundance (for those ecologically minded folks the reference is in a
Greve, et al., 2005 paper) to determine phenology of a species or
functional group. I previously applied this method in Excel and want
to replicate the formula in R using cumsum.

Easy right?
Excel method:
1. calculate cumulative sum
2. divide the cumulative sum for each time point by the annual
cumulative sum or maximum value
3. Calculate three different percentile thresholds corresponding to
the start (when a population reaches 15% of cumulative annual
abundance), the middle (50% of cumulative abundance) or end (85%).
This is done by taking an average between the original sampling dates
that most closely correspond to the threshold values. The function in
excel is INDEX and in R is which.min.

R method:
# convert data
zoo_date <- strptime(mc_pheno$zoo_date,"%m/%d/%Y")

# check dates
str(zoo_date)

# prepare data frame
data<-na.omit(data.frame(date=zoo_date,sp=mc_pheno[,2])

# start the series
datayear<-data[format(data$date,"%Y")==1984,]

# calculate cumulative sum
datayear$cumsum<-cumsum(datayear$sp)
# calculate cumsum standardised
datayear$cumsum<-cumsum(datayear$sp)/max(cumsum(datayear$sp))

# take index of minimum value of cumsum
  idq15<-which.min(mean(.15-datayear$cumsum))
  idq50<-which.min(mean(.50-datayear$cumsum))
  idq85<-which.min(mean(.85-datayear$cumsum))

#take corresponding date
 dateq15<-datayear$date[idq15]
 dateq50<-datayear$date[idq50]
 dateq85<-datayear$date[idq85]

# output results in day in year form
print(format(c(dateq15,dateq50,dateq85),"%j")
# results should be similar to this:
[1] "2010-03-02 CET"  "2010-06-29 CEST" "2010-10-27 CEST"
with dates formatted as a day in year corresponding to each threshold

The problem lies in the comparison between methods between Excel and
R. Using the original method in R, the dates obtained are always
earlier than with Excel due to the which.min calculation:
idq15<-which.min(abs(.15-datayear$cumsum))
idq50<-which.min(abs(.50-datayear$cumsum))
idq85<-which.min(abs(.85-datayear$cumsum))

If I change the formula to
idq15<-which.min(mean(.15-datayear$cumsum))  how do I take an average
between the dates so each threshold is consistent between the results
I obtain in R and excel?
Adding the mean to this line instead of abs resulted in obtaining the
exact same output dates.

Happy to provide more code but this is long enough.
Thanks for your input!
---
Heather A. Wright, PhD candidate
Ecology and Evolution of Plankton
Stazione Zoologica Anton Dohrn
Villa Comunale
80121 - Napoli, Italy
Lab: +39 081 583 3201
Fax: +39 081 764 1355

__
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] Access to OpenBLAS

2012-02-14 Thread Scott Raynaud
My IT people have set up a Kubuntu box with an RWkard front
end.  I have OpenBLAS set up as a shared BLAS but I'm not
sure how to get R to see it.  A.3.1 of the installation docs talks
about it but I'm not clear if I need a option on my startup line or
if I need to find a config file.  The BLAS is is in:
 
/usr/lib/openblas-base
 
on my machine. Recommendations?

__
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] How to suppress the empty plots in xyplot (lattice)

2012-02-14 Thread Jun Shen
Dear all,

In a plot command like

xyplot(Y~X|ID*PERIOD,data=...)

xyplot will generate all the possible ID*PERIOD combinations. But not all
of them have data in there. So I have a lot of empty plots. How can I
suppress those empty plots and ask xyplot only to generate plots actually
with data. Thanks.

Jun

[[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] If (x > 0)

2012-02-14 Thread ilai
On Tue, Feb 14, 2012 at 6:41 AM, Schmidt, Michael
 wrote:
> So the function must come BEFORE the call to the function...I see.

Yes. May be different than what you're used to but in R think of
functions as just another set of "objects". Therefore they must be
declared in the environment from which they are called beforehand,
just like the arguments.

Cheers

Elai.

> Thanks for that info.
> Take care
> Mike
>
> -Original Message-
> From: ila...@gmail.com [mailto:ila...@gmail.com] On Behalf Of ilai
> Sent: Monday, February 13, 2012 6:19 PM
> To: Schmidt, Michael
> Subject: Re: [R] If (x > 0)
>
> After placing res <- conditional1(x) ; cat("result= ",res,"\n") AFTER 
> defining the function conditional1 (or R wouldn't know what "conditional1" 
> is), your script worked flawlessly for me.
> From the terminal in xubuntu10.04:
> $ R --no-save --slave --args 1 < test1.R [1] 1 result=  1 $ R --no-save 
> --slave --args -1e-6 < test1.R [1] -1e-06 result=  -1 $ R --no-save --slave 
> --args 0 < test1.R [1] 0 result=  0
>
> So maybe you had an old (bad) version of conditional1 which was used by test1 
> ?
>
> Cheers
>
>
>
> On Mon, Feb 13, 2012 at 2:59 PM, Schmidt, Michael  
> wrote:
>> Hi,
>> I am new to R. I was trying to get a very simple program to run. Take
>> one number from the command line. If the number < 0 return -1. If
>> number > 0 return 1 and if the number == 0 return 0. The code is in a
>> file called test1.R
>>
>>
>> The code:
>>
>> #useage: R --no-save --args 5 < test1.R args = (commandArgs(TRUE)) x =
>> as.numeric(args[1])
>> print(x)
>>
>> res <- conditional1(x)
>> cat("result= ",res,"\n")
>>
>> conditional1 <- function(x){
>>        result <- 0
>>        if (x > 0) {
>>                result <- 1
>>        } else if (x < 0) {
>>                result <- -1
>>        }
>>        return(result)
>> }
>>
>>                result <- 1
>>        } else if (x < 0) {
>>                result <- -1
>>        }
>>        return(result)
>> }
>>
>>
>> The output:
>>>R --no-save --slave --args 1 < test1.R
>> [1] 1
>> result=  1
>>>R --no-save --slave --args -1 < test1.R
>> [1] -1
>> result=  -1
>>>] R --no-save --slave --args 0 < test1.R
>> [1] 0
>> result=  -1
>>
>>
>> The problem:
>> For arguments 1 and -1 it works as intended. For 0 (zero) it does not. If 
>> the 0 value is passed into the function named "conditional1"  I would expect 
>> both if-statements to evaluate to false and 0 being return. From what I can 
>> tell (0 < 0) evaluates to true since -1 is returned. Hm...
>> What is going on? What am I doing wrong? Why is this happening? I am baffled!
>> Any help would be appreciated.
>> Thanks
>> Mike
>>
>>
>>
>>
>>        [[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] cross validation in rvm not working? (kernlab package)

2012-02-14 Thread Uwe Ligges
Please report bugs in packages to the corresponding package maintainer 
(perhaps suggesting a fix if you have an idea how to do that).


Uwe Ligges

On 14.02.2012 12:42, Martin Batholdy wrote:

Hi,

according to ?rvm the relevance vector machine function as implemented in the 
kernlab-package
has an argument 'cross' with which you can perform k-fold cross validation.

However, when I try to add a 10-fold cross validation I get the following error 
message:

Error in match.arg(type, c("C-svc", "nu-svc", "kbb-svc", "spoc-svc", "C-bsvc",  
:
   'arg' should be one of “C-svc”, “nu-svc”, “kbb-svc”, “spoc-svc”, “C-bsvc”, 
“one-svc”, “eps-svr”, “eps-bsvr”, “nu-svr”


code-example:

# create data
x<- seq(-20,20,0.1)
y<- sin(x)/x + rnorm(401,sd=0.05)

# train relevance vector machine
foo<- rvm(x, y, cross=10)


So, does that mean that cross-validation is not working for rvm at the moment?
(since the type argument only allows support vector regression or 
classification)

__
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] save output of loop

2012-02-14 Thread johnmark
The short answer to your question is *don't* concatenate the values in the
row, then attempt to /rbind()/ them incrementally to a data.frame.  Instead
build each column separately inside the loop, then /cbind() (data.frame()/ 
does an implicit/ cbind()/ ) them together at the end.  Something like this:

/
lat.column <- c(length(100))
lon.column <- c(length(100))
...
for (i in 1:100){ 
 ...
  lat.column[i]  <- data[,7] # latitude 
  lon.column[i] <- data[,8] # longitude 
   ...
} 
my.data <- data.frame(lat.column, lon.column, ...)
/

-jm


--
View this message in context: 
http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4387476.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Selecting elements from all items in a list

2012-02-14 Thread geotheory
Yes the lapply function sorted it out.  Thanks for the advice.

--
View this message in context: 
http://r.789695.n4.nabble.com/Selecting-elements-from-all-items-in-a-list-tp4387045p4387505.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] cumsum function to determine plankton phenology

2012-02-14 Thread Heather Anne Wright
Apologies for the empty email earlier!

I have species abundance data sampled at a weekly frequency or
sometimes monthly depending on the year.
The goal is to identify the dates in an annual cycle in which the
cumulative abundance of a species reaches some threshold.

Here's an example of the data for 1 species over an annual period:
"mc_pheno" is the object created from this data:

zoo_date sp
1/8/1988 13.2
1/20/1988 11.6
2/3/1988 8.7
2/17/1988 10.7
3/3/1988 2.5
3/21/1988 2.2
4/12/1988 0.6
5/2/1988 0.3
5/10/1988 0.6
5/25/1988 0.8
6/8/1988 0.8
6/28/1988 3.8
7/5/1988 11.2
7/20/1988 9.3
8/3/1988 31.1
8/17/1988 50.9
9/6/1988 225
9/15/1988 172.6
10/18/1988 752.1
11/3/1988 28
11/15/1988 32.8
11/30/1988 10.6
12/14/1988 2.6

I want to obtain the start, middle and end dates of cumulative
abundance (for those ecologically minded folks the reference is in a
Greve, et al., 2005 paper) to determine phenology of a species or
functional group. I previously applied this method in Excel and want
to replicate the formula in R using cumsum.

Easy right?
Excel method:
1. calculate cumulative sum
2. divide the cumulative sum for each time point by the annual
cumulative sum or maximum value
3. Calculate three different percentile thresholds corresponding to
the start (when a population reaches 15% of cumulative annual
abundance), the middle (50% of cumulative abundance) or end (85%).
This is done by taking an average between the original sampling dates
that most closely correspond to the threshold values. The function in
excel is INDEX and in R is which.min.

R method:
# convert data
zoo_date <- strptime(mc_pheno$zoo_date,"%m/%d/%Y")

# check dates
str(zoo_date)

# prepare data frame
data<-na.omit(data.frame(date=zoo_date,sp=mc_pheno[,2])

# start the series
datayear<-data[format(data$date,"%Y")==1984,]

# calculate cumulative sum
datayear$cumsum<-cumsum(datayear$sp)
# calculate cumsum standardised
datayear$cumsum<-cumsum(datayear$sp)/max(cumsum(datayear$sp))

# take index of minimum value of cumsum
  idq15<-which.min(mean(.15-datayear$cumsum))
  idq50<-which.min(mean(.50-datayear$cumsum))
  idq85<-which.min(mean(.85-datayear$cumsum))

#take corresponding date
 dateq15<-datayear$date[idq15]
 dateq50<-datayear$date[idq50]
 dateq85<-datayear$date[idq85]

# output results in day in year form
print(format(c(dateq15,dateq50,dateq85),"%j")
# results should be similar to this:
[1] "2010-03-02 CET"  "2010-06-29 CEST" "2010-10-27 CEST"
with dates formatted as a day in year corresponding to each threshold

The problem lies in the comparison between methods between Excel and
R. Using the original method in R, the dates obtained are always
earlier than with Excel due to the which.min calculation:
idq15<-which.min(abs(.15-datayear$cumsum))
idq50<-which.min(abs(.50-datayear$cumsum))
idq85<-which.min(abs(.85-datayear$cumsum))

If I change the formula to
idq15<-which.min(mean(.15-datayear$cumsum))  how do I take an average
between the dates so each threshold is consistent between the results
I obtain in R and excel?
Adding the mean to this line instead of abs resulted in obtaining the
exact same output dates.

Happy to provide more code but this is long enough.
Thanks for your input!
---
Heather A. Wright, PhD candidate
Ecology and Evolution of Plankton
Stazione Zoologica Anton Dohrn
Villa Comunale
80121 - Napoli, Italy
Lab: +39 081 583 3201
Fax: +39 081 764 1355

__
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] passing an extra argument to an S3 generic

2012-02-14 Thread ilai
Hi Michael,
Try the attached. The only change to your script is in the first line
where I explicitly tell hatvalues to use methods (the "infmlm" class
stays). I also commented out all your TESTME at the end.

source('mlminfl-testHELP.R')

Now this should have worked for you too. Let me know. Sorry about that
"just remove the class". Had somewhat of a brain glitch when writing
the E-mail and wasn't clear.

Cheers






On Tue, Feb 14, 2012 at 8:05 AM, Michael Friendly  wrote:
> On 2/11/2012 12:00 PM, ilai wrote:
>>
>> You are setting a new class ("inflmlm") at the end of mlm.influence.
>> Remove that second to last line and enjoy your new S3 method.
>
>
> Thanks for the suggestion, but it doesn't help -- I still get the same
> behavior whether mlm.influence returns a classed object or not.
> As well, I am defining print.inflmlm() and as.data.frame.inflmlm() methods
> for these objects, so I do need mlm.influence to return a classed object.
>
> My hatvalues.mlm is designed to be similar in structure to
> stats::hatvalues.lm where the S3 generic is defined.
>
>
> hatvalues.mlm <- function(model, m=1, infl, ...)
> {
>   if (missing(infl)) {
>     infl <- mlm.influence(model, m=m, do.coef=FALSE);
>   }
>    hat <- infl$H
>    m <- infl$m
>    names(hat) <- if(m==1) infl$subsets else apply(infl$subsets,1, paste,
> collapse=',')
>    hat
> }
>
>> hatvalues
> function (model, ...)
> UseMethod("hatvalues")
> 
> 
>> hatvalues.lm
>
> function (model, infl = lm.influence(model, do.coef = FALSE),
>    ...)
> {
>    hat <- infl$hat
>    names(hat) <- names(infl$wt.res)
>    hat
> }
> 
> 
>
> The idea is that the infl= argument specifies a call to the computational
> function, mlm.influence() in my case, just as lm.influence() does in the
> stats package.
>
> The logic of UseMethod is that it should dispatch on the class of the
> *first* argument to the function, which in my test case is c("mlm", "lm")
>
>
>> Rohwer.mod <- lm(cbind(SAT, PPVT, Raven) ~ n+s+ns+na+ss, data=Rohwer2)
>> class(Rohwer.mod)
> [1] "mlm" "lm"
>
>> trace(hatvalues)
>> hatvalues(Rohwer.mod, m=2)
> trace: hatvalues(Rohwer.mod, m = 2)
>
> Error in UseMethod("hatvalues") :
>  no applicable method for 'hatvalues' applied to an object of class
> "c('double', 'numeric')"
>> hatvalues(Rohwer.mod)
> trace: hatvalues(Rohwer.mod)
>
>         1          2          3          4          5          6     7
>    8
> 0.16700926 0.21845327 0.14173469 0.07314341 0.56821462 0.15432157 0.04530969
> 0.17661104
> ...
>
>
> I'm still stumped on why with the extra argument m=2, R sees this
> as an object of class c('double', 'numeric').  As well, I can't see
> any way to debug this.
>
>
>
>> I'm not sure, but I think it is just the new class "inflmlm" applied
>> to inf in the formals of hatvalues.mlm confused the dispatch
>> mechanism. You would think the error message will call the offending
>> class not "numeric" double" but that's above my pay grade...
>>
>> You could probably put back the inflmlm class assignment with an
>> explicit call to UseMethod in hatvalues.mlm ?
>>
>> Cheers
>>
>> On Fri, Feb 10, 2012 at 2:35 PM, Michael Friendly
>>  wrote:
>>>
>>> On 2/10/2012 4:09 PM, Henrik Bengtsson wrote:


 So people may prefer to do the following:

 hatvalues.mlm<- function(model, m=1, infl, ...)
 {
    if (missing(infl)) {
      infl<- mlm.influence(model, m=m, do.coef=FALSE);
    }

    hat<- infl$H
    m<- infl$m
    names(hat)<- if(m==1) infl$subsets else apply(infl$subsets,1,
 paste, collapse=',')
    hat
 }
>>>
>>>
>>> Thanks;  I tried exactly that, but I still can't pass m=2 to the mlm
>>> method
>>> through the generic
>>>
 hatvalues(Rohwer.mod)
>>>
>>>         1          2          3          4          5          6
>>>  7
>>>          8
>>> 0.16700926 0.21845327 0.14173469 0.07314341 0.56821462 0.15432157
>>> 0.04530969
>>> 0.17661104
>>>         9         10         11         12         13         14
>>> 15
>>>         16
>>> 0.05131298 0.45161152 0.14542776 0.17050399 0.10374592 0.12649927
>>> 0.33246744
>>> 0.33183461
>>>        17         18         19         20         21         22
>>> 23
>>>         24
>>> 0.17320579 0.26353864 0.29835817 0.07880597 0.14023750 0.19380286
>>> 0.04455330
>>> 0.20641708
>>>        25         26         27         28         29         30
>>> 31
>>>         32
>>> 0.15712604 0.15333879 0.36726467 0.11189754 0.30426999 0.08655434
>>> 0.08921878
>>> 0.07320950
>>>
 hatvalues(Rohwer.mod, m=2)
>>>
>>> Error in UseMethod("hatvalues") :
>>>  no applicable method for 'hatvalues' applied to an object of class
>>> "c('double', 'numeric')"
>>>
>>> ## This works:

 hatvalues.mlm(Rohwer.mod, m=2)
>>>
>>>   ... output snipped
>>>
 hatvalues
>>>
>>>
>>> function (model, ...)
>>> UseMethod("hatvalues")
>>> 
>>> 
>>>

>>>
>>> -Michael
>>>
>>>
>>> --
>>> Michael Friendly     Email: friendly AT yorku DOT ca
>>> Professor, Psychology Dept.
>>> York Univer

Re: [R] Writing R-scripts

2012-02-14 Thread Cem Girit
Hello Jim,

Thank you. This will really help me to get the result I need.  But I
am having an error message when I run the code although "testtype" is set to
2 in the calling function to run the Dunnet test. What am I doing wrong?

> pt<-parametric.tests(testtype=2, resp, groups, vehicle="Control",
alpha=0.05)
Error in clean.args(resp, group, vehicle, alpha, lmat, testtype) : 
  unused argument(s) (testtype)

library(plotrix)

parametric.tests<-function(testtype, resp, group, vehicle, alpha, lmat){

result<-do.call(deparse(substitute(testtype)), 
 clean.args(resp, group, vehicle, alpha, lmat, testtype))
return(result)
}

if (testtype==1){

### 
## resp:  response variable, must be numeric and vector
## group: group id for resp, numeric or character
## alpha: CL 0.05 or 0.01

## Bonferroni
 bonferroni.test <- function(resp, group, alpha) 
{
.

 result <- data.frame(label=label, estimate=estimate, alpha=alpha,
p.value=pval, significance=sig)
 return(result)
}
} else if (testtype==2){

###
## resp:  response variable, must be numeric and vector
## group: group id for resp, numeric or character
## alpha: CL 0.05 or 0.01
## vehicle: label for the reference group (for example, vehicle = "Control"
if the lable for the ## reference group is 'Control')

## Dunnett 
dunnett.test <- function(resp, group, alpha, vehicle)
{


 result <- data.frame(label=label, estimate=estimate, alpha=alpha,
lower=lower, upper=upper, p.value=pval, significance=sig)
 return(result)
}
}

Cem

-Original Message-
From: Jim Lemon [mailto:j...@bitwrit.com.au] 
Sent: Tuesday, February 14, 2012 3:32 AM
To: Cem Girit
Cc: r-help@r-project.org
Subject: Re: [R] Writing R-scripts

On 02/14/2012 08:59 AM, Cem Girit wrote:
> Hello,
>
>   This is my first attempt to write a script in R. The program below
is 
> intended to do some parametric tests on group data. There are 
> subroutines for each type of test. The call to the "parametric.tests", 
> routine sets the argument "testtype" for the test to be used. How can 
> I transfer the calculated values (in "result" below) in each routine 
> to the calling parametric.tests routine?
>
Hi Cem,
You may find it easier to use the do.call method and the clean.args
function:

library(plotrix)
parametric.tests<-function(testfun,arglist) {
  result<-do.call(deparse(substitute(testfun)),
   clean.args(arglist,testfun))
  return(result)
}
parametric.tests(mean,list(x=1:5,na.rm=TRUE,foo="?"))

If the function called ("testfun") is available, and the argument "arglist"
contains sufficient arguments for it to run, it will return the value of the
function. The reason for using "clean.args" is in case you are passing a
fixed list of arguments, all of which may not be appropriate for any of the
functions that you want to pass as "testfun".

Jim

__
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] save objects of own function to workspace

2012-02-14 Thread Jeff Newmiller
It is possible to do what you ask using <<- instead of <-, but from the 
perspective of using the function it will be much clearer if you add mz as an 
argument to the function along with Var, and return mz_int at the end. (The 
easiest way to return a variable is to type it alone on the last line if your 
function.) Then you can assign the return value to some variable of your 
choosing (including possibly one named mz_int in the calling environment) where 
you call the function.
---
Jeff NewmillerThe .   .  Go Live...
DCN:Basics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k
--- 
Sent from my phone. Please excuse my brevity.

Marion Wenty  wrote:

>Dear R-helpers,
>
>I created an own function which looks like this
>
>s_elternmz <- function(Var="balt")
>{
>  Dg_a<-mz[,c("asbhh","apkz",Var)]
>  colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
>  colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")
>  mz_int<-merge(mz,Dg_a,by=c("asbhh","bpkzm"),all.x=T)
>  colnames(Dg_a)[colnames(Dg_a)=="bpkzm"]<-"bpkzv"
>
>colnames(Dg_a)[colnames(Dg_a)==paste(Var,"_m",sep="")]<-paste(Var,"_v",sep="")
>  mz_int<-merge(mz_int,Dg_a,by=c("asbhh","bpkzv"),all.x=T)
>  mz_int <-
>mz_int[order(mz_int$asbhh,mz_int$apkz),c(colnames(mz),paste(Var,"_m",sep=""),paste(Var,"_v",sep=""))]
> }
>
>My problem is that the objects are not saved in the workspace.
>Especially I
>need the object which I created in the end - mz_int - because I want to
>do
>calculations with it in R afterwards.
>
>Thank you very much for your help in advance.
>
>Marion
>
>   [[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] save objects of own function to workspace

2012-02-14 Thread Marion Wenty
Thank you very much for your help, David!

Now I have got an object which I can work with.

You mentioned that I took two steps to create the new column names.
I had tried doing it in one step but couldn't find out how. Could you help
me with that, as well?

Thank you very much!

Marion


2012/2/14 David Winsemius 

>
> On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:
>
>  Dear R-helpers,
>>
>> I created an own function which looks like this
>>
>> s_elternmz <- function(Var="balt")
>>
>
> {
>>  Dg_a<-mz[,c("asbhh","apkz",**Var)]
>>  colnames(Dg_a)[colnames(Dg_a)=**="apkz"]<-"bpkzm"
>>  colnames(Dg_a)[colnames(Dg_a)=**=Var]<-paste(Var,"_m",sep="")
>>  mz_int<-merge(mz,Dg_a,by=c("**asbhh","bpkzm"),all.x=T)
>>  colnames(Dg_a)[colnames(Dg_a)=**="bpkzm"]<-"bpkzv"
>>
>> colnames(Dg_a)[colnames(Dg_a)=**=paste(Var,"_m",sep="")]<-**
>> paste(Var,"_v",sep="")
>>
>
> Why take two steps to change that column name?
>
>
>   mz_int<-merge(mz_int,Dg_a,by=**c("asbhh","bpkzv"),all.x=T)
>>
>
> You just overwrote an object that you created earklier but have not done
> anything with in the meantime.
>
>   mz_int <-
>> mz_int[order(mz_int$asbhh,mz_**int$apkz), c(colnames(mz),paste(Var,"_m",*
>> *sep=""), paste(Var,"_v",sep=""))]
>>
>> }
>>
>> My problem is that the objects are not saved in the workspace. Especially
>> I
>> need the object which I created in the end - mz_int - because I want to do
>> calculations with it in R afterwards.
>>
>
> Then just assign it:
>
> mz_result <- s_elternmz()
>
> As you have noticed, assignment inside a function is not a durable event.
> The function returns the value of either its first encounter with return()
> or the value
> of hte last assignment (but not the name to which that object was
> assigned.) Welcome to R's version of functional programming.
>
>
>> Thank you very much for your help in advance.
>>
>> Marion
>>
>>[[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.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>

[[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] fit data to y~A+B*sin(C*x)

2012-02-14 Thread Bert Gunter
Note that, given C, A and B can be obtained by simple linear regression of y on 
sin(Cx). Hence you could avoid nls altogether by a simple search of the minimal 
ls solution(possibly robust) over a grid of C values. Or do this to find good 
starting values for nls.

Bert

Sent from my iPhone -- please excuse typos.

On Feb 14, 2012, at 7:24 AM, Berend Hasselman  wrote:

> nlmod

__
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] fit data to y~A+B*sin(C*x)

2012-02-14 Thread Berend Hasselman

On 13-02-2012, at 23:54, Jonas Stein wrote:

> I want to fit discrete data that was measured on a wavegenerator.
> In this minimal example i generate some artificial data:
> 
> testsin <- 2+ 5 * sin(1:100) #generate sin data
> testsin <-  testsin+ rnorm(length(testsin), sd = 0.01) #add noise
> 
> mydata <- list(X=1:100, Y=testsin) # generate mydata object
> 
> nlmod <- nls(X ~ A+B*sin(C* Y), data=mydata, start=list(A=2, B=4, C=1), 
> trace=TRUE)
> 
> # this nls fit fails. 

What do you mean by "fail"
> nlmod
Nonlinear regression model
  model:  X ~ A + B * sin(C * Y) 
   data:  mydata 
  A   B   C 
50.7553  0.6308  0.8007 
 residual sum-of-squares: 83308

Number of iterations to convergence: 24 
Achieved convergence tolerance: 7.186e-06 

Results don't seem to look ok.
But I think you made a small mistake in the formula.
The argument to sin in testsin is 1:100 but that's not what you are giving nls.

Try this

> nlmod <- nls(Y ~ A+B*sin(C* X), data=mydata, start=list(A=2, B=4, C=1), 
> trace=TRUE)
50.30593 :  2 4 1 
0.01014092 :  2.0003732 5.0002681 0.979 
0.01014016 :  2.0003732 5.0002681 0.983 
> nlmod
Nonlinear regression model
  model:  Y ~ A + B * sin(C * X) 
   data:  mydata 
A B C 
2 5 1 
 residual sum-of-squares: 0.01014

Number of iterations to convergence: 2 
Achieved convergence tolerance: 1.201e-07 

Looks better?

Berend

__
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] Wildcard for indexing?

2012-02-14 Thread Johannes Radinger


 Original-Nachricht 
> Datum: Tue, 14 Feb 2012 10:18:33 -0500
> Von: Sarah Goslee 
> An: Johannes Radinger 
> CC: R-help@r-project.org
> Betreff: Re: [R] Wildcard for indexing?

> Hi,
> 
> You should probably do a bit of reading about regular expressions, but
> here's one way:
> 
> On Tue, Feb 14, 2012 at 10:10 AM, Johannes Radinger 
> wrote:
> > Hi,
> >
> >  Original-Nachricht 
> >> Datum: Tue, 14 Feb 2012 09:59:39 -0500
> >> Von: "R. Michael Weylandt" 
> >> An: Johannes Radinger 
> >> CC: R-help@r-project.org
> >> Betreff: Re: [R] Wildcard for indexing?
> >
> >> I think the grep()-family (regular expressions) will be the easiest
> >> way to do this, though it sounds like you might prefer grepl() which
> >> returns a logical vector:
> >>
> >> ^[AB] # Starts with either an A or a B
> >> ^A_ # Starting with A_
> >>
> >> a <-  c("A_A","A_B","C_A","BB","A_Asd"
> >> grepl("^[AB]", a)
> >> grepl("^A_")
> >
> > Yes grepl() is what I am looking for.
> > is there also something like an OR statement e.g. if I want to
> > select for elements that start with "as" OR "df"?
> 
> > a <- c("as1", "bb", "as2", "cc", "df", "aa", "dd", "sdf")
> > grepl("^as|^df", a)
> [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
> 
> 
> The square brackets match any of those characters, so are good
> for single characters. For more complex patterns, | is the or symbol.
> ^ marks the beginning.

Thank you so much Sarah! I tried that | symbol intuitively, there was just a 
problem with the quotation marks :(

Now everything is solved...

/johannes

> 
> Sarah
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org

--

__
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] Wildcard for indexing?

2012-02-14 Thread Duncan Murdoch

On 14/02/2012 10:10 AM, Johannes Radinger wrote:

Hi,

 Original-Nachricht 
>  Datum: Tue, 14 Feb 2012 09:59:39 -0500
>  Von: "R. Michael Weylandt"
>  An: Johannes Radinger
>  CC: R-help@r-project.org
>  Betreff: Re: [R] Wildcard for indexing?

>  I think the grep()-family (regular expressions) will be the easiest
>  way to do this, though it sounds like you might prefer grepl() which
>  returns a logical vector:
>
>  ^[AB] # Starts with either an A or a B
>  ^A_ # Starting with A_
>
>  a<-  c("A_A","A_B","C_A","BB","A_Asd"
>  grepl("^[AB]", a)
>  grepl("^A_")

Yes grepl() is what I am looking for.
is there also something like an OR statement e.g. if I want to
select for elements that start with "as" OR "df"?


grepl("^(as|df)", a)

should work.  See ?regexp for all the possibilities, rules about 
operator precedence, etc.  (Or just use grepl("^as", a) | grepl("^df", a).)


Duncan Murdoch


/johannes

>
>  Michael
>
>  On Tue, Feb 14, 2012 at 9:54 AM, Johannes Radinger
>  wrote:
>  >  Hi,
>  >
>  >  I'd like to know if it is possible to use wildcards * for indexing...
>  >  E.g. I have a vector of strings. Now I'd like to select all elements
>  >  which start with A_*? I'd also need to combine that with logical
>  operators:
>  >
>  >  "Select all elements of a vector that start with A (A*) OR that start
>  with B (B*)"
>  >
>  >  Probably that is quite easy. I looked into grep() which I think might
>  perform such tasks, but probably there is a more straigth forward solution.
>  >
>  >  a<- c("A_A","A_B","C_A","BB","A_Asd")
>  >  a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard
>  >
>  >  /johannes
>  >  --
>  >
>  >  __
>  >  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.


Re: [R] fit data to y~A+B*sin(C*x)

2012-02-14 Thread David L Carlson
Don't you want to predict testsin (aka Y) from X? 

> nlmod <- nls(Y ~ A+B*sin(C* X), data=mydata, start=list(A=2, B=4, C=1),
trace=TRUE)
50.42965 :  2 4 1 
0.007393982 :  1.9989006 5.0014997 0.902 
0.007377637 :  1.9989005 5.0015002 0.922 
0.007377637 :  1.9989005 5.0015002 0.922 
> nlmod
Nonlinear regression model
  model:  Y ~ A + B * sin(C * X) 
   data:  mydata 
A B C 
1.999 5.002 1.000 
 residual sum-of-squares: 0.007378

Number of iterations to convergence: 3 
Achieved convergence tolerance: 1.499e-08

--
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of Sarah Goslee
Sent: Tuesday, February 14, 2012 9:13 AM
To: Jonas Stein
Cc: r-h...@stat.math.ethz.ch
Subject: Re: [R] fit data to y~A+B*sin(C*x)

Hi Jonas,

I'm afraid we need more detail.

On Mon, Feb 13, 2012 at 5:54 PM, Jonas Stein  wrote:
> I want to fit discrete data that was measured on a wavegenerator.
> In this minimal example i generate some artificial data:
>
> testsin <- 2+ 5 * sin(1:100) #generate sin data
> testsin <-  testsin+ rnorm(length(testsin), sd = 0.01) #add noise
>
> mydata <- list(X=1:100, Y=testsin) # generate mydata object
>
> nlmod <- nls(X ~ A+B*sin(C* Y), data=mydata, start=list(A=2, B=4, C=1),
trace=TRUE)
>
> # this nls fit fails.
>

Fails how? It runs when I copy and paste your code into a terminal, given:
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

What happens when you run it? What do you expect to happen?
What's your sessionInfo()?

"fails" doesn't convey enough information here.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] Wildcard for indexing?

2012-02-14 Thread Sarah Goslee
Hi,

You should probably do a bit of reading about regular expressions, but
here's one way:

On Tue, Feb 14, 2012 at 10:10 AM, Johannes Radinger  wrote:
> Hi,
>
>  Original-Nachricht 
>> Datum: Tue, 14 Feb 2012 09:59:39 -0500
>> Von: "R. Michael Weylandt" 
>> An: Johannes Radinger 
>> CC: R-help@r-project.org
>> Betreff: Re: [R] Wildcard for indexing?
>
>> I think the grep()-family (regular expressions) will be the easiest
>> way to do this, though it sounds like you might prefer grepl() which
>> returns a logical vector:
>>
>> ^[AB] # Starts with either an A or a B
>> ^A_ # Starting with A_
>>
>> a <-  c("A_A","A_B","C_A","BB","A_Asd"
>> grepl("^[AB]", a)
>> grepl("^A_")
>
> Yes grepl() is what I am looking for.
> is there also something like an OR statement e.g. if I want to
> select for elements that start with "as" OR "df"?

> a <- c("as1", "bb", "as2", "cc", "df", "aa", "dd", "sdf")
> grepl("^as|^df", a)
[1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE


The square brackets match any of those characters, so are good
for single characters. For more complex patterns, | is the or symbol.
^ marks the beginning.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] fit data to y~A+B*sin(C*x)

2012-02-14 Thread Sarah Goslee
Hi Jonas,

I'm afraid we need more detail.

On Mon, Feb 13, 2012 at 5:54 PM, Jonas Stein  wrote:
> I want to fit discrete data that was measured on a wavegenerator.
> In this minimal example i generate some artificial data:
>
> testsin <- 2+ 5 * sin(1:100) #generate sin data
> testsin <-  testsin+ rnorm(length(testsin), sd = 0.01) #add noise
>
> mydata <- list(X=1:100, Y=testsin) # generate mydata object
>
> nlmod <- nls(X ~ A+B*sin(C* Y), data=mydata, start=list(A=2, B=4, C=1), 
> trace=TRUE)
>
> # this nls fit fails.
>

Fails how? It runs when I copy and paste your code into a terminal, given:
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C LC_NAME=C
 [9] LC_ADDRESS=C   LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

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

What happens when you run it? What do you expect to happen?
What's your sessionInfo()?

"fails" doesn't convey enough information here.

Sarah

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] Wildcard for indexing?

2012-02-14 Thread Johannes Radinger
Hi,

 Original-Nachricht 
> Datum: Tue, 14 Feb 2012 09:59:39 -0500
> Von: "R. Michael Weylandt" 
> An: Johannes Radinger 
> CC: R-help@r-project.org
> Betreff: Re: [R] Wildcard for indexing?

> I think the grep()-family (regular expressions) will be the easiest
> way to do this, though it sounds like you might prefer grepl() which
> returns a logical vector:
> 
> ^[AB] # Starts with either an A or a B
> ^A_ # Starting with A_
> 
> a <-  c("A_A","A_B","C_A","BB","A_Asd"
> grepl("^[AB]", a)
> grepl("^A_")

Yes grepl() is what I am looking for.
is there also something like an OR statement e.g. if I want to
select for elements that start with "as" OR "df"?

/johannes

> 
> Michael
> 
> On Tue, Feb 14, 2012 at 9:54 AM, Johannes Radinger 
> wrote:
> > Hi,
> >
> > I'd like to know if it is possible to use wildcards * for indexing...
> > E.g. I have a vector of strings. Now I'd like to select all elements
> > which start with A_*? I'd also need to combine that with logical
> operators:
> >
> > "Select all elements of a vector that start with A (A*) OR that start
> with B (B*)"
> >
> > Probably that is quite easy. I looked into grep() which I think might
> perform such tasks, but probably there is a more straigth forward solution.
> >
> > a <- c("A_A","A_B","C_A","BB","A_Asd")
> > a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard
> >
> > /johannes
> > --
> >
> > __
> > 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] passing an extra argument to an S3 generic

2012-02-14 Thread Michael Friendly

On 2/11/2012 12:00 PM, ilai wrote:

You are setting a new class ("inflmlm") at the end of mlm.influence.
Remove that second to last line and enjoy your new S3 method.


Thanks for the suggestion, but it doesn't help -- I still get the same
behavior whether mlm.influence returns a classed object or not.
As well, I am defining print.inflmlm() and as.data.frame.inflmlm() 
methods for these objects, so I do need mlm.influence to return a 
classed object.


My hatvalues.mlm is designed to be similar in structure to 
stats::hatvalues.lm where the S3 generic is defined.


hatvalues.mlm <- function(model, m=1, infl, ...)
{
   if (missing(infl)) {
 infl <- mlm.influence(model, m=m, do.coef=FALSE);
   }
hat <- infl$H
m <- infl$m
names(hat) <- if(m==1) infl$subsets else apply(infl$subsets,1, 
paste, collapse=',')

hat
}

> hatvalues
function (model, ...)
UseMethod("hatvalues")


> hatvalues.lm
function (model, infl = lm.influence(model, do.coef = FALSE),
...)
{
hat <- infl$hat
names(hat) <- names(infl$wt.res)
hat
}



The idea is that the infl= argument specifies a call to the 
computational function, mlm.influence() in my case, just as 
lm.influence() does in the stats package.


The logic of UseMethod is that it should dispatch on the class of the 
*first* argument to the function, which in my test case is c("mlm", "lm")


> Rohwer.mod <- lm(cbind(SAT, PPVT, Raven) ~ n+s+ns+na+ss, data=Rohwer2)
> class(Rohwer.mod)
[1] "mlm" "lm"

> trace(hatvalues)
> hatvalues(Rohwer.mod, m=2)
trace: hatvalues(Rohwer.mod, m = 2)
Error in UseMethod("hatvalues") :
  no applicable method for 'hatvalues' applied to an object of class 
"c('double', 'numeric')"

> hatvalues(Rohwer.mod)
trace: hatvalues(Rohwer.mod)
 1  2  3  4  5  6 
7  8
0.16700926 0.21845327 0.14173469 0.07314341 0.56821462 0.15432157 
0.04530969 0.17661104

...


I'm still stumped on why with the extra argument m=2, R sees this
as an object of class c('double', 'numeric').  As well, I can't see
any way to debug this.



I'm not sure, but I think it is just the new class "inflmlm" applied
to inf in the formals of hatvalues.mlm confused the dispatch
mechanism. You would think the error message will call the offending
class not "numeric" double" but that's above my pay grade...

You could probably put back the inflmlm class assignment with an
explicit call to UseMethod in hatvalues.mlm ?

Cheers

On Fri, Feb 10, 2012 at 2:35 PM, Michael Friendly  wrote:

On 2/10/2012 4:09 PM, Henrik Bengtsson wrote:


So people may prefer to do the following:

hatvalues.mlm<- function(model, m=1, infl, ...)
{
if (missing(infl)) {
  infl<- mlm.influence(model, m=m, do.coef=FALSE);
}

hat<- infl$H
m<- infl$m
names(hat)<- if(m==1) infl$subsets else apply(infl$subsets,1,
paste, collapse=',')
hat
}


Thanks;  I tried exactly that, but I still can't pass m=2 to the mlm method
through the generic


hatvalues(Rohwer.mod)

 1  2  3  4  5  6  7
  8
0.16700926 0.21845327 0.14173469 0.07314341 0.56821462 0.15432157 0.04530969
0.17661104
 9 10 11 12 13 14 15
 16
0.05131298 0.45161152 0.14542776 0.17050399 0.10374592 0.12649927 0.33246744
0.33183461
17 18 19 20 21 22 23
 24
0.17320579 0.26353864 0.29835817 0.07880597 0.14023750 0.19380286 0.04455330
0.20641708
25 26 27 28 29 30 31
 32
0.15712604 0.15333879 0.36726467 0.11189754 0.30426999 0.08655434 0.08921878
0.07320950


hatvalues(Rohwer.mod, m=2)

Error in UseMethod("hatvalues") :
  no applicable method for 'hatvalues' applied to an object of class
"c('double', 'numeric')"

## This works:

hatvalues.mlm(Rohwer.mod, m=2)

   ... output snipped


hatvalues


function (model, ...)
UseMethod("hatvalues")







-Michael


--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University  Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA







--
Michael Friendly Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University  Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele StreetWeb:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

__
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] fit data to y~A+B*sin(C*x)

2012-02-14 Thread Jonas Stein
I want to fit discrete data that was measured on a wavegenerator.
In this minimal example i generate some artificial data:

testsin <- 2+ 5 * sin(1:100) #generate sin data
testsin <-  testsin+ rnorm(length(testsin), sd = 0.01) #add noise

mydata <- list(X=1:100, Y=testsin) # generate mydata object

nlmod <- nls(X ~ A+B*sin(C* Y), data=mydata, start=list(A=2, B=4, C=1), 
trace=TRUE)

# this nls fit fails. 

Who can help me to fit this type of data?
Kind regards,

-- 
Jonas Stein 

__
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] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 9:44 AM, Duncan Murdoch wrote:


On 14/02/2012 7:41 AM, ATANU wrote:
I am using R 2.14.1. I am trying to configure Lyx with Sweave. I  
have read

articles but I found none to be complete  and I cannot import sweave
document in lyx . Can anyone please help me with a stepwise  
procedure how to
configure Lyx with Sweave so that I can run my R-code chunks from  
LYX ,the

output being a pdf.?


I've never used Lyx, but I've written instructions for several  
different editors (Winedt, TeXWorks, TeXShop) and you might find

those helpful if you need to do it yourself.  See

http://www.umanitoba.ca/statistics/seminars/2011/3/4/duncan-murdoch-using-sweave-R/



Thank you for that Duncan;

I thought the question was overly broad and not containing needed  
information, but it is having welcome side-effects. Your tutorial on  
TexShop integration with Sweave looks very promising. I'm a LaTeX  
noob, but on my Mac it seems to be the most "welcoming" of the many  
LaTeX editors available.



--
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] save output of loop

2012-02-14 Thread jim holtman
try this:

file_s <- list.files(path = ".", pattern = "v2.0.2.txt", all.files = FALSE,
 full.names = FALSE, recursive = FALSE,
 ignore.case = FALSE)
result <- do.call(rbind, lapply(file_s, function(.file){
data <- read.table(.file, header=TRUE)
data.frame(lat  = data[,7] # latitude
, lon = data[,8] # longitude
, gas  = data[,45] # gas
, time.s   = data[,5] # time
, stringsAsFactors = FALSE
)
}))



On Tue, Feb 14, 2012 at 5:07 AM, uday  wrote:
> I have some data files e.g 100 . and after for loop I would like to save all
> data in one single data frame
>
> file_s <- list.files(path = ".", pattern = "v2.0.2.txt", all.files = FALSE,
>                  full.names = FALSE, recursive = FALSE,
>                  ignore.case = FALSE)
> for (i in 1:100){
>  data     = read.table(file_s[i],header=TRUE)
>  lat  = data[,7] # latitude
>  lon = data[,8] # longitude
>  gas  = data[,45] # gas
>  time.s   = data[,5] # time
> }
>
> How I should get all these 100 files variable in to single data frame ?
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/save-output-of-loop-tp4386599p4386599.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
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] Wildcard for indexing?

2012-02-14 Thread Sarah Goslee
Hi,

On Tue, Feb 14, 2012 at 9:54 AM, Johannes Radinger  wrote:
> Hi,
>
> I'd like to know if it is possible to use wildcards * for indexing...
> E.g. I have a vector of strings. Now I'd like to select all elements
> which start with A_*? I'd also need to combine that with logical operators:
>
> "Select all elements of a vector that start with A (A*) OR that start with B 
> (B*)"
>
> Probably that is quite easy. I looked into grep() which I think might perform 
> such tasks, but probably there is a more straigth forward solution.
>
> a <- c("A_A","A_B","C_A","BB","A_Asd")
> a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard

Do you want elements that start with A or B, as you state above, or elements
that start with A_A or A_B as here?

Either way, this is a job for grepl(), and it is quite easy:

> a <- c("A_A","A_B","C_A","BB","A_Asd")
>
> grepl("^[AB]", a)
[1]  TRUE  TRUE FALSE  TRUE  TRUE
> grepl("^A_[AB]", a)
[1]  TRUE  TRUE FALSE FALSE  TRUE

Sarah


-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] Wildcard for indexing?

2012-02-14 Thread R. Michael Weylandt
I think the grep()-family (regular expressions) will be the easiest
way to do this, though it sounds like you might prefer grepl() which
returns a logical vector:

^[AB] # Starts with either an A or a B
^A_ # Starting with A_

a <-  c("A_A","A_B","C_A","BB","A_Asd"
grepl("^[AB]", a)
grepl("^A_")

Michael

On Tue, Feb 14, 2012 at 9:54 AM, Johannes Radinger  wrote:
> Hi,
>
> I'd like to know if it is possible to use wildcards * for indexing...
> E.g. I have a vector of strings. Now I'd like to select all elements
> which start with A_*? I'd also need to combine that with logical operators:
>
> "Select all elements of a vector that start with A (A*) OR that start with B 
> (B*)"
>
> Probably that is quite easy. I looked into grep() which I think might perform 
> such tasks, but probably there is a more straigth forward solution.
>
> a <- c("A_A","A_B","C_A","BB","A_Asd")
> a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard
>
> /johannes
> --
>
> __
> 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] Wildcard for indexing?

2012-02-14 Thread Duncan Murdoch

On 14/02/2012 9:54 AM, Johannes Radinger wrote:

Hi,

I'd like to know if it is possible to use wildcards * for indexing...
E.g. I have a vector of strings. Now I'd like to select all elements
which start with A_*? I'd also need to combine that with logical operators:

"Select all elements of a vector that start with A (A*) OR that start with B 
(B*)"

Probably that is quite easy. I looked into grep() which I think might perform 
such tasks, but probably there is a more straigth forward solution.

a<- c("A_A","A_B","C_A","BB","A_Asd")
a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard


Try grepl():

a[grepl("^[AB]", a)]

is probably the simplest way for your example.

Duncan Murdoch

__
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] Selecting elements from all items in a list

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 8:44 AM, geotheory wrote:

Basic question (if you know the answer)...  I am dealing with a list  
of

commonly-formatted sub-lists, for example:

l <- list("")
l[[1]] <- c("A1","A2","A3")
l[[2]] <- c("B1","B2","B3")
l[[3]] <- c("C1","B2","B3")

Lets say I need to extract every 2nd item (i.e. A2, B2, C2).  [[]]  
cannot be
used.   l[2] returns practically the same as l[[2]].  Is there a way  
to

achieve this without having to loop or convert to data.frame?


Any such extraction would of necessity have a loop-like basis, but  
perhaps you meant without an explicit for-loop?


> sapply(l, "[[", 2)
[1] "A2" "B2" "B2"


--

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.


[R] Wildcard for indexing?

2012-02-14 Thread Johannes Radinger
Hi,

I'd like to know if it is possible to use wildcards * for indexing...
E.g. I have a vector of strings. Now I'd like to select all elements
which start with A_*? I'd also need to combine that with logical operators:

"Select all elements of a vector that start with A (A*) OR that start with B 
(B*)"

Probably that is quite easy. I looked into grep() which I think might perform 
such tasks, but probably there is a more straigth forward solution.

a <- c("A_A","A_B","C_A","BB","A_Asd")
a[a=="A_A"| a=="A_B"] # here I'd like an index but with wildcard

/johannes
--

__
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] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread Rainer M Krug
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On 14/02/12 13:41, ATANU wrote:
> I am using R 2.14.1. I am trying to configure Lyx with Sweave. I 
> have read articles but I found none to be complete  and I cannot 
> import sweave document in lyx . Can anyone please help me with a 
> stepwise procedure how to configure Lyx with Sweave so that I can 
> run my R-code chunks from LYX ,the output being a pdf.?


I would really recommend to ask this question on the lyx mailing list
(http://www.lyx.org/MailingLists)

See you there,

Rainer


> Thanks in advance. -Atanu
> 
> -- View this message in context: 
> http://r.789695.n4.nabble.com/configure-lyx2-0-2-with-sweave-in-windows-32-bit-tp4386871p4386871.html
>
>
> 
Sent from the R help mailing list archive at Nabble.com.
> 
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the
> posting guide http://www.R-project.org/posting-guide.html and
> provide commented, minimal, self-contained, reproducible code.


- -- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :   +33 - (0)9 53 10 27 44
Cell:   +33 - (0)6 85 62 59 98
Fax :   +33 - (0)9 58 10 27 44

Fax (D):+49 - (0)3 21 21 25 22 44

email:  rai...@krugs.de

Skype:  RMkrug
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk86dPYACgkQoYgNqgF2egrOpwCfeGsBWH9YjB7vlUyP+A9JFwMz
WHsAnAgskN0WvzVIUTWxgZapdOStS/ap
=aWJ5
-END PGP SIGNATURE-

__
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] Selecting elements from all items in a list

2012-02-14 Thread Dimitris Rizopoulos

How about:

l <- list("")
l[[1]] <- c("A1","A2","A3")
l[[2]] <- c("B1","B2","B3")
l[[3]] <- c("C1","B2","B3")


lapply(l, "[[", 2)

or

sapply(l, "[[", 2)


I hope it helps.

Best,
Dimitris


On 2/14/2012 2:44 PM, geotheory wrote:

Basic question (if you know the answer)...  I am dealing with a list of
commonly-formatted sub-lists, for example:

l<- list("")
l[[1]]<- c("A1","A2","A3")
l[[2]]<- c("B1","B2","B3")
l[[3]]<- c("C1","B2","B3")

Lets say I need to extract every 2nd item (i.e. A2, B2, C2).  [[]] cannot be
used.   l[2] returns practically the same as l[[2]].  Is there a way to
achieve this without having to loop or convert to data.frame?

Thanks in advance.

--
View this message in context: 
http://r.789695.n4.nabble.com/Selecting-elements-from-all-items-in-a-list-tp4387045p4387045.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/

__
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] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread Duncan Murdoch

On 14/02/2012 7:41 AM, ATANU wrote:

I am using R 2.14.1. I am trying to configure Lyx with Sweave. I have read
articles but I found none to be complete  and I cannot import sweave
document in lyx . Can anyone please help me with a stepwise procedure how to
configure Lyx with Sweave so that I can run my R-code chunks from LYX ,the
output being a pdf.?


I've never used Lyx, but I've written instructions for several different 
editors (Winedt, TeXWorks, TeXShop) and you might find

those helpful if you need to do it yourself.  See

http://www.umanitoba.ca/statistics/seminars/2011/3/4/duncan-murdoch-using-sweave-R/

for the most recent, or

http://www.stats.uwo.ca/faculty/murdoch/computing

for some older ones.

__
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] save objects of own function to workspace

2012-02-14 Thread David Winsemius


On Feb 14, 2012, at 9:20 AM, Marion Wenty wrote:


Dear R-helpers,

I created an own function which looks like this

s_elternmz <- function(Var="balt")



{
 Dg_a<-mz[,c("asbhh","apkz",Var)]
 colnames(Dg_a)[colnames(Dg_a)=="apkz"]<-"bpkzm"
 colnames(Dg_a)[colnames(Dg_a)==Var]<-paste(Var,"_m",sep="")
 mz_int<-merge(mz,Dg_a,by=c("asbhh","bpkzm"),all.x=T)
 colnames(Dg_a)[colnames(Dg_a)=="bpkzm"]<-"bpkzv"

colnames(Dg_a)[colnames(Dg_a)==paste(Var,"_m",sep="")]<- 
paste(Var,"_v",sep="")


Why take two steps to change that column name?


 mz_int<-merge(mz_int,Dg_a,by=c("asbhh","bpkzv"),all.x=T)


You just overwrote an object that you created earklier but have not  
done anything with in the meantime.



 mz_int <-
mz_int[order(mz_int$asbhh,mz_int$apkz),  
c(colnames(mz),paste(Var,"_m",sep=""), paste(Var,"_v",sep=""))]

}

My problem is that the objects are not saved in the workspace.  
Especially I
need the object which I created in the end - mz_int - because I want  
to do

calculations with it in R afterwards.


Then just assign it:

mz_result <- s_elternmz()

As you have noticed, assignment inside a function is not a durable  
event. The function returns the value of either its first encounter  
with return() or the value
of hte last assignment (but not the name to which that object was  
assigned.) Welcome to R's version of functional programming.




Thank you very much for your help in advance.

Marion

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


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] Two surfaces in one plot with visibility

2012-02-14 Thread Duncan Murdoch

On 14/02/2012 9:19 AM, Michael Friendly wrote:

On 2/13/2012 11:15 AM, Duncan Murdoch wrote:
>  On 13/02/2012 9:24 AM, Sebastian Schubert wrote:
>>  Hi,
>>
>>  I would like plot two surfaces which are each given by vectors x and y,
>>  and a matrix m(x,y) representing the z coordinate. With persp() I can
>>  plot both, using par(new=TRUE) I can put it in one plot. However, I
>>  would like to have the visibility of the surfaces taken into account as
>>  if they are solid thin surfaces, so that for example the order of the
>>  plot commands does not matter.
>>
>>  Any idea how to do that?
>
>  That's really hard in persp(). You'd have to plot the facets of the
>  surfaces from back to front, and there's no easy way to do that.
>  I'd recommend using rgl::persp3d, where your graphics hardware will do
>  the computations of which surface is in front.
>
>  Duncan Murdoch
>

In addition to using rgl, you might find it useful to use transparent
colors for one or both surfaces.


That often makes a nicer looking plot, but if the surfaces intersect, it 
won't look very nice along the boundary if both are transparent, because 
of limitations in the algorithm rgl uses.  It should be fine if they 
don't intersect, or only one is transparent.


Duncan Murdoch

__
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] Selecting elements from all items in a list

2012-02-14 Thread geotheory
Basic question (if you know the answer)...  I am dealing with a list of
commonly-formatted sub-lists, for example:

l <- list("")
l[[1]] <- c("A1","A2","A3")
l[[2]] <- c("B1","B2","B3")
l[[3]] <- c("C1","B2","B3")

Lets say I need to extract every 2nd item (i.e. A2, B2, C2).  [[]] cannot be
used.   l[2] returns practically the same as l[[2]].  Is there a way to
achieve this without having to loop or convert to data.frame?

Thanks in advance.

--
View this message in context: 
http://r.789695.n4.nabble.com/Selecting-elements-from-all-items-in-a-list-tp4387045p4387045.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] configure lyx2.0.2 with sweave in windows 32 bit.

2012-02-14 Thread ATANU
I am using R 2.14.1. I am trying to configure Lyx with Sweave. I have read
articles but I found none to be complete  and I cannot import sweave
document in lyx . Can anyone please help me with a stepwise procedure how to
configure Lyx with Sweave so that I can run my R-code chunks from LYX ,the
output being a pdf.?
Thanks in advance.
-Atanu

--
View this message in context: 
http://r.789695.n4.nabble.com/configure-lyx2-0-2-with-sweave-in-windows-32-bit-tp4386871p4386871.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


  1   2   >