[R] how to NULL multiple variables of a df efficiently?

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Wed Feb 25 14:04:26 CET 2009


Wacek Kusnierczyk wrote:

> 
> as above, this works as well:
> 
>     df[, vars] = list(NULL)
> 
> and this, simplest of them all, works too:
> 
>     df[vars] = list(NULL)

That's actually a curious anomaly/design-flaw/whatever... The "usual"
rule is that you can treat data frames as lists, but


> aq <- as.list(head(airquality))
> aq[2:4] <- list(NULL)
> aq
$Ozone
[1] 41 36 12 18 NA 28

$Solar.R
NULL

$Wind
NULL

$Temp
NULL

$Month
[1] 5 5 5 5 5 5

$Day
[1] 1 2 3 4 5 6

> aq[2:4] <- NULL
> aq
$Ozone
[1] 41 36 12 18 NA 28

$Month
[1] 5 5 5 5 5 5

$Day
[1] 1 2 3 4 5 6


#------------- whereas

> aq <- head(airquality)
> aq[2:4] <- NULL
Error in `[<-.data.frame`(`*tmp*`, 2:4, value = NULL) :
  replacement has 0 items, need 18
> aq[2:4] <- list(NULL)
> aq
  Ozone Month Day
1    41     5   1
2    36     5   2
3    12     5   3
4    18     5   4
5    NA     5   5
6    28     5   6


It is not too strange that assigning list(NULL) differs since you can't
have NULL columns in a data frame. It's  more odd that assigning NULL to
a set of variables fails to delete them.  Not sure what the rationale
(if any) for that might be.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list