Re: [R] Multiple if function

2015-09-16 Thread peter dalgaard
I think a more common idiom for the simpler case would be to use indexing

vals <- c(0.1, 0.15, 0.2) 
mult <- vals[ASBclass]

(However, some people are on the move to enforce 

mult <- vals[as.numeric(ASBclass)]

because people who confuse factors and character variables get even more 
confused about factor indexing being different from character indexing.)

For the more complex cases, I think Chuck's split/unsplit principle is the 
ticket. For one thing, you avoid silliness like (X >= 0) * sqrt(X) + (X < 0) * 
-sqrt(-X) coming out with warnings from calculating the non-selected 
alternative.

-pd

> On 16 Sep 2015, at 07:56 , Anthoni, Peter (IMK)  wrote:
> 
> Hi,
> 
> I guess this might work too and might be quite speedy:
> 
> ASBclass = factor(c(1,2,2,3,2,1))
> Flow = c(1,1,1,1,1,1)
> 
> mult = ((ASBclass==1) * 0.1 + (ASBclass==2) * 0.15 + (ASBclass==3) * 0.2)
> deviation = mult * Flow
> 
> or with the more complex arithmetic:
> 
> deviation = ((ASBclass==1) * (Flow*2) + (ASBclass==2) * (Flow+3) + 
> (ASBclass==3) * sqrt(Flow))
> 
> cheers
> Peter
> 
> 
> 
>> On 16 Sep 2015, at 04:20, Charles C. Berry  wrote:
>> 
>> On Tue, 15 Sep 2015, Bert Gunter wrote:
>> 
>>> Thanks to both Davids.
>>> 
>>> I realize that these things are often a matter of aesthetics -- and
>>> hence have little rational justification -- but I agree with The Other
>>> David: eval(parse) seems to me to violate R's soul( it makes R a macro
>>> language instead of a functional one).
>>> 
>>> However, mapply(... switch) effectively loops through the frame row by
>>> row. Aesthetically, I like it; but it seems inefficient. If there are
>>> e.g. 1e6 rows in say 10 categories, I think Jeff's approach should do
>>> much better.  I'll try to generate some actual data to see unless
>>> someone else beats me to it.
>> 
>> Use mapply like this on large problems:
>> 
>> unsplit(
>>   mapply(
>>   function(x,z) eval( x, list( y=z )),
>>   expression( A=y*2, B=y+3, C=sqrt(y) ),
>>   split( dat$Flow, dat$ASB ),
>>   SIMPLIFY=FALSE),
>>   dat$ASB)
>> 
>> Chuck
>> 
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

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


Re: [R] help with old libraries

2015-09-16 Thread Uwe Ligges



On 15.09.2015 11:54, Pau Marc Muñoz Torres wrote:

Hello everybody,

  I want to use Rapidr package, it is an old package that uses the
package requires GenomicRanges version 1.14.4. The current version of the
package is GenomicRanges 1.20.6. There is some way of having both the
actual and the previous packages installed? I tried to install the package
locally, but I had problems with dependences, how can I deal with it? Would
be possible have both versions installed and choose which one to use?



Install the otehr version in a separate library and specify in your call 
to require() or library() which library to use.


Best,
Uwe Ligges




Thanks in advance

Pau Marc Muñoz Torres
skype: pau_marc
http://www.linkedin.com/in/paumarc
http://www.researchgate.net/profile/Pau_Marc_Torres3/info/

[[alternative HTML version deleted]]

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



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

Re: [R] Drop in a Loop

2015-09-16 Thread Will Hopper
The data structure returned by optimx (that you're storing as gmmiv) is
like a data frame, and stores the conversion code in the field  convcode.
So do something like this:

gmmiv =Optimx()
if (gmmiv$convcode ==0) {
  store[j,] = coef(gmmiv)
}

On Tue, Sep 15, 2015 at 3:12 PM, Olu Ola  wrote:

> Thanks Will.
>  Below is the flow of my code
>
> Yhat is the fitted value
> Errhat is the difference between the dependent variable and the yhat
> gmmdata is the data name
>
> N <- nrow(gmmdata)
> B <- 1000
> store <- matrix(0,B,11)
> for (j in 1:B) {
>   index = sample(1:N, N, replace=T)
>   errnew = errhat[index]
>   yt = yhat + errnew
> objective function subroutine
> gradient function subroutine
> gmmiv =Optimx()
>   store[j,] = coef(gmmiv)
> }
>
> What I want to do is that if the convergence code from optimx for a
> particular iteration is Not zero, then it should not be stored in store[j,].
>
> Any help will be appreciated
>
> Thank you
>
>
>
>
>
>
> 
> On Tue, 9/15/15, Will Hopper  wrote:
>
>  Subject: Re: [R] Drop in a Loop
>  To: "Olu Ola" 
>  Cc: r-help@r-project.org
>  Date: Tuesday, September 15, 2015, 2:30 PM
>
>  I
>  think you ought to show a small example of how the code
>  you're using. Are you saving results at every iteration?
>  In a list, data frame, etc? People likely need that to help
>  answer your question.
>
>   Also probably have a look the control list
>  argument and the save.failures option, that might be
>  something you're interested in.
>
>  - Will
>
>  On Tue, Sep 15, 2015 at
>  1:34 PM, Olu Ola via R-help 
>  wrote:
>  Hello,
>
>  I am doing some estimation using optimx and after each round
>  of estimation, I store the coefficient. However, I need to
>  drop the set of coefficients for which the convergence code
>  in optimx is GREATER than Zero. How do I go about this?
>
>
>
>  A way forward will be highly appreciated.
>
>
>
>  Thank you
>
>
>
>  __
>
>  R-help@r-project.org
>  mailing list -- To UNSUBSCRIBE and more, see
>
>  https://stat.ethz.ch/mailman/listinfo/r-help
>
>  PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>
>  and provide commented, minimal, self-contained, reproducible
>  code.
>
>
>
>

[[alternative HTML version deleted]]

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


Re: [R] Multiple if function

2015-09-16 Thread Bert Gunter
Yes! Chuck's use of mapply is exactly the split/combine strategy I was
looking for. In retrospect, exactly how one should think about it.
Many thanks to all for a constructive discussion .

-- Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 12:40 AM, peter dalgaard  wrote:
> I think a more common idiom for the simpler case would be to use indexing
>
> vals <- c(0.1, 0.15, 0.2)
> mult <- vals[ASBclass]
>
> (However, some people are on the move to enforce
>
> mult <- vals[as.numeric(ASBclass)]
>
> because people who confuse factors and character variables get even more 
> confused about factor indexing being different from character indexing.)
>
> For the more complex cases, I think Chuck's split/unsplit principle is the 
> ticket. For one thing, you avoid silliness like (X >= 0) * sqrt(X) + (X < 0) 
> * -sqrt(-X) coming out with warnings from calculating the non-selected 
> alternative.
>
> -pd
>
>> On 16 Sep 2015, at 07:56 , Anthoni, Peter (IMK)  
>> wrote:
>>
>> Hi,
>>
>> I guess this might work too and might be quite speedy:
>>
>> ASBclass = factor(c(1,2,2,3,2,1))
>> Flow = c(1,1,1,1,1,1)
>>
>> mult = ((ASBclass==1) * 0.1 + (ASBclass==2) * 0.15 + (ASBclass==3) * 0.2)
>> deviation = mult * Flow
>>
>> or with the more complex arithmetic:
>>
>> deviation = ((ASBclass==1) * (Flow*2) + (ASBclass==2) * (Flow+3) + 
>> (ASBclass==3) * sqrt(Flow))
>>
>> cheers
>> Peter
>>
>>
>>
>>> On 16 Sep 2015, at 04:20, Charles C. Berry  wrote:
>>>
>>> On Tue, 15 Sep 2015, Bert Gunter wrote:
>>>
 Thanks to both Davids.

 I realize that these things are often a matter of aesthetics -- and
 hence have little rational justification -- but I agree with The Other
 David: eval(parse) seems to me to violate R's soul( it makes R a macro
 language instead of a functional one).

 However, mapply(... switch) effectively loops through the frame row by
 row. Aesthetically, I like it; but it seems inefficient. If there are
 e.g. 1e6 rows in say 10 categories, I think Jeff's approach should do
 much better.  I'll try to generate some actual data to see unless
 someone else beats me to it.
