How to remove all rows from data.frame in r

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

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 > > > > dput(ddf) structure(list(vint1 = c(9L, 9L, 6L, 10L, 7L), vint2 = c(10L, 6L, 2L, 6L, 12L), vfac1 = structure(c(1L, 3L, 2L, 2L, 3L), .Label = c("1", "2", "3"), class = "factor"), vfac2 = structure(c(2L, 3L, 1L, 3L, 1L), .Label = c("2", "3", "4"), class = "factor")), .Names = c("vint1", "vint2", "vfac1", "vfac2"), class = "data.frame", row.names = c(NA, -5L)) 

I did my best:

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

but they do not work. Thank you for your help on this basic subject.

+16
source share
1 answer

If you really want to delete all lines:

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

If you mean keeping 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 
+32
source

Source: https://habr.com/ru/post/974597/


All Articles