Hi, thanks for the example. I agree that this is dangerous -- that is one of the reasons why I use (and teach others to use) merge() instead of relying on sorting when joining data.frames.
table2$Subject <- rownames(table2) df2 <- merge(df, table2) will work, even when Subject is stored as a character in table2 and as a factor in df. merge() is a reliable and safe way to perform this type of operation. I do agree that it would be good for [ to return a warning when the index(s) are not numeric or character vectors, but training yourself (and others) to use merge() will help avoid these problems. Best, Ista On Mon, Feb 14, 2011 at 2:10 PM, WB Kloke <[email protected]> wrote: > Ista Zahn <izahn <at> psych.rochester.edu> writes: > >> >> Hi, >> I think an example would be helpful. I'm not sure what behavior you >> are referring to. >> >> best, > > Here is an example: > > If a data.frame of experimental results is read from an external > file, a column of strings, e.g. subject codes, is converted to a > factor by default. > > If I have a second table containing additional data for the subjects > (or a big pool of subjects) with subject code as rownames, then > sometimes I want to add data looked up from the 2nd table to the > data.frame, possibly to use them as additional covariates. > > The wrong way to do is: > > df$age <- table2[df$Subject,"age"] > > This doesn't work as expected because df$Subject is silently converted > to a number, which not meaningful for table2 except when the table > happens to list the subjects in the order of levels of df$Subject. > > So only > df$age <- table2[levels(df$Subject)[as.numeric(df$Subject)],"age"] > or > df$age <- table2[as.character(df$Subject),"age"] > > is expected to work correctly, as is explained in the factor() doc. > > This has biten me more than once, and it will bite users I am > going to evangelize R to, too. > > ______________________________________________ > [email protected] 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. > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org ______________________________________________ [email protected] 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.