>>>
>>> Use mapply like this on large problems:
>>>
>>> unsplit(
>>>   mapply(
>>>   function(x,z) eval( x, list( y=z )),
>>>   expression( A=y*2, B=y+3, C=sqrt(y) ),
>>>   split( dat$Flow, dat$ASB ),
>>>   SIMPLIFY=FALSE),
>>>   dat$ASB)
>>>
>>> Chuck
>>>
>>> __
>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: pd@cbs.dk  Priv: pda...@gmail.com
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


[R] setTxtProgressBar clearing console (Windows RGui)

2015-09-16 Thread Jon Skoien
I have frequently noticed a strange effect when using the progress bar 
in a loop, that the console will sometimes be cleared. Whatever was 
printed before will then be inaccessible, which can be annoying when I 
want to check the progress and possible issues in a long script. It 
seems to be an intermittent problem, so it could also be related to my 
computer or other software running. I have had this issue in several R 
versions, and managed to provoke it in 3.2.2 with the following:


testit <- function(x = 100, ...)
{
pb <- txtProgressBar(0, x, ...)
for(i in 0:x) {Sys.sleep(0.1); setTxtProgressBar(pb, i)}
Sys.sleep(4)
close(pb)
print(1:x)
print(1:x)
print(1:x)
print(length(x))
}

for (i in 1:1000) {
  testit(style = 3)
}


The printing is just to fill up the console quicker, as the clearing 
seems to occur more frequent then. However, the loop above can sometimes 
call testit 5-30 times before the console is cleared again. In my real 
script the console is usually cleared more frequent.
With x = 10 above, it seems not to clear the console, at least not for a 
long time.


Does anyone have a clue why this happens, or if something can be done to 
prevent it? Is it reproducible, or something that only bothers me?


Thanks,
Jon

BTW:
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

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







--
Jon Olav Skøien
Joint Research Centre - European Commission
Institute for Environment and Sustainability (IES)
Climate Risk Management Unit

Via Fermi 2749, TP 100-01,  I-21027 Ispra (VA), ITALY

jon.sko...@jrc.ec.europa.eu
Tel:  +39 0332 789205

Disclaimer: Views expressed in this email are those of the individual 
and do not necessarily represent official views of the European Commission.


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

Re: [R] by Function Result Factor Levels

2015-09-16 Thread Dario Strbenac
Good day,

Yes, exactly. I found that aggregate is another alternative which doesn't 
require a package dependency, although the column formatting is less suitable, 
always prepending x.

aggregate(warpbreaks[, 1], warpbreaks[, 2:3], function(breaks) c(Min = 
min(breaks), Med = median(breaks), Max = max(breaks)))
  wool tension x.Min x.Med x.Max
1A   L255170
2B   L142944
3A   M122136
4B   M162842
5A   H102443
6B   H131728

--
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] mtext in the top left of margin

2015-09-16 Thread Hermann Norpois
Hello,

for a multiple figures plot I am looking for the syntax to put text in the
top left of the margin (of the plot). I want my testfunction plot.figure to
place mtext in the top left of the red margin (created by box("figure",
col="red")).

Can anybody help?

Thanks
Hermann

plot.figure <- function ()
{


par (mfrow=c(3,1))
par (mar=c(3,3,1,0.5))
par (oma=c(1,1,1,1))
par (mar=c(3,5,3,2))

plot (dnorm, from=-4, to=4, main="Test")
box ("plot", col="grey")
box ("figure", col="red")
box ("outer", col="blue")
mtext ("A", side=3, adj=0, line=1.5)

plot (dnorm, from=-4, to=4)
box ("plot", col="grey")
box ("figure", col="red")
box ("outer", col="blue")
 mtext ("B", side=3, adj=0, line=1)
plot (dnorm, from=-4, to=4)
box ("plot", col="grey")
box ("figure", col="red")
box ("outer", col="blue")
mtext ("C", side=3, adj=0)
}

[[alternative HTML version deleted]]

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


[R] generate ordered categorical variable in R

2015-09-16 Thread thanoon younis
Dear R- users

I want to generate ordered categorical variable vector with 200x1 dimension
and from 1 to 4 categories and i tried with this code

Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
decimals like 1.244, 2.342,4,321 and so on ... My question how can i
generate a vector and also a matrix with orered categorical variables and
without decimals just 1,2,3 ,4 ,1,2,3,4, 

Many thanks in advance





-- 
Thanoon Y. Thanoon
PhD Candidate
Department of Mathematical Sciences
Faculty of Science
University Technology Malaysia, UTM
E.Mail: thanoon.youni...@gmail.com
E.Mail: dawn_praye...@yahoo.com
Facebook:Thanoon Younis AL-Shakerchy
Twitter: Thanoon Alshakerchy
H.P:00601127550205

[[alternative HTML version deleted]]

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Michael Dewey

If I understand correctly

?sample


On 16/09/2015 18:11, thanoon younis wrote:

Dear R- users

I want to generate ordered categorical variable vector with 200x1 dimension
and from 1 to 4 categories and i tried with this code

Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
decimals like 1.244, 2.342,4,321 and so on ... My question how can i
generate a vector and also a matrix with orered categorical variables and
without decimals just 1,2,3 ,4 ,1,2,3,4, 

Many thanks in advance







--
Michael
http://www.dewey.myzen.co.uk/home.html

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


Re: [R] by Function Result Factor Levels

2015-09-16 Thread David L Carlson
Actually x is the variable name since your function returned a vector of three 
values: 

> tbl <- aggregate(warpbreaks[, 1], warpbreaks[, 2:3], function(breaks) c(Min = 
> min(breaks),
+  Med = median(breaks), Max = max(breaks)))
> str(tbl)
'data.frame':   6 obs. of  3 variables:
 $ wool   : Factor w/ 2 levels "A","B": 1 2 1 2 1 2
 $ tension: Factor w/ 3 levels "L","M","H": 1 1 2 2 3 3
 $ x  : num [1:6, 1:3] 25 14 12 16 10 13 51 29 21 28 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "Min" "Med" "Max"

You have two options. One is to convert the matrix to three separate columns:

> tbl2 <- data.frame(tbl[, 1:2], tbl$x)
> str(tbl2)
'data.frame':   6 obs. of  5 variables:
 $ wool   : Factor w/ 2 levels "A","B": 1 2 1 2 1 2
 $ tension: Factor w/ 3 levels "L","M","H": 1 1 2 2 3 3
 $ Min: num  25 14 12 16 10 13
 $ Med: num  51 29 21 28 24 17
 $ Max: num  70 44 36 42 43 28
> tbl2
  wool tension Min Med Max
1A   L  25  51  70
2B   L  14  29  44
3A   M  12  21  36
4B   M  16  28  42
5A   H  10  24  43
6B   H  13  17  28

The other is to change the name of x to something more informative:

> names(tbl)[3] <- "breaks"
> str(tbl)
'data.frame':   6 obs. of  3 variables:
 $ wool   : Factor w/ 2 levels "A","B": 1 2 1 2 1 2
 $ tension: Factor w/ 3 levels "L","M","H": 1 1 2 2 3 3
 $ breaks : num [1:6, 1:3] 25 14 12 16 10 13 51 29 21 28 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "Min" "Med" "Max"
> tbl
  wool tension breaks.Min breaks.Med breaks.Max
