[R] Automatic routine - NEW

Monica Palaseanu-Lovejoy monica.palaseanu-lovejoy at stud.man.ac.uk
Wed Jun 23 14:53:27 CEST 2004


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




More information about the R-help mailing list