Sorry if this is a silly question. I'm trying to extract all elements of a dataframe at a particular row. I can find no mention of this in any documentation and it may be naivety of dataframe on my part as I'm very green at this. Thankyou, Mark
On Thu, 2003-10-09 at 12:02, Mark Lee wrote:> Sorry if this is a silly question. I'm trying to extract all elements > of a dataframe at a particular row. I can find no mention of this in > any documentation and it may be naivety of dataframe on my part as I'm > very green at this. Thankyou,If d is your data.frame then d[ 1, ] will give all the columns coresponding to row 1 in a data.frame> d <- read.table('data') > class(d)[1] "data.frame"> x <- d[1,] > class(x)[1] "data.frame">HTH ------------------------------------------------------------------- Rajarshi Guha <rxg218 at psu.edu> <http://jijo.cjb.net> GPG Fingerprint: 0CCA 8EE2 2EEB 25E2 AB04 06F7 1BB9 E634 9B87 56EE ------------------------------------------------------------------- A red sign on the door of a physics professor: 'If this sign is blue, you're going too fast.'
Mark Lee wrote:> Sorry if this is a silly question. I'm trying to extract all elements > of a dataframe at a particular row. I can find no mention of this in > any documentation and it may be naivety of dataframe on my part as I'm > very green at this. Thankyou, > > Mark > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >dat <- data.frame(var1 = c(1:10), var2 = c(31:40), var3 = c(6:15)) dat[1, ] #extract 1st row dat[1:4, ] #extract rows 1 through 4 dat[c(1,3,5,7,9), ] #extract named non-consecutive rows dat[ ,1] # extract 1st column ... etc It's in 'An Introduction to R' that should have come with your distribution, or in the manuals section of the r website: http://www.r-project.org HTH -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpson at ucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Sorry if this is a silly question. I'm trying to extract all elements > of a dataframe at a particular row.How about df[12, ] to get row number 12?> I can find no mention of this in any documentationYou must have different documentation than me .... Subsetting is described on the first couple of pages of pretty much every introducory manual. cu Philipp -- Dr. Philipp Pagel Tel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany
Suppose foo.frame is your dataframe. Use foo.frame[j,] to extract all of the elements in the jth row of foo.frame. Also, you should get two excellent books that will help your learning process: "The Basics of S and Splus", by Krause and Olsen, Third Edition, Springer-Verlag "Modern Applied Statistics with S", by Venables and Ripley, Fourth Edition, Springer-Verlag Krause & Olsen's book is the more elementary of the two. Venables and Ripley will get you to the point where you can productively use R/Splus for statistical analyses. Hope this helps. Best, david paul -----Original Message----- From: Mark Lee [mailto:mark.lee at orbisuk.com] Sent: Thursday, October 09, 2003 12:02 PM To: r-help at stat.math.ethz.ch Subject: [R] Getting rows from a dataframe Sorry if this is a silly question. I'm trying to extract all elements of a dataframe at a particular row. I can find no mention of this in any documentation and it may be naivety of dataframe on my part as I'm very green at this. Thankyou, Mark ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help