1A   L 25 51 70
2B   L 14 29 44
3A   M 12 21 36
4B   M 16 28 42
5A   H 10 24 43
6B   H 13 17 28

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dario Strbenac
Sent: Wednesday, September 16, 2015 1:00 AM
To: William Dunlap
Cc: r-help@R-project.org
Subject: Re: [R] by Function Result Factor Levels

Good day,

Yes, exactly. I found that aggregate is another alternative which doesn't 
require a package dependency, although the column formatting is less suitable, 
always prepending x.

aggregate(warpbreaks[, 1], warpbreaks[, 2:3], function(breaks) c(Min = 
min(breaks), Med = median(breaks), Max = max(breaks)))
  wool tension x.Min x.Med x.Max
1A   L255170
2B   L142944
3A   M122136
4B   M162842
5A   H102443
6B   H131728

--
Dario Strbenac
PhD Student
University of Sydney
Camperdown NSW 2050
Australia
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Rui Barradas

Hello,

Try ?sample.

Hope this helps,

Rui Barradas

Em 16-09-2015 18:11, thanoon younis escreveu:

Dear R- users

I want to generate ordered categorical variable vector with 200x1 dimension
and from 1 to 4 categories and i tried with this code

Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
decimals like 1.244, 2.342,4,321 and so on ... My question how can i
generate a vector and also a matrix with orered categorical variables and
without decimals just 1,2,3 ,4 ,1,2,3,4, 

Many thanks in advance







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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread David L Carlson
There is a version of sample especially for integers:

> Q1 <- matrix(sample.int(4, 200, replace=TRUE), 200)
> str(Q1)
 int [1:200, 1] 1 4 4 2 3 3 4 4 2 3 ...

-
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of thanoon younis
Sent: Wednesday, September 16, 2015 12:11 PM
To: r-help@r-project.org
Subject: [R] generate ordered categorical variable in R

Dear R- users

I want to generate ordered categorical variable vector with 200x1 dimension
and from 1 to 4 categories and i tried with this code

Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
decimals like 1.244, 2.342,4,321 and so on ... My question how can i
generate a vector and also a matrix with orered categorical variables and
without decimals just 1,2,3 ,4 ,1,2,3,4, 

Many thanks in advance





-- 
Thanoon Y. Thanoon
PhD Candidate
Department of Mathematical Sciences
Faculty of Science
University Technology Malaysia, UTM
E.Mail: thanoon.youni...@gmail.com
E.Mail: dawn_praye...@yahoo.com
Facebook:Thanoon Younis AL-Shakerchy
Twitter: Thanoon Alshakerchy
H.P:00601127550205

[[alternative HTML version deleted]]

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

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Marc Schwartz

> On Sep 16, 2015, at 12:11 PM, thanoon younis  
> wrote:
> 
> Dear R- users
> 
> I want to generate ordered categorical variable vector with 200x1 dimension
> and from 1 to 4 categories and i tried with this code
> 
> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
> generate a vector and also a matrix with orered categorical variables and
> without decimals just 1,2,3 ,4 ,1,2,3,4, 
> 
> Many thanks in advance


You are sampling from a uniform distribution on the continuous interval from 1 
to 4.

See ?sample

set.seed(1)

> sample(4, 200, replace = TRUE)
  [1] 2 2 3 4 1 4 4 3 3 1 1 1 3 2 4 2 3 4 2 4 4 1 3 1 2 2 1 2 4 2 2 3 2
 [34] 1 4 3 4 1 3 2 4 3 4 3 3 4 1 2 3 3 2 4 2 1 1 1 2 3 3 2 4 2 2 2 3 2
 [67] 2 4 1 4 2 4 2 2 2 4 4 2 4 4 2 3 2 2 4 1 3 1 1 1 1 1 3 4 4 4 2 2 4
[100] 3 3 2 2 4 3 1 1 2 4 3 4 3 2 2 1 1 3 1 2 3 4 2 2 1 4 2 3 1 1 3 3 1
[133] 1 3 4 3 3 3 4 3 3 3 1 2 3 2 1 3 1 4 3 3 2 2 3 1 3 1 2 1 2 4 2 4 4
[166] 2 1 2 3 2 3 4 4 2 2 4 3 3 3 4 2 1 4 3 4 1 4 3 4 3 3 2 1 4 2 3 1 4
[199] 2 4

Here, the result is a vector of integers.


set.seed(1)

> sample(factor(1:4), 200, replace = TRUE)
  [1] 2 2 3 4 1 4 4 3 3 1 1 1 3 2 4 2 3 4 2 4 4 1 3 1 2 2 1 2 4 2 2 3 2
 [34] 1 4 3 4 1 3 2 4 3 4 3 3 4 1 2 3 3 2 4 2 1 1 1 2 3 3 2 4 2 2 2 3 2
 [67] 2 4 1 4 2 4 2 2 2 4 4 2 4 4 2 3 2 2 4 1 3 1 1 1 1 1 3 4 4 4 2 2 4
[100] 3 3 2 2 4 3 1 1 2 4 3 4 3 2 2 1 1 3 1 2 3 4 2 2 1 4 2 3 1 1 3 3 1
[133] 1 3 4 3 3 3 4 3 3 3 1 2 3 2 1 3 1 4 3 3 2 2 3 1 3 1 2 1 2 4 2 4 4
[166] 2 1 2 3 2 3 4 4 2 2 4 3 3 3 4 2 1 4 3 4 1 4 3 4 3 3 2 1 4 2 3 1 4
[199] 2 4
Levels: 1 2 3 4

Here, the result is a factor.


set.seed(1)

> sample(factor(c("A", "B", "C", "D")), 200, replace = TRUE)
  [1] B B C D A D D C C A A A C B D B C D B D D A C A B B A B D B B C B
 [34] A D C D A C B D C D C C D A B C C B D B A A A B C C B D B B B C B
 [67] B D A D B D B B B D D B D D B C B B D A C A A A A A C D D D B B D
[100] C C B B D C A A B D C D C B B A A C A B C D B B A D B C A A C C A
[133] A C D C C C D C C C A B C B A C A D C C B B C A C A B A B D B D D
[166] B A B C B C D D B B D C C C D B A D C D A D C D C C B A D B C A D
[199] B D
Levels: A B C D

Here, the result is a factor.


Just depends upon what you want for the categorial variable.

Regards,

Marc Schwartz

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Bert Gunter
Yikes! The uniform distribution is a **continuous** distribution over
an interval. You seem to want to sample over a discrete distribution.
See ?sample for that, as in:

sample(1:4,100,rep=TRUE)

## or for this special case and faster

sample.int(4,size=100,rep=TRUE)

Cheers,
Bert

Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
 wrote:
> Dear R- users
>
> I want to generate ordered categorical variable vector with 200x1 dimension
> and from 1 to 4 categories and i tried with this code
>
> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
> generate a vector and also a matrix with orered categorical variables and
> without decimals just 1,2,3 ,4 ,1,2,3,4, 
>
> Many thanks in advance
>
>
>
>
>
> --
> Thanoon Y. Thanoon
> PhD Candidate
> Department of Mathematical Sciences
> Faculty of Science
> University Technology Malaysia, UTM
> E.Mail: thanoon.youni...@gmail.com
> E.Mail: dawn_praye...@yahoo.com
> Facebook:Thanoon Younis AL-Shakerchy
> Twitter: Thanoon Alshakerchy
> H.P:00601127550205
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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


Re: [R] mtext in the top left of margin

2015-09-16 Thread Adams, Jean
You can use the coordinates of the plot region as fractions of the figure
region, par("plt"), to define the adj= argument of mtext().  And you can
use the number of lines of the plot margin to define the line= argument of
mtext().  For example:

