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
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 > >
The documentation is good for things like this. In most (all recent?) versions of R, "help.start()" brings up a help page. Select "An Introduction to R", and go to "Vector Indices". Also look at "Arrays and Matrices: Array Indexing". There, you will find that "DF[3:5,]" might be what you want. Actually, I'm not certain that your "c" below is what you want, because class(c[1,1]) is "list", and I could not do arithmetic on it. I discovered that when I tried all.equal(c, DF[3:5,]). {Also, "c" is the name of a very useful function; try "?c". It is usually wise to avoid using names of functions for matrices of data.frames. R will select the one you want from the context in most but not all cases.} hope this helps. spencer graves 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): > >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 > >
Hi Spencer, Your answer is very helpful. I was wondering if i should write again to the list to ask where i suppose to read about indices and things liek that since it seems they are very useful in lots of things. But you already gave me the answer. Thanks again, Monica
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 >