Using row names to select from data.frame returns wrong value?

21 views
Skip to first unread message

Bullet

unread,
Nov 30, 2011, 5:52:54 PM11/30/11
to MadUseR
Hi everybody.

If I index a data frame with a row name that doesn't exist, I seem to
get the closest value. But this doesn't seem right. Ideas?

ar = data.frame(c1 = c("test1", "test1", "test1"), c2 = c(1, 1, 1), c3
= c(1, 3, 20))

row.names(ar) = do.call("paste", c(ar[,c(1,3)], sep = ":"))

> ar
c1 c2 c3
test1:1 test1 1 1
test1:3 test1 1 3
test1:20 test1 1 20

> ar["test1:2",]
c1 c2 c3
test1:20 test1 1 20


I appreciate any thoughts.

William Hoyt

unread,
Nov 30, 2011, 6:33:45 PM11/30/11
to mad...@googlegroups.com
Looks like maybe R accepts a truncated form of the row name, as long as
it is not ambiguous.

This also works with list elements, for example

reg1 <- lm(Y ~ X, data=df1)
reg1$coef

The second command will print the element of the list named "coefficients".

-Bill.


--
William T. Hoyt, Ph.D.
Training Director, Ph.D. Program
Department of Counseling Psychology
University of Wisconsin
335 Education Building, 1000 Bascom Mall
Madison, WI 53706

608-262-0462
wth...@education.wisc.edu

Reply all
Reply to author
Forward
0 new messages