plot.figure <- function() {
  par(mfrow=c(3, 1), mar=c(3, 5, 3, 2), oma=c(1, 1, 1, 1))
  # use the coords of the plot region as fractions of the figure region
  # to define the adj= argument of mtext()
  pplt <- par("plt")
  adjx <- (0 - pplt[1]) / (pplt[2] - pplt[1])
  # use the number of lines of margin to define the line= argument of
mtext()
  liney <- par("mar")[3] - 1.5
  plot(dnorm, from=-4, to=4, main="Test")
box("plot", col="grey")
box("figure", col="red")
mtext("A", side=3, adj=adjx, line=liney)
  plot(dnorm, from=-4, to=4)
box("plot", col="grey")
box("figure", col="red")
mtext("B", side=3, adj=adjx, line=liney)
  plot(dnorm, from=-4, to=4)
box("plot", col="grey")
box("figure", col="red")
mtext("C", side=3, adj=adjx, line=liney)
  box("outer", col="blue")
}
plot.figure()

Jean

On Wed, Sep 16, 2015 at 5:00 AM, Hermann Norpois  wrote:

> Hello,
>
> for a multiple figures plot I am looking for the syntax to put text in the
> top left of the margin (of the plot). I want my testfunction plot.figure to
> place mtext in the top left of the red margin (created by box("figure",
> col="red")).
>
> Can anybody help?
>
> Thanks
> Hermann
>
> plot.figure <- function ()
> {
>
>
> par (mfrow=c(3,1))
> par (mar=c(3,3,1,0.5))
> par (oma=c(1,1,1,1))
> par (mar=c(3,5,3,2))
>
> plot (dnorm, from=-4, to=4, main="Test")
> box ("plot", col="grey")
> box ("figure", col="red")
> box ("outer", col="blue")
> mtext ("A", side=3, adj=0, line=1.5)
>
> plot (dnorm, from=-4, to=4)
> box ("plot", col="grey")
> box ("figure", col="red")
> box ("outer", col="blue")
>  mtext ("B", side=3, adj=0, line=1)
> plot (dnorm, from=-4, to=4)
> box ("plot", col="grey")
> box ("figure", col="red")
> box ("outer", col="blue")
> mtext ("C", side=3, adj=0)
> }
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Marc Schwartz

> On Sep 16, 2015, at 1:06 PM, Bert Gunter  wrote:
> 
> Yikes! The uniform distribution is a **continuous** distribution over
> an interval. You seem to want to sample over a discrete distribution.
> See ?sample for that, as in:
> 
> sample(1:4,100,rep=TRUE)
> 
> ## or for this special case and faster
> 
> sample.int(4,size=100,rep=TRUE)


Bert,

I am not sure that it is really faster, since internally, sample() calls 
sample.int():

> sample
function (x, size, replace = FALSE, prob = NULL) 
{
if (length(x) == 1L && is.numeric(x) && x >= 1) {
if (missing(size)) 
size <- x
sample.int(x, size, replace, prob)
}
else {
if (missing(size)) 
size <- length(x)
x[sample.int(length(x), size, replace, prob)]
}
}


set.seed(1)

> system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
   user  system elapsed 
  2.755   0.170   2.925 


set.seed(1)
> system.time(x2 <- sample.int(1e10, 1e8, replace = TRUE))
   user  system elapsed 
  2.767   0.183   2.951 


> all(x1 == x2)
[1] TRUE


Regards,

Marc


> 
> Cheers,
> Bert
> 
> Bert Gunter
> 
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>   -- Clifford Stoll
> 
> 
> On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
>  wrote:
>> Dear R- users
>> 
>> I want to generate ordered categorical variable vector with 200x1 dimension
>> and from 1 to 4 categories and i tried with this code
>> 
>> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
>> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
>> generate a vector and also a matrix with orered categorical variables and
>> without decimals just 1,2,3 ,4 ,1,2,3,4, 
>> 
>> Many thanks in advance

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Bert Gunter
Yes. Thanks Marc. I stand corrected.

-- Bert
Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 1:28 PM, Marc Schwartz  wrote:
>
>> On Sep 16, 2015, at 1:06 PM, Bert Gunter  wrote:
>>
>> Yikes! The uniform distribution is a **continuous** distribution over
>> an interval. You seem to want to sample over a discrete distribution.
>> See ?sample for that, as in:
>>
>> sample(1:4,100,rep=TRUE)
>>
>> ## or for this special case and faster
>>
>> sample.int(4,size=100,rep=TRUE)
>
>
> Bert,
>
> I am not sure that it is really faster, since internally, sample() calls 
> sample.int():
>
>> sample
> function (x, size, replace = FALSE, prob = NULL)
> {
> if (length(x) == 1L && is.numeric(x) && x >= 1) {
> if (missing(size))
> size <- x
> sample.int(x, size, replace, prob)
> }
> else {
> if (missing(size))
> size <- length(x)
> x[sample.int(length(x), size, replace, prob)]
> }
> }
>
>
> set.seed(1)
>
>> system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
>user  system elapsed
>   2.755   0.170   2.925
>
>
> set.seed(1)
>> system.time(x2 <- sample.int(1e10, 1e8, replace = TRUE))
>user  system elapsed
>   2.767   0.183   2.951
>
>
>> all(x1 == x2)
> [1] TRUE
>
>
> Regards,
>
> Marc
>
>
>>
>> Cheers,
>> Bert
>>
>> Bert Gunter
>>
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>>   -- Clifford Stoll
>>
>>
>> On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
>>  wrote:
>>> Dear R- users
>>>
>>> I want to generate ordered categorical variable vector with 200x1 dimension
>>> and from 1 to 4 categories and i tried with this code
>>>
>>> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
>>> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
>>> generate a vector and also a matrix with orered categorical variables and
>>> without decimals just 1,2,3 ,4 ,1,2,3,4, 
>>>
>>> Many thanks in advance
>

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


Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Bert Gunter
Nope. Take it back. I stand uncorrected.

> system.time(z <-sample(1:10,1e6, rep=TRUE))
   user  system elapsed
  0.045   0.001   0.047

> system.time(z <-sample.int(10,1e6,rep=TRUE))
   user  system elapsed
  0.012   0.000   0.013


sample() has to do subscripting in the general case; sample.int doesn't.

But I would agree that the difference is likely almost always unnoticeable.


-- Bert
Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 1:34 PM, Bert Gunter  wrote:
> Yes. Thanks Marc. I stand corrected.
>
> -- Bert
> Bert Gunter
>
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>-- Clifford Stoll
>
>
> On Wed, Sep 16, 2015 at 1:28 PM, Marc Schwartz  wrote:
>>
>>> On Sep 16, 2015, at 1:06 PM, Bert Gunter  wrote:
>>>
>>> Yikes! The uniform distribution is a **continuous** distribution over
>>> an interval. You seem to want to sample over a discrete distribution.
>>> See ?sample for that, as in:
>>>
>>> sample(1:4,100,rep=TRUE)
>>>
>>> ## or for this special case and faster
>>>
>>> sample.int(4,size=100,rep=TRUE)
>>
>>
>> Bert,
>>
>> I am not sure that it is really faster, since internally, sample() calls 
>> sample.int():
>>
>>> sample
>> function (x, size, replace = FALSE, prob = NULL)
>> {
>> if (length(x) == 1L && is.numeric(x) && x >= 1) {
>> if (missing(size))
>> size <- x
>> sample.int(x, size, replace, prob)
>> }
>> else {
>> if (missing(size))
>> size <- length(x)
>> x[sample.int(length(x), size, replace, prob)]
>> }
>> }
>>
>>
>> set.seed(1)
>>
>>> system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
>>user  system elapsed
>>   2.755   0.170   2.925
>>
>>
>> set.seed(1)
>>> system.time(x2 <- sample.int(1e10, 1e8, replace = TRUE))
>>user  system elapsed
>>   2.767   0.183   2.951
>>
>>
>>> all(x1 == x2)
>> [1] TRUE
>>
>>
>> Regards,
>>
>> Marc
>>
>>
>>>
>>> Cheers,
>>> Bert
>>>
>>> Bert Gunter
>>>
>>> "Data is not information. Information is not knowledge. And knowledge
>>> is certainly not wisdom."
>>>   -- Clifford Stoll
>>>
>>>
>>> On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
>>>  wrote:
 Dear R- users

 I want to generate ordered categorical variable vector with 200x1 dimension
 and from 1 to 4 categories and i tried with this code

 Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
 decimals like 1.244, 2.342,4,321 and so on ... My question how can i
 generate a vector and also a matrix with orered categorical variables and
 without decimals just 1,2,3 ,4 ,1,2,3,4, 

 Many thanks in advance
