AdvisoRs: Is the following a bug, feature, hinky error message, or dumb Bert?> mtest <- matrix(1:12,nr=4) > dftest <- data.frame(mtest) > ix <- cbind(1:2,2:3) > mtest[ix] <- NA > mtest[,1] [,2] [,3] [1,] 1 NA 9 [2,] 2 6 NA [3,] 3 7 11 [4,] 4 8 12 ## But ...> dftest[ix] <- NAError in `[<-.data.frame`(`*tmp*`, ix, value = NA) : only logical matrix subscripts are allowed in replacement Obviously, I was expecting matrix indexing for replacement to work similarly in both cases; however, I can see why it would be problematic for data frames (mixed types), but was a bit nonplussed by the error message, which seems hinky to me. Cheers, Bert -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
Hi Bert, The failure itself is the documented behavior: ?'[.data.frame' says "Matrix indexing ('x[i]' with a logical or a 2-column integer matrix 'i') using '[' is not recommended, and barely supported. For extraction, 'x' is first coerced to a matrix. For replacement, a logical matrix (only) can be used to select the elements to be replaced in the same way as for a matrix." The error message may be a bit hinky, as obviously data.frames can be indexed by things other than logical matricies. Or is there another reason this strikes you as odd? Best, Ista On Tue, May 1, 2012 at 1:33 PM, Bert Gunter <gunter.berton at gene.com> wrote:> AdvisoRs: > > Is the following a bug, feature, hinky error message, or dumb Bert? > >> mtest <- matrix(1:12,nr=4) >> dftest <- data.frame(mtest) >> ix <- cbind(1:2,2:3) >> mtest[ix] <- NA >> mtest > ? ? [,1] [,2] [,3] > [1,] ? ?1 ? NA ? ?9 > [2,] ? ?2 ? ?6 ? NA > [3,] ? ?3 ? ?7 ? 11 > [4,] ? ?4 ? ?8 ? 12 > > ## But ... >> dftest[ix] <- NA > Error in `[<-.data.frame`(`*tmp*`, ix, value = NA) : > ?only logical matrix subscripts are allowed in replacement > > Obviously, I was expecting matrix indexing for replacement to work > similarly in both cases; however, I can see why it would be > problematic for data frames (mixed types), but was a bit nonplussed by > the error message, which seems hinky to me. > > Cheers, > Bert > > -- > > Bert Gunter > Genentech Nonclinical Biostatistics > > Internal Contact Info: > Phone: 467-7374 > Website: > http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm > > ______________________________________________ > 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.
On 01-May-2012 17:33:23 Bert Gunter wrote:> AdvisoRs: > > Is the following a bug, feature, hinky error message, or dumb Bert? > > mtest <- matrix(1:12,nr=4) > dftest <- data.frame(mtest) > ix <- cbind(1:2,2:3) > mtest[ix] <- NA > mtest > [,1] [,2] [,3] > [1,] 1 NA 9 > [2,] 2 6 NA > [3,] 3 7 11 > [4,] 4 8 12 > >## But ... > dftest[ix] <- NA > Error in `[<-.data.frame`(`*tmp*`, ix, value = NA) : > only logical matrix subscripts are allowed in replacement > > Obviously, I was expecting matrix indexing for replacement to > work similarly in both cases; however, I can see why it would > be problematic for data frames (mixed types), but was a bit > nonplussed by the error message, which seems hinky to me. > > Cheers, > BertAlso interesting is that, prior to the substitution commands, mtest[ix] # [1] 5 10 dftest[ix] # [1] 5 10 both as one would expect on Bert's "naive" gounds (which, I confess, I also share[d]). Ted. ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 01-May-2012 Time: 19:03:14 This message was sent by XFMail
David Winsemius
2012-May-01 21:34 UTC
[R] Data frame vs matrix quirk: Hinky error message?
On May 1, 2012, at 1:33 PM, Bert Gunter wrote:> AdvisoRs: > > Is the following a bug, feature, hinky error message, or dumb Bert? > >> mtest <- matrix(1:12,nr=4) >> dftest <- data.frame(mtest) >> ix <- cbind(1:2,2:3) >> mtest[ix] <- NA >> mtest > [,1] [,2] [,3] > [1,] 1 NA 9 > [2,] 2 6 NA > [3,] 3 7 11 > [4,] 4 8 12 > > ## But ... >> dftest[ix] <- NA > Error in `[<-.data.frame`(`*tmp*`, ix, value = NA) : > only logical matrix subscripts are allowed in replacementI'm not sure _I_ would have expected '[<-.data.frame' to recognize that a matrix was being offered because the "[.]" formalism without a comma (called "i-indexing" on the help page) would generally be referencing only columns (i.e. list elements). I had not realized the possibilitiy of offering a logical matrix to df but it does succeed as predicted by ?"[.data.frame" " For replacement, a logical matrix (only) can be used to select the elements to be replaced in the same way as for a matrix." So how you want to characterize documented behavior is your call. I would never choose the label you offered. > mtest <- matrix(FALSE, 4,4) > ix <- cbind(1:2,2:3) > dftest <- data.frame(mtest) > mtest[ix] <- TRUE > dftest[mtest] <- "a" > dftest X1 X2 X3 X4 1 FALSE a FALSE FALSE 2 FALSE FALSE a FALSE 3 FALSE FALSE FALSE FALSE 4 FALSE FALSE FALSE FALSE The nonassignment operation still succeeds: > dftest[ix] [1] "a" "a"> Obviously, I was expecting matrix indexing for replacement to work > similarly in both cases; however, I can see why it would be > problematic for data frames (mixed types), but was a bit nonplussed by > the error message, which seems hinky to me. > > Cheers, > Bert > > ---- David Winsemius, MD Heritage Laboratories West Hartford, CT
Reasonably Related Threads
- Filtering out bad data points
- Logistic Regression - Variable Selection Methods With Prediction
- [PATCH 1/2] Modify autoconf tests for intrinsics to stop clang from optimizing them away.
- Problem with filenames with commas in them
- mgcv package plot superimposing smoothers