[R] Remove rows that have repeated items in a particular column

2012-07-25 Thread Zhongyi Yuan
Dear R Users,

I apology for not being able to provide an adequately informative subject.
Let me describe my problem with an example.

For a matrix
*(mat - matrix(c(1,1,2,2,2,3,3, 5,9,1,3,7,4,8), ncol = 2))*
my desired output is
*(desired - matrix(c(1,2,3, 5,1,4), ncol = 2))*

That is, the first column is numerically grouped and only the first item in
each group is wanted.  The second column is in increasing order within each
group.  My actual data will be of size 10^6 by 100 so I am hoping to solve
this by a simple function.  Thank you very much for your help.

Best,
Zhongyi Yuan

[[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] Remove rows that have repeated items in a particular column

2012-07-25 Thread Zhongyi Yuan
Worked like a charm.  Thanks.

Zhongyi

On Wed, Jul 25, 2012 at 11:58 PM, MK mkao006rm...@gmail.com wrote:

 Very simple

 mat[!duplicated(mat[, 1]), ]

 M

[[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] color of math annotation in legend

2011-07-28 Thread Zhongyi Yuan
Dear useRs,

Can someone help me to adjust the color of math annotation in a legend? The
following code gives me a black alpha = 2.

x=y=1:100
z=seq(0.5,50,by=0.5)
plot(x,y,type='l',col='black')
lines(x,z,col='red')
legend('topleft',c(expression(paste(alpha, = , 1)),
expression(paste(alpha, = , 2))),col=c('black','red'))

Is it possible to make it red? Thanks in advance for your help.

Best,
Zhongyi Yuan

[[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 of math annotation in legend

2011-07-28 Thread Zhongyi Yuan
Thank you Tyler.
I thought the problem was due to the use of expression(...).

Best,
Zhongyi

On Thu, Jul 28, 2011 at 2:49 AM, Tyler Rinker tyler_rin...@hotmail.comwrote:

  Use the text.col argument as below
 ?legend


 x=y=1:100
 z=seq(0.5,50,by=0.5)
 plot(x,y,type='l',col='black')
 lines(x,z,col='red')
 legend('topleft',c(expression(paste(alpha, = , 1)),
 expression(paste(alpha, = , 2))),text.col=c(black,red))




   Date: Thu, 28 Jul 2011 02:32:04 -0500
  From: zhongyi-y...@uiowa.edu
  To: r-help@r-project.org
  Subject: [R] color of math annotation in legend

 
  Dear useRs,
 
  Can someone help me to adjust the color of math annotation in a legend?
 The
  following code gives me a black alpha = 2.
 
  x=y=1:100
  z=seq(0.5,50,by=0.5)
  plot(x,y,type='l',col='black')
  lines(x,z,col='red')
  legend('topleft',c(expression(paste(alpha, = , 1)),
  expression(paste(alpha, = , 2))),col=c('black','red'))
 
  Is it possible to make it red? Thanks in advance for your help.
 
  Best,
  Zhongyi Yuan
 
  [[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] roundoff error in recursion

2011-06-19 Thread Zhongyi Yuan
Dear R users,

I run into the following problem and hope someone can help me out. Thank you
in advance for your time.

I have a function defined recursively as follows:
recFun = function(k,x){ #i is less than 10. x can be any real number. I need
the value of recFun(k=1,2,3,4, x=0.12).
 if(k==1)
  integrand = function(z) (1 + pi*exp(z+0.9*x+0.012))^(-1.5) * dnorm(z)
 else
  integrand = function(z) (1 + pi*exp(z+0.9*x+0.012))^(-1.5) *
recFun(k-1,z+0.9*x+0.012) * dnorm(z)
 integrate(integrand, -Inf, Inf)
}

The goal of the function is to evaluate a quantity satisfying the recursive
relation that
$$I(1,x)=\int_{-\infty }^{\infty }\left( 1+\pi
e^{z+0.9x+0.012}\right)^{-1.5}\Phi (dz),$$ and
$$I(k,x)=\int_{-\infty }^{\infty }\left( 1+\pi e^{z+0.9x+0.012}\right)
^{-1.5} I(k-1,z+0.9x+0.012)\Phi (dz),$$
where $\Phi$ is the distribution function of the standard normal
distribution.

But for k=2, a roundoff error occurs:
 recFun(2,0.12)
Error in integrate(integrand, -Inf, Inf) : roundoff error was detected

Can someone suggest a solution to this problem? Thanks a lot.

Best,
Zhongyi

[[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] sample exponential r.v. by MCMC

2010-09-29 Thread Zhongyi Yuan
Dear R users,

I am leaning MCMC sampling, and have a problem while trying to sample
exponential r.v.'s via the following code:

samp - MCMCmetrop1R(dexp, theta.init=1, rate=2,
  mcmc=5000, burnin=500,
  thin=10, verbose=500, logfun=FALSE)

I tried other distribtions such as Normal, Gamma with shape1, it works
perfectly fine. Can someon tell me why?
Thank you for your help.

Best,
Zhongyi

[[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] sample exponential r.v. by MCMC

2010-09-29 Thread Zhongyi Yuan
Hi Dennis,

Sorry for not being considerate. I should have at least mentioned I was
using MCMCpack.
I have no idea about traceback() though.

I appreciate your suggestion.

Best,
Zhongyi



On Wed, Sep 29, 2010 at 5:00 PM, Dennis Murphy djmu...@gmail.com wrote:

 Hi:

 It might be helpful to inform the list which package you're using -
 evidently it's MCMCpack, but I had to search to learn that.
 You may also want to include the following information:


  samp - MCMCmetrop1R(dexp, theta.init=1, rate=2,
 +  mcmc=5000, burnin=500,
 +  thin=10, verbose=500, logfun=FALSE)
 Error in optim(theta.init, maxfun, control = optim.control, lower =
 optim.lower,  :
   non-finite finite-difference value [1]
  traceback()
 2: optim(theta.init, maxfun, control = optim.control, lower = optim.lower,
upper = optim.upper, method = optim.method, hessian = TRUE,
...)
 1: MCMCmetrop1R(dexp, theta.init = 1, rate = 2, mcmc = 5000, burnin = 500,
thin = 10, verbose = 500, logfun = FALSE)

 along with the results of sessionInfo(). You may also want to cc the
 package maintainer in case this happens to be a bug:
  maintainer('MCMCpack')
 [1] Andrew D. Martin admar...@wustl.edu

 I don't know enough about the package to help you, but providing this
 information to the list will increase the probability of a successful
 resolution in a shorter period of time.

 Cheers,
 Dennis

 On Wed, Sep 29, 2010 at 2:19 PM, Zhongyi Yuan zhongyi-y...@uiowa.eduwrote:

 Dear R users,

 I am leaning MCMC sampling, and have a problem while trying to sample
 exponential r.v.'s via the following code:

 samp - MCMCmetrop1R(dexp, theta.init=1, rate=2,
  mcmc=5000, burnin=500,
  thin=10, verbose=500, logfun=FALSE)

 I tried other distribtions such as Normal, Gamma with shape1, it works
 perfectly fine. Can someon tell me why?
 Thank you for your help.

 Best,
 Zhongyi

[[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] labels gone

2010-03-16 Thread Zhongyi Yuan
Hi Milton,

Thanks for your help.

I must confess that I don't understand what your code means at this moment.
But you really helped me out and I'll figure it out later. Thanks.

Cheers.
Zhongyi

On Tue, Mar 16, 2010 at 12:26 AM, milton ruser milton.ru...@gmail.comwrote:

 Hi Zhongyi,

 I must confess I not understood completely what you need, but...

 Tau-seq(0.05,0.95,0.05);
 Pi - seq(0.19,0.01,-0.01);
 par(cex.axis=0.8,ps=9,mar=c(1.5,1,0.5,1), oma=c(1,1,0.2,1) ,tck=-0.01);
 plot(Tau,Pi, type='l', xlab=Tau,ylab=Pi,col=4, xaxt=n, yaxt=n);
 axis(1,labels=F)
 axis(1,line=-1, lwd=0)
 axis(2,labels=F)
 axis(2,line=-0.5, lwd=0)
 mtext(x_txt,side=1, line=0.5)
 mtext(y_txt,side=2, line=1.2)

 cheers

 milton
 On Tue, Mar 16, 2010 at 12:28 AM, Zhongyi Yuan zhongyi-y...@uiowa.eduwrote:

 Dear R users:

 I am drawing a graph with the following code:

 Tau-seq(0.05,0.95,0.05);
 Pi - seq(0.19,0.01,-0.01);
 par(cex.axis=0.8,ps=9,mar=c(1.5,1,0.5,1), oma=c(1,1,0.2,1) ,tck=-0.01);
 plot(Tau,Pi, type='l', xlab=Tau,ylab=Pi,col=4);

 I want to make the graph take as little space as possible. Here I run into
 two problems. One is that the labels are gone, the other is the scale
 numbers of axises are a little too far from my axises. Can anyone help me
 with this? I am new to graphics and have spent a lot of time on par but
 still not yet got it.
 Thanks.

 best,
 Zhongyi

[[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.htmlhttp://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] labels gone

2010-03-15 Thread Zhongyi Yuan
Dear R users:

I am drawing a graph with the following code:

Tau-seq(0.05,0.95,0.05);
Pi - seq(0.19,0.01,-0.01);
par(cex.axis=0.8,ps=9,mar=c(1.5,1,0.5,1), oma=c(1,1,0.2,1) ,tck=-0.01);
plot(Tau,Pi, type='l', xlab=Tau,ylab=Pi,col=4);

I want to make the graph take as little space as possible. Here I run into
two problems. One is that the labels are gone, the other is the scale
numbers of axises are a little too far from my axises. Can anyone help me
with this? I am new to graphics and have spent a lot of time on par but
still not yet got it.
Thanks.

best,
Zhongyi

[[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] construct a list that consists of lists

2010-03-12 Thread Zhongyi Yuan
Hi Jim,

Dennis Murphy solved my problem by the following code.
Thank you for you suggestion. Will check out listBuilder function too.

best,
Zhongyi

# (2) A little more general: prespecify the number of list components
#  and run a one-line loop to populate the list
 l - vector('list', 6)
 for(i in seq_along(l)) l[[i]] - v
 l
[[1]]
[[1]]$para1
[1] 1 2 3 4 5

[[1]]$para2
[1] 5 6 7 8 9


[[2]]
[[2]]$para1
[1] 1 2 3 4 5

[[2]]$para2
[1] 5 6 7 8 9


[[3]]
[[3]]$para1
[1] 1 2 3 4 5

[[3]]$para2
[1] 5 6 7 8 9


[[4]]
[[4]]$para1
[1] 1 2 3 4 5

[[4]]$para2
[1] 5 6 7 8 9


[[5]]
[[5]]$para1
[1] 1 2 3 4 5

[[5]]$para2
[1] 5 6 7 8 9


[[6]]
[[6]]$para1
[1] 1 2 3 4 5

[[6]]$para2
[1] 5 6 7 8 9


On Fri, Mar 12, 2010 at 3:06 AM, Jim Lemon j...@bitwrit.com.au wrote:

 On 03/12/2010 05:13 PM, Zhongyi Yuan wrote:

 Dear R users:

 I am hoping that someone can help with constructing a list that consists
 of
 list with the number of lists variable.
 i.e. to find a convenient express(or loop sentences) to realize the
 following:
 list( list(para1=p1, para2=p2), list(para1=p1, para2=p2), ,
 list(para1=p1,para2=p2) )

  Hi Zhongyi,
 Have a look at the listBuilder function in the crank package.

 Jim



[[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] construct a list that consists of lists

2010-03-12 Thread Zhongyi Yuan
Hi Henrique,

Great.
I tried a similar thing but used
list( replicate(6, list(para1 = 1:5, para2 = 5:9), simplify = FALSE) )

and didn't work. Seems I don't need list(...).
Thanks.

Zhongyi

On Fri, Mar 12, 2010 at 11:56 AM, Henrique Dallazuanna www...@gmail.comwrote:

 You can do this also, using replicate:

 replicate(6, list(para1 = 1:5, para2 = 5:9), simplify = FALSE)

 On Fri, Mar 12, 2010 at 2:51 PM, Zhongyi Yuan zhongyi-y...@uiowa.edu
 wrote:
  Hi Jim,
 
  Dennis Murphy solved my problem by the following code.
  Thank you for you suggestion. Will check out listBuilder function too.
 
  best,
  Zhongyi
 
  # (2) A little more general: prespecify the number of list components
  #  and run a one-line loop to populate the list
  l - vector('list', 6)
  for(i in seq_along(l)) l[[i]] - v
  l
  [[1]]
  [[1]]$para1
  [1] 1 2 3 4 5
 
  [[1]]$para2
  [1] 5 6 7 8 9
 
 
  [[2]]
  [[2]]$para1
  [1] 1 2 3 4 5
 
  [[2]]$para2
  [1] 5 6 7 8 9
 
 
  [[3]]
  [[3]]$para1
  [1] 1 2 3 4 5
 
  [[3]]$para2
  [1] 5 6 7 8 9
 
 
  [[4]]
  [[4]]$para1
  [1] 1 2 3 4 5
 
  [[4]]$para2
  [1] 5 6 7 8 9
 
 
  [[5]]
  [[5]]$para1
  [1] 1 2 3 4 5
 
  [[5]]$para2
  [1] 5 6 7 8 9
 
 
  [[6]]
  [[6]]$para1
  [1] 1 2 3 4 5
 
  [[6]]$para2
  [1] 5 6 7 8 9
 
 
  On Fri, Mar 12, 2010 at 3:06 AM, Jim Lemon j...@bitwrit.com.au wrote:
 
  On 03/12/2010 05:13 PM, Zhongyi Yuan wrote:
 
  Dear R users:
 
  I am hoping that someone can help with constructing a list that
 consists
  of
  list with the number of lists variable.
  i.e. to find a convenient express(or loop sentences) to realize the
  following:
  list( list(para1=p1, para2=p2), list(para1=p1, para2=p2), ,
  list(para1=p1,para2=p2) )
 
   Hi Zhongyi,
  Have a look at the listBuilder function in the crank package.
 
  Jim
 
 
 
 [[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.
 



 --
 Henrique Dallazuanna
 Curitiba-Paraná-Brasil
 25° 25' 40 S 49° 16' 22 O


[[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] construct a list that consists of lists

2010-03-11 Thread Zhongyi Yuan
Dear R users:

I am hoping that someone can help with constructing a list that consists of
list with the number of lists variable.
i.e. to find a convenient express(or loop sentences) to realize the
following:
list( list(para1=p1, para2=p2), list(para1=p1, para2=p2), ,
list(para1=p1,para2=p2) )

I appreciate your help.

best,
Zhongyi

[[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] append something to an xls file

2010-02-09 Thread Zhongyi Yuan
Hi everyone,

I run into this problem and hope somebody can help. Thank you in advance.

I have an excel file with the first column being a list of names.
What I want is to add values that I compute for each person to the right of
the corresponding names.
Can someone help me with this? Thanks.

Best,
Zhongyi

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

2010-01-30 Thread Zhongyi Yuan
Hi,

Could anybody please help me with this? In the following function, I try to
return a good format of x, i.e. if my x[1] is 800, I don't want it to give
me 800.0.
I tried format(x, trim = TRUE), but didn't work.
If my x has only first three components, which are all integers, I don't run
into this problem. So I guess fractional numbers cause this problem.
The comment line works, but I wondering if there are better ways of doing
this.
Thank you.

problem1-function(){
 x - round(runif(1,0.5,9.5))*100;
 if (x=500){
x-c(x,x+50+round(runif(1,0.5,19.5))*5,x+50+round(runif(1,0.5,19.5))*5,
  round(runif(1,0.5,4.5))+0.5, round(runif(1,4,9),1));}
 else{

x-c(x,x+100+round(runif(1,0.5,19.5))*5,x+100+round(runif(1,0.5,19.5))*5,
  round(runif(1,0.5,4.5))+0.5, round(runif(1,4,9),1));}

c(format(x))
 # c(format(x[1]),format(x[2]),format(x[3]))
}

Best,
Zhongyi

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