>>

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


[R] aggregate counting variable factors

2015-09-16 Thread Kai Mx
Hi everybody,

>From a questionnaire, I have a dataset  like this one with some 40 items:

df1 <- data.frame(subject=c('user1','user2', 'user3', 'user4'),
item1=c(0,1,2,5), item2=c(1,2,1,2), item3=c(2,3,4,0), item4=c(0,3,3,2),
item5=c(5,5,5,5))

Users can choose an answer from 0 to 5 for each item.

Now I want to reshape the dataset to have the items in rows and the count
of each of the result factors in columns:

result <- data.frame (item=c("item1", "item2", "item3", "item4", "item5"),
result0=c(1,0,1,1,0), result1=c(1,2,0,0,0), result2=c(1,2,1,1,0),
result3=c(0,0,1,2,0), result4=c(0,0,1,0,0), result5=c(1,0,0,0,4))

I have been fiddling around with melt/plyr, but haven't been able to figure
it out. What's the most elegant way to do this (preferably without typing
in all the item names).

Thanks so much!

Best,

Kai

[[alternative HTML version deleted]]

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


[R] Get R-realisations for a 'custom' distribution

2015-09-16 Thread holograph via R-help
Hello all,

I've been busy figuring out how to get realisations for a non-standard
distribution in R. Define \theta=o (not 0). Consider  f_x(x)=o*x for o in
\sqrt{2/o}.

Yet, I can't seem to find a way to get realisations for custom
distributions. Can anyone help me out here? 



--
View this message in context: 
http://r.789695.n4.nabble.com/Get-R-realisations-for-a-custom-distribution-tp4712338.html
Sent from the R help mailing list archive at Nabble.com.

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


[R] Error Message when using VarSelection in YaiImpute package with the randomForest

2015-09-16 Thread andrew haywood
Dear All,

when using the following code

x <- iris[,1:2]  # Sepal.Length Sepal.Width
y <- iris[,3:4]  # Petal.Length Petal.Width
vsel <-
varSelection(x=x,y=y,nboot=5,yaiMethod="randomForest",useParallel=FALSE)


I get the following error code

