Bug 14263 - Wrong behaviour of [<-.data.frame when removing columns (<-NULL)
Wrong behaviour of [<-.data.frame when removing columns (<-NULL)
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Misc
R 2.10.1
All All
: P5 enhancement
Assigned To: R-core
: 14315 (view as bug list)
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2010-04-20 10:45 UTC by give_me_more_spam
Modified: 2010-06-11 13:04 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description give_me_more_spam 2010-04-20 10:45:20 UTC
[<-.data.frame as documented could be use to remove columns:

> X <- data.frame(A1=1,A2=2,A3=3,A4=4)
> X[2:3] <- list(NULL)
> X
  A1 A4
1  1  4

But if indices are in wrong order (ie not increasing) then: 

> X <- data.frame(A1=1,A2=2,A3=3,A4=4)
> X[3:2] <- list(NULL)
> X
  A1 A3
1  1  3

I look at code of the function, lines 243-251:

243     for (jjj in p:1L) {
244         jj <- jseq[jjj]
245         v <- value[[jvseq[[jjj]]]]
246         if (nrows > 0L && !length(v)) 
247             length(v) <- nrows
248         x[[jj]] <- v
249         if (!is.null(v) && is.atomic(x[[jj]])) 
250             names(x[[jj]]) <- NULL
251     }

There is no check if jseq is increasing. Same results are if one provides character names:

111     X[c("A3","A2")] <- list(NULL)

then jseq is match(j, names(x)).
Comment 1 Peter Dalgaard 2010-05-10 15:01:19 UTC
Sorted now (literally!). 

Fixed in R-devel (2.12.0-to-be).
Comment 2 Peter Dalgaard 2010-06-11 13:04:54 UTC
*** Bug 14315 has been marked as a duplicate of this bug. ***