26

I have a data frame from which I want to delete all rows while keeping original structure (columns).

ddf <- data.frame(
  vint1 = c(9L, 9L, 6L, 10L, 7L),
  vint2 = c(10L, 6L, 2L, 6L, 12L),
  vfac1 = factor(c("1", "3", "2", "2", "3")),
  vfac2 = factor(c("3", "4", "2", "4", "2"))
)
ddf
#>   vint1 vint2 vfac1 vfac2
#> 1     9    10     1     3
#> 2     9     6     3     4
#> 3     6     2     2     2
#> 4    10     6     2     4
#> 5     7    12     3     2

I tried:

ddf = NA

for(i in 1:nrow(ddf) ddf[i,] = NULL

but they do not work. Thanks for your help on this basic question.

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

53

If you really want to delete all rows:

> ddf <- ddf[0,]
> ddf
[1] vint1 vint2 vfac1 vfac2
<0 rows> (or 0-length row.names)    

If you mean by keeping the structure using placeholders:

> ddf[,]=matrix(ncol=ncol(ddf), rep(NA, prod(dim(ddf))))
> ddf
  vint1 vint2 vfac1 vfac2
1    NA    NA    NA    NA
2    NA    NA    NA    NA
3    NA    NA    NA    NA
4    NA    NA    NA    NA
5    NA    NA    NA    NA 
user1981275
  • 13,002
  • 8
  • 72
  • 101
  • 3
    Why the zero? I think `ddf <- ddf[c(), ]` makes more sense. – Gabor Csardi Aug 31 '14 at 02:08
  • 1
    @GaborCsardi - wouldn't `df[NULL,]` be most appropriate, logically? – Rich Scriven Aug 31 '14 at 02:32
  • 2
    Well, if you know what indexing with `NULL` means. Similarly to indexing with zero. They are both documented, so they are fine solutions. But I think `c()` is just more intuitive, you select zero rows. To be sure about `NULL` and zero I had to check the manual..... But maybe it's just me and everybody else knows what indexing with `NULL` means. – Gabor Csardi Aug 31 '14 at 02:50
  • 12
    Of `c()`, `NULL` and `0`, I'll choose the one that's easiest to type. – Matthew Lundberg Aug 31 '14 at 03:15
  • 4
    Perhaps this is better than your second suggestion: `ddf[which(!is.na(ddf), arr.ind = TRUE)] <- NA`. Whereas your second suggestion just creates a single type of `NA`, my suggestion retains things like the original factor levels and assigns the correct `NA` type to each column. – A5C1D2H2I1M1N2O1R2T1 Aug 31 '14 at 04:50
  • All three methods require your data.frame to have more than one column... the more you know – Collin May 18 '22 at 20:27