Error in yai(x = xa, y = y, method = yaiMethod, bootstrap = bootstrap,  :
  object 'xcvRefs' not found


If anybody could tell me what I am doing wrong.

Cheers
Andrew

[[alternative HTML version deleted]]

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


[R] HELP IN GRAPHS - slip screen

2015-09-16 Thread Rosa Oliveira
Dear all,

I’m trying to do a graph, 

3 rows, 5 columns, with the design:
#   3   4   5   6 
#2
#   7   8   9   10 

I had a code for 3 rows, 3 columns, with the design::
#   3   4   
#2
#   7   8
 and I tried to modify it, but I had no success :(

I suppose the problem is in the slip.screen code (red part of the code).

I attach my code, can anyone please help me?


Best,
RO


setwd("/Users/RO/Dropbox/LMER - 3rdproblem/R/latest_version/graphs/data")

library(ggplot2)
library(reshape)
library(lattice)


# read in what looks like half of the data

bias.alpha2<-read.csv("graphs_bias_alpha2.csv")
SE.alpha2<-read.csv("graphs_SE_alpha2.csv")
bias.alpha1<-read.csv("graphs_bias_alpha1.csv")
SE.alpha1<-read.csv("graphs_SE_alpha1.csv")



quartz(width=10,height=6)
# do the first split, to get the rightmost screen for the legend
split.screen(figs=matrix(c(0,0.8,0,1,0.8,1,0,1),nrow=2,byrow=TRUE))
# now split the first screen to get your six screens for the plots



split.screen(figs=matrix(c(0,0.5,0.5,1,#primeira linha primeira coluna
   0.5,1,0.5,1,#primeira linha segunda coluna
   0,0.5,0,0.5,#segunda linha primeira coluna
   0.5,1,0,0.5),#segunda linha segunda coluna
 ncol=4,byrow=TRUE),screen=1)


# this produces seven screens numbered like this:
#   3   4   5   6 
#2
#   7   8   9   10 
# select the upper left screen



screen(3)
par(mar=c(0,3.5,3,0))
# now the second set
n250<-bias.alpha1$nsample==250
matplot(x=bias.alpha1$lambda[n250],y=bias.alpha1[n250,3:5],
type="l",pch=1:3,col=c(4,2,3),xaxt="n",ylim=c(-.1, 
.6),main="nsample=250",ylab="", cex.main=1)
abline(h = 0, col = "gray60")
mtext(expression(paste("Bias av. for  ",alpha[1])),side=2,line=2, cex.main=1)

screen(4)
par(mar=c(0,0,3,0))
# now the second set
n1000<-bias.alpha1$nsample==1000
matplot(x=bias.alpha1$lambda[n1000],y=bias.alpha1[n1000,3:5],
type="l",pch=1:3,col=c(4,2,3),xaxt="n",yaxt="n",ylim=c(-.1, 
.6),main="nsample=1000",ylab="")
abline(h = 0, col = "gray60")



screen(5)
par(mar=c(0,3.5,3,0))
# now the second set
par(mar=c(3,3.5,0,0))
# now the second set
n250<-bias.alpha2$nsample==250
matplot(x=bias.alpha2$lambda[n250],y=bias.alpha2[n250,3:5],
type="l",pch=1:3,col=c(4,2,3),ylim=c(-.1, .6),ylab="")
abline(h = 0, col = "gray60")
mtext(expression(paste("Bias av. for  ",alpha[2])),side=2,line=2, cex.main=1.5)

screen(6)
par(mar=c(3,0,0,0))
# now the second set
n1000<-bias.alpha2$nsample==1000
matplot(x=bias.alpha2$lambda[n1000],y=bias.alpha2[n1000,3:5],
type="l",pch=1:3,col=c(4,2,3),yaxt="n",ylim=c(-.1, .6))
abline(h = 0, col = "gray60")




screen(7)
par(mar=c(0,3.5,3,0))
# now the second set
n250<-SE.alpha1$nsample==250
matplot(x=SE.alpha1$lambda[n250],y=SE.alpha1[n250,3:5],
type="l",pch=1:3,col=c(4,2,3),xaxt="n",ylim=c(0, 
1.1),main="nsample=250",ylab="", cex.main=1)
abline(h = -1, col = "gray60")
mtext(expression(paste("SE av. for  ",alpha[1])),side=2,line=2, cex.main=1)
mtext(expression(paste(lambda)),side=1,line=2, cex.main=1.5)


screen(8)
par(mar=c(0,0,3,0))
# now the second set
n1000<-SE.alpha1$nsample==1000
matplot(x=SE.alpha1$lambda[n1000],y=SE.alpha1[n1000,3:5],
type="l",pch=1:3,col=c(4,2,3),xaxt="n",yaxt="n",ylim=c(0, 
1.1),main="nsample=1000",ylab="")
abline(h = -1, col = "gray60")




screen(9)
par(mar=c(3,3.5,0,0))
# now the second set
n250<-SE.alpha2$nsample==250
matplot(x=SE.alpha2$lambda[n250],y=SE.alpha2[n250,3:5],
type="l",pch=1:3,col=c(4,2,3),ylim=c(0, 1.1),ylab="")
abline(h = -.5, col = "gray60")
mtext(expression(paste("SE av. for  ",alpha[2])),side=2,line=2, cex.main=1.5)
mtext(expression(paste(lambda)),side=1,line=2, cex.main=1.5)


screen(10)
par(mar=c(3,0,0,0))
# now the second set
n1000<-SE.alpha2$nsample==1000
matplot(x=SE.alpha2$lambda[n1000],y=SE.alpha2[n1000,3:5],
type="l",pch=1:3,col=c(4,2,3),yaxt="n",ylim=c(0, 1.1))
abline(h = -.5, col = "gray60")
mtext(expression(paste(lambda)),side=1,line=2, , cex.main=1.5)



screen(2)
par(mar=c(0,0,0,0))
# plot an empty plot to get the coordinates
plot(0:1,0:1,type="n",axes=FALSE)
legend(0,0.6,c("OLS", "GLS", "Reg. Cal.", "0"),bty = "n", 
lty=1:3,col=c(4,2,3,"gray60"),xpd=TRUE)


close.screen(all=TRUE)




Best,
RO


Atenciosamente,
Rosa Oliveira

-- 



Rosa Celeste dos Santos Oliveira, 

E-mail: rosit...@gmail.com
Tlm: +351 939355143 
Linkedin: https://pt.linkedin.com/in/rosacsoliveira

"Many admire, few know"
Hippocrates

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

Re: [R] generate ordered categorical variable in R

2015-09-16 Thread Marc Schwartz

> On Sep 16, 2015, at 3:40 PM, Bert Gunter  wrote:
> 
> Nope. Take it back. I stand uncorrected.
> 
>> system.time(z <-sample(1:10,1e6, rep=TRUE))
>   user  system elapsed
>  0.045   0.001   0.047
> 
>> system.time(z <-sample.int(10,1e6,rep=TRUE))
>   user  system elapsed
>  0.012   0.000   0.013
> 
> 
> sample() has to do subscripting in the general case; sample.int doesn't.
> 
> But I would agree that the difference is likely almost always unnoticeable.


Well, in your defense Bert, given the nuance of the example you provided, it 
actually gets worse the larger the initial sample space is, if defined as a 
vector rather than a scalar.

On my MacBook Pro, with 16 Gb of RAM and a 2.5 Ghz i7, running R version 3.2.2 
(2015-08-14):

> system.time(x1 <- sample(1:1e10, 1e8, replace = TRUE))
Killed: 9

That ran for a couple of minutes and eventually crashed R.

However, as below:

> system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
   user  system elapsed 
  2.943   0.238   3.191 

> system.time(x1 <- sample.int(1e10, 1e8, replace = TRUE))
   user  system elapsed 
  3.135   0.198   3.336 


Here is another example that works, showing a larger time difference with the 
sample space as a vector:

> system.time(x1 <- sample(1:1e9, 1e8, replace = TRUE))
   user  system elapsed 
  7.069   1.317   8.399 

> system.time(x1 <- sample(1e9, 1e8, replace = TRUE))
   user  system elapsed 
  1.324   0.111   1.438 

> system.time(x1 <- sample.int(1e9, 1e8, replace = TRUE))
   user  system elapsed 
  1.328   0.116   1.450 


If one is running Monte Carlo simulations, repeating the above a very large 
number of times, it can become a meaningful difference.

Thus, there is an incentive for one to specify the sample space as a scalar and 
perhaps consider the resultant vector, if needed, as indices (1:x) into the 
actual sample space desired.

Interesting...

Regards,

Marc


> 
> 
> -- Bert
> Bert Gunter
> 
> "Data is not information. Information is not knowledge. And knowledge
> is certainly not wisdom."
>   -- Clifford Stoll
> 
> 
> On Wed, Sep 16, 2015 at 1:34 PM, Bert Gunter  wrote:
>> Yes. Thanks Marc. I stand corrected.
>> 
>> -- Bert
>> Bert Gunter
>> 
>> "Data is not information. Information is not knowledge. And knowledge
>> is certainly not wisdom."
>>   -- Clifford Stoll
>> 
>> 
>> On Wed, Sep 16, 2015 at 1:28 PM, Marc Schwartz  wrote:
>>> 
 On Sep 16, 2015, at 1:06 PM, Bert Gunter  wrote:
 
 Yikes! The uniform distribution is a **continuous** distribution over
 an interval. You seem to want to sample over a discrete distribution.
 See ?sample for that, as in:
 
 sample(1:4,100,rep=TRUE)
 
 ## or for this special case and faster
 
 sample.int(4,size=100,rep=TRUE)
>>> 
>>> 
>>> Bert,
>>> 
>>> I am not sure that it is really faster, since internally, sample() calls 
>>> sample.int():
>>> 
 sample
>>> function (x, size, replace = FALSE, prob = NULL)
>>> {
>>>if (length(x) == 1L && is.numeric(x) && x >= 1) {
>>>if (missing(size))
>>>size <- x
>>>sample.int(x, size, replace, prob)
>>>}
>>>else {
>>>if (missing(size))
>>>size <- length(x)
>>>x[sample.int(length(x), size, replace, prob)]
>>>}
>>> }
>>> 
>>> 
>>> set.seed(1)
>>> 
 system.time(x1 <- sample(1e10, 1e8, replace = TRUE))
>>>   user  system elapsed
>>>  2.755   0.170   2.925
>>> 
>>> 
>>> set.seed(1)
 system.time(x2 <- sample.int(1e10, 1e8, replace = TRUE))
>>>   user  system elapsed
>>>  2.767   0.183   2.951
>>> 
>>> 
 all(x1 == x2)
>>> [1] TRUE
>>> 
>>> 
>>> Regards,
>>> 
>>> Marc
>>> 
>>> 
 
 Cheers,
 Bert
 
 Bert Gunter
 
 "Data is not information. Information is not knowledge. And knowledge
 is certainly not wisdom."
  -- Clifford Stoll
 
 
 On Wed, Sep 16, 2015 at 10:11 AM, thanoon younis
  wrote:
> Dear R- users
> 
> I want to generate ordered categorical variable vector with 200x1 
> dimension
> and from 1 to 4 categories and i tried with this code
> 
> Q1=runif(200,1,4) the results are not just 1 ,2 3,4, but the results with
> decimals like 1.244, 2.342,4,321 and so on ... My question how can i
> generate a vector and also a matrix with orered categorical variables and
> without decimals just 1,2,3 ,4 ,1,2,3,4, 
> 
> Many thanks in advance
>>> 

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


Re: [R] Multiple if function

2015-09-16 Thread David Winsemius

On Sep 15, 2015, at 7:20 PM, Charles C. Berry wrote:

> On Tue, 15 Sep 2015, Bert Gunter wrote:
> 
>> Thanks to both Davids.
>> 
>> I realize that these things are often a matter of aesthetics -- and
>> hence have little rational justification -- but I agree with The Other
>> David: eval(parse) seems to me to violate R's soul( it makes R a macro
>> language instead of a functional one).
>> 
>> However, mapply(... switch) effectively loops through the frame row by
>> row. Aesthetically, I like it; but it seems inefficient. If there are
>> e.g. 1e6 rows in say 10 categories, I think Jeff's approach should do
>> much better.  I'll try to generate some actual data to see unless
>> someone else beats me to it.
> 
> Use mapply like this on large problems:
> 
> unsplit(
>mapply(
>function(x,z) eval( x, list( y=z )),
>expression( A=y*2, B=y+3, C=sqrt(y) ),
>split( dat$Flow, dat$ASB ),
>SIMPLIFY=FALSE),
>dat$ASB)
> 

Seems unnecessarily complex, but definitely elegant. Was there a reason it was 
not just:

mapply(
   function(x,z) eval( x, list( y=z )),
   expression(A= y*2, B=y+3, C=sqrt(y) ),
   split( dat$Flow, dat$ASB )
 )

Also readers should note that the names in that expression vector are quite 
arbitrary at the moment. The only association is via the order. I don't suppose 
someone wants to take on the challenge of matching the names of the expression 
vector with the names of returned split components?


> Chuck

David Winsemius
Alameda, CA, USA

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


Re: [R] Looking for Post-hoc tests (a la TukeyHSD) or interaction-level independent contrasts for survival analysis.

2015-09-16 Thread David Winsemius

On Sep 15, 2015, at 12:09 PM, Huot, Matthieu wrote:

> Hi Tom
> 
> I know the post is over 7-8 years old but I am having the same question. How 
> to do a post-hoc test like TukeyHSD on coxph type output.

Create a new variable using the `interaction`-function, apply you contrasts to 
that object, and that should let you side-step all the errors of the person 
trying to follow Crawley's book.

-- 
David.

> 
> Have you received any info in this matter?
> Thanks
> Matthieu
> 
> Looking for Post-hoc tests (a la TukeyHSD) or interaction-level independent 
> contrasts for survival analysis.
> Thomas Oliver toliver at stanford.edu 
> 
> Tue Apr 29 23:12:33 CEST 2008
> 
>  *   Previous message: [R] AIC extract and comparison 
> 
>  *   Next message: [R] for loop in nls function 
> 
>  *   Messages sorted by: [ date 
> ] [ thread 
> ] [ 
> subject 
> ] [ 
> author ]
> 
> 
> 
> Hello all R-helpers,
> 
> 
> 
>I've performed an experiment to test for differential effects of
> 
> elevated temperatures on three different groups of corals.  I'm
> 
> currently performing a cox proportional hazards regression with
> 
> censoring on the survivorship (days to mortality) of each individual
> 
> in the experiment with two factors: Temperature Treatment (2 levels:
> 
> ambient and elevated) and experimental group (3 levels: say 1,2,3).
> 
> 
> 
> In my experiment, all three groups survived equally well in the
> 
> ambient control treatment, but  two of three of the groups succumbed
> 
> to heat stress in the elevated temperature treatment.  I can see that
> 
> the third group had a small degree of mortality, but it appears to be
> 
> significantly less than the other two and may be not significantly
> 
> different from the ambient controls.
> 
> 
> 
> I would like to ask three questions:  1) Is group 3 different from
> 
> controls? 2) Is group 3 different from group 1 and/or group 2 in the
> 
> elevated treatment? and 3) are groups 1 and 2 different from each
> 
> other in the elevated treatment?
> 
> 
> 
>   Because I'm testing for differential effects among the elevated
> 
> temperature treatment group, and "I've seen the data" by now,  the
> 
> analysis would be easiest for me if I performed a responsible
> 
> multiple comparisons test like TukeyHSD to see how each of the six
> 
> Treatment:Group subgroups compared to each other.  TukeyHSD does not
> 
> appear to be defined for outputs from the function coxph -- (see
> 
> survival library).
> 
> 
> 
> cph <- coxph(Surv(DayToMort, Censor) ~ Treatment*Group, data=subb)
> 
> 
> 
> --> Does anyone know of an implementation of TukeyHSD that would
> 
> work, or of another post-hoc multiple comparison test?
> 
> 
> 
> I believe that another responsible tack would be to clearly define
> 
> the contrasts I'd like to make within the interaction term. However
> 
> this has yet to work as fully as I'd like it.
> 
> 
> 
>  I've successfully set the contrasts matrix for the three-level
> 
> factor "Group" following Crawley's The R Book.
> 
> 
> 
> cmat<-cbind(c(-1,1,0),c(0,-1,1))
> 
> contrasts(subb$Group)<-cmat
> 
> contrasts(subb$Group)
> 
> 
> 
> By setting these contrasts and then looking at the interaction terms
> 
> in the coxph model, this allows me to compare groups _within_ each
> 
> separate treatment, and confirms both that #2) that groups 1 and 2
> 
> are not sig. different in the elevated treatment, and #3) the group3
> 
> corals survived significantly better than the other groups in the
> 
> elevated treatment. BUT it does not allow me to say if the group3
> 
> survival is or is not different from its own control.(#1 above).
> 
> 
> 
> To make this comparison, I've tried setting the contrast matrix for
> 
> the Treatment:Group interaction term, with no success.  Whenever I
> 
> attempt to do so, I run the code below:
> 
> 
> 
> cmat<-cbind(c(-1,0,0,1,0,0),c(0,-1,0,0,1,0),c(0,0,-1,0,0,1),c(0,0,0,-1,1,0),c(0,0,0,0,-1,1))
> 
> #Build a matrix
> 
> rownames(cmat)<-rownames(contrasts(subb$Treatment:subb$Group))  #give
> 
> cmat the correct rownames
> 
> colnames(cmat)<-colnames(contrasts(subb$Treatment:subb$Group))
> 
> #give cmat the correct colnames
> 
> contrasts(subb$Treatment:subb$Group)<-cmat   #try to assign cmat
> 
> 
> 
> and I get this error message:
> 
> Error in contras

Re: [R] Multiple if function

2015-09-16 Thread Bert Gunter
I assume it's to return the vectors in their original order.

-- Bert


Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 2:10 PM, David Winsemius  wrote:
>
> On Sep 15, 2015, at 7:20 PM, Charles C. Berry wrote:
>
>> On Tue, 15 Sep 2015, Bert Gunter wrote:
>>
>>> Thanks to both Davids.
>>>
>>> I realize that these things are often a matter of aesthetics -- and
>>> hence have little rational justification -- but I agree with The Other
>>> David: eval(parse) seems to me to violate R's soul( it makes R a macro
>>> language instead of a functional one).
>>>
>>> However, mapply(... switch) effectively loops through the frame row by
>>> row. Aesthetically, I like it; but it seems inefficient. If there are
>>> e.g. 1e6 rows in say 10 categories, I think Jeff's approach should do
>>> much better.  I'll try to generate some actual data to see unless
>>> someone else beats me to it.
>>
>> Use mapply like this on large problems:
>>
>> unsplit(
>>mapply(
>>function(x,z) eval( x, list( y=z )),
>>expression( A=y*2, B=y+3, C=sqrt(y) ),
>>split( dat$Flow, dat$ASB ),
>>SIMPLIFY=FALSE),
>>dat$ASB)
>>
>
> Seems unnecessarily complex, but definitely elegant. Was there a reason it 
> was not just:
>
> mapply(
>function(x,z) eval( x, list( y=z )),
>expression(A= y*2, B=y+3, C=sqrt(y) ),
>split( dat$Flow, dat$ASB )
>  )
>
> Also readers should note that the names in that expression vector are quite 
> arbitrary at the moment. The only association is via the order. I don't 
> suppose someone wants to take on the challenge of matching the names of the 
> expression vector with the names of returned split components?
>
>
>> Chuck
>
> David Winsemius
> Alameda, CA, USA
>

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


