[R] Automatic routine - NEW

Liaw, Andy andy_liaw at merck.com
Wed Jun 23 15:09:03 CEST 2004


Sounds to me that you are looking for something like:

subDF <- DF[3:5,]

Andy

> From: Monica Palaseanu-Lovejoy
> 
> Hi Again,
> 
> First of all thank you for all the responses to my previous query. 
> Your answers were very helpful and I did the job ;-). Now I hope you 
> can answer as quick the following (sorry I am invading you with 
> trivial questions):
> 
> Let's use again the following data.frame example:
> DF <- data.frame(x=rnorm(5), y=rnorm(5))
> 
> I want to obtain a new data.frame (or matrix) that contains only n 
> rows (from the i rows DF has), and all the columns. If I have 
> to do it 
> step by step I would do something like that, for example:
> 
> a3 <- DF[3,]
> a4 <- DF[4,]
> a5 <- DF[5,]
> b <- data.frame(a3, a4, a5)
> c <- matrix(b, nrow=3, ncol=2, byrow=TRUE)
> 
> Now I want to do the same in one go, so I wrote:
> 
> for (i in 3:5)
> {
> 	d[i] <- DF[i,]
> 	e <- data.frame(d[i])
> 	f <- matrix(e, ncol=2, nrow=3, byrow=TRUE)
> }
> 
> Which of course gives me errors and the matrix f has all elements 
> equal with DF[5,5]. If I don't use [i] after d, the resulting 
> f matrix is 
> made up from the DF[5,] elements (which is quite normal since i 
> replaces itself ...). So .... How is this done correctly?
> 
> I am really appreciating your time and effort to answer me,
> 
> Monica
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list