[R] str(data.frame) after subsetting reflects original structure, not subsetted structure?

Bryan Hanson hanson at depauw.edu
Fri Jul 24 15:17:41 CEST 2009


I find that after subsetting (you may prefer "conditional selection") a data
frame and assigning it to a new object, the str(new object) reflects the
original data frame, not the new one:

A <- rnorm(20)
B <- factor(rep(c("t", "g"), 10))
C <- factor(rep(c("h", "l"), 10))
D <- data.frame(A, B, C)

str(D) # reports correctly

E <- D[D$C == "h",]

str(E) # reports that D$C still has 2 levels, but
E # or E$C shows that subsetting worked properly
Summary(E) # shows the original structure and that subsetting worked

Is this the expected behavior, and if so, is there a particular rationale?
I would be pretty certain that the information about E was inherited from D,
but why wasn't it updated to reflect the revised object?  Is there an
argument that I can use to force the updating?

For better or worse, I use str() a lot to check my work, and in this case,
it seems to have misled me.

Thanks as always, Bryan
*************
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University, Greencastle IN USA




More information about the R-help mailing list