Re: [R] Multiple if function

2015-09-16 Thread Dénes Tóth



On 09/16/2015 04:41 PM, Bert Gunter wrote:

Yes! Chuck's use of mapply is exactly the split/combine strategy I was
looking for. In retrospect, exactly how one should think about it.
Many thanks to all for a constructive discussion .

-- Bert


Bert Gunter



Use mapply like this on large problems:

unsplit(
   mapply(
   function(x,z) eval( x, list( y=z )),
   expression( A=y*2, B=y+3, C=sqrt(y) ),
   split( dat$Flow, dat$ASB ),
   SIMPLIFY=FALSE),
   dat$ASB)

Chuck




Is there any reason not to use data.table for this purpose, especially 
if efficiency is of concern?


---

# load data.table and microbenchmark
library(data.table)
library(microbenchmark)
#
# prepare data
DF <- data.frame(
ASB = rep_len(factor(LETTERS[1:3]), 3e5),
Flow = rnorm(3e5)^2)
DT <- as.data.table(DF)
DT[, ASB := as.character(ASB)]
#
# define functions
#
# Chuck's version
fnSplit <- function(dat) {
unsplit(
mapply(
function(x,z) eval( x, list( y=z )),
expression( A=y*2, B=y+3, C=sqrt(y) ),
split( dat$Flow, dat$ASB ),
SIMPLIFY=FALSE),
dat$ASB)
}
#
# data.table-way (IMHO, much easier to read)
fnDataTable <- function(dat) {
dat[,
result :=
if (.BY == "A") {
2 * Flow
} else if (.BY == "B") {
3 + Flow
} else if (.BY == "C") {
sqrt(Flow)
},
by = ASB]
}
#
# benchmark
#
microbenchmark(fnSplit(DF), fnDataTable(DT))
identical(fnSplit(DF), fnDataTable(DT)[, result])

