[R] Automatic routine - NEW

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Wed Jun 23 15:53:49 CEST 2004

On Wed, 2004-06-23 at 13:53, Monica Palaseanu-Lovejoy wrote:
> 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):
> Lets 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)

1. You need to read the section on subsetting a matrix or dataframe and
sequence generation. e.g. help("[") and help(":")

2. b <- DF[3:5, ] should do the trick. Also please learn the difference
between a matrix and dataframe.

3. 'c' is a built in function. Do not create objects with 'c' or any
other built in function if possible.

> 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 dont 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?

You have not define 'd' before the loop. So you cannot subset or assign
to 'd'. Read the errors and it should say something like 'Error: Object
"d" not found'.

> I am really appreciating your time and effort to answer me,

I think you better the read the R manuals or Peter Daalgard's book on
Introduction to R. It might take you a couple of hours/days but the time
spent is well worth it.

> 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