[R] Extracting from data.frame

Bill.Venables at csiro.au Bill.Venables at csiro.au
Mon Nov 15 01:55:17 CET 2010


It's because in this instance R (and S before it) behaves a bit like Microsoft and tries to guess what you really wanted rather than listen to what you asked for.

If you ask for a single column from a data frame, e.g.

df[,1]

then by default this behaves like df[[1]] and you get a single component, no longer a part of the data frame.  If you want to ensure you always get a data frame, you need to say so with

df[,j, drop = FALSE]

If you select a single row from a data frame in general you get a data frame.  The exceptin is when the data frame itself only has one column and the dropping convention swings into action once more.  The way to avoid this is the same, i.e.

df[j,,drop = FALSE]

In general if you want to ensure consistent behaviour in scripts, it is a recommended that you use "drop = FALSE" as a kind of pseudo-index in the last position like this.  You can always demote the object to a simpler form, if that's what you really want and if one exists, using the drop() function later.

Why is it so?  It was not my idea.  I do know, though, that the Matrix package allows matrix operations with this convention inoperative.  Other people must have become a tad frustrated with it before now.

Bill Venables. 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of James Hirschorn
Sent: Monday, 15 November 2010 10:32 AM
To: R-help at r-project.org
Subject: [R] Extracting from data.frame

Can someone please explain the following behavior?

 

df1 and df2 are data.frames. Suppose I want a subset of the rows
(observations) using extraction, say just the first row. What I want to know
is why if df1 has just one column then df1[1,] returns a vector, whereas if
df2 has 2 or more columns then df2[1,] returns a data frame? Why is the
single column case different?


	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list