---

Actually, in Chuck's version the unsplit() part is slow. If the order is 
not of concern (e.g., DF is reordered before calling fnSplit), fnSplit 
is comparable to the DT-version.



Denes

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


Re: [R] HELP ERROR WHEN USING GAMMA2 IN VGLM TO SET MU AS AN INTERCEPT

2015-09-16 Thread Rolf Turner

On 17/09/15 09:50, Khedhaouiria Dikra wrote:

Hi everyone,


I'm trying to fit several models with the VGAM packages using the
gamma2 function with an identity function link. ..




For questions concerning a particular package it is usually best to 
contact the package maintainer (see maintainer("VGAM")) in the first 
instance.  If you do not get a response from the maintainer, then 
address the R-help list.


cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

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


Re: [R] Multiple if function

2015-09-16 Thread Bert Gunter
Dénes:

A fair point! The only reason I have is ignorance -- I have not used
data.table. I am not surprised that it and perhaps other packages
(dplyr maybe?) can do things in a reasonable way very efficiently. The
only problem is that it requires us to learn yet another
package/paradigm.  There may also be issues with ts flexibility
compared to base R data structures, but, again, I must plead ignorance
here.

It is interesting that, mod the unsplit reconstruction of the original
vectors, Chuck's base R solution is as efficient as data.table's.

Cheers,
Bert
Bert Gunter

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
   -- Clifford Stoll


On Wed, Sep 16, 2015 at 4:42 PM, Dénes Tóth  wrote:
>
>
> On 09/16/2015 04:41 PM, Bert Gunter wrote:
>>
>> Yes! Chuck's use of mapply is exactly the split/combine strategy I was
>> looking for. In retrospect, exactly how one should think about it.
>> Many thanks to all for a constructive discussion .
>>
>> -- Bert
>>
>>
>> Bert Gunter
>>
>
> Use mapply like this on large problems:
>
> unsplit(
>mapply(
>function(x,z) eval( x, list( y=z )),
>expression( A=y*2, B=y+3, C=sqrt(y) ),
>split( dat$Flow, dat$ASB ),
>SIMPLIFY=FALSE),
>dat$ASB)
>
> Chuck
>
>
>
> Is there any reason not to use data.table for this purpose, especially if
> efficiency is of concern?
>
> ---
>
> # load data.table and microbenchmark
> library(data.table)
> library(microbenchmark)
> #
> # prepare data
> DF <- data.frame(
> ASB = rep_len(factor(LETTERS[1:3]), 3e5),
> Flow = rnorm(3e5)^2)
> DT <- as.data.table(DF)
> DT[, ASB := as.character(ASB)]
> #
> # define functions
> #
> # Chuck's version
> fnSplit <- function(dat) {
> unsplit(
> mapply(
> function(x,z) eval( x, list( y=z )),
> expression( A=y*2, B=y+3, C=sqrt(y) ),
> split( dat$Flow, dat$ASB ),
> SIMPLIFY=FALSE),
> dat$ASB)
> }
> #
> # data.table-way (IMHO, much easier to read)
> fnDataTable <- function(dat) {
> dat[,
> result :=
> if (.BY == "A") {
> 2 * Flow
> } else if (.BY == "B") {
> 3 + Flow
> } else if (.BY == "C") {
> sqrt(Flow)
> },
> by = ASB]
> }
> #
> # benchmark
> #
> microbenchmark(fnSplit(DF), fnDataTable(DT))
> identical(fnSplit(DF), fnDataTable(DT)[, result])
>
> ---
>
> Actually, in Chuck's version the unsplit() part is slow. If the order is not
> of concern (e.g., DF is reordered before calling fnSplit), fnSplit is
> comparable to the DT-version.
>
>
> Denes

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

[R] finding those elements of listA that are found in listB

2015-09-16 Thread John Sorkin
I have two structures. I think they are lists, but I am not sure. Both 
structures contain integers. I am trying to find those members of list b that 
are found in list a. I have tried to perform the search using grep, but I get 
an error. Please see code below. I would appreciate knowing how to search listB 
for any element in listA
Thanks
John


> str(listA)
 int [1:42] 13083 13705 14123 14168 14382 14652 14654 14678 14817 14822 ...
> str(listB)
 int [1:633] 13083 13083 13083 13083 13083 13083 13705 13705 13705 13705 ...
> grep(listA,listB)
[1] 1 2 3 4 5 6
Warning message:
In grep(listA, listB) :
  argument 'pattern' has length > 1 and only the first element will be used







 

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric 
Medicine
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing) 


Confidentiality Statement:
This email message, including any attachments, is for the sole use of the 
intended recipient(s) and may contain confidential and privileged information. 
Any unauthorized use, disclosure or distribution is prohibited. If you are not 
the intended recipient, please contact the sender by reply email and destroy 
all copies of the original message. 
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] finding those elements of listA that are found in listB

2015-09-16 Thread Richard M. Heiberger
I think you are looking for match or %in% (which is a based on match)

> a <- sample(12)
> b <- c(1, 3, 5, 11, 17)
> a
 [1] 10  8  1  4  7  3  6 11  2 12  5  9
> b
[1]  1  3  5 11 17
> [1] 1
> match(a, b)
 [1] NA NA  1 NA NA  2 NA  4 NA NA  3 NA
> match(a, b, 0)
 [1] 0 0 1 0 0 2 0 4 0 0 3 0
> match(b, a)
[1]  3  6 11  8 NA
> match(b, a, 0)
[1]  3  6 11  8  0
> a %in% b
 [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
> b %in% a
[1]  TRUE  TRUE  TRUE  TRUE FALSE
>

Rich

On Wed, Sep 16, 2015 at 10:43 PM, John Sorkin 
wrote:

> I have two structures. I think they are lists, but I am not sure. Both
> structures contain integers. I am trying to find those members of list b
> that are found in list a. I have tried to perform the search using grep,
> but I get an error. Please see code below. I would appreciate knowing how
> to search listB for any element in listA
> Thanks
> John
>
>
> > str(listA)
>  int [1:42] 13083 13705 14123 14168 14382 14652 14654 14678 14817 14822 ...
> > str(listB)
>  int [1:633] 13083 13083 13083 13083 13083 13083 13705 13705 13705 13705
> ...
> > grep(listA,listB)
> [1] 1 2 3 4 5 6
> Warning message:
> In grep(listA, listB) :
>   argument 'pattern' has length > 1 and only the first element will be used
>
>
>
>
>
>
>
>
>
> John David Sorkin M.D., Ph.D.
> Professor of Medicine
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology and
> Geriatric Medicine
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
>
>
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:16}}

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