[Rd] problem assigning an array to a variable in a data frame

John Fox jfox at mcmaster.ca
Wed Jan 7 23:41:13 MET 2004


Dear r-devel list members,

Dirk Eddelbuettel brought the following problem to my attention. The code 
is abstracted from the appendix on mixed models from my R and S-PLUS Companion:

 > set.seed(12345)  # for reproducibility
 > library(nlme)
Loading required package: lattice
 > data(MathAchieve)
 > data(MathAchSchool)
 > attach(MathAchieve)
 > mses <- tapply(SES, School, mean)
 > detach(MathAchieve)
 > Bryk <- as.data.frame(MathAchieve[, c("School", "SES", "MathAch")])
 > names(Bryk) <- c("school", "ses", "mathach")
 > sample20 <- sort(sample(7185, 20))
 > Bryk$meanses <- mses[as.character(Bryk$school)]
 > Bryk[sample20,]
Error in as.data.frame.default(x[[i]], optional = TRUE) :
         can't coerce array into a data.frame
 >

This is with R 1.8.1 under Windows 2000. The same code works fine with R 
1.8.0. As Dirk discovered, the problem disappears if the assignment

Bryk$meanses <- mses[as.character(Bryk$school)]

is changed to

Bryk$meanses <- as.vector(mses[as.character(Bryk$school)])

It seems to me that it if $ doesn't want to coerce the array 
mses[as.character(Bryk$school)] to a vector (as I guess was previously the 
case), then the error should have been produced at that point.

Thanks,
  John
-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox



More information about the R-devel mailing list