Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all rows from a data.frame? [duplicate]

Tags:

I have a data frame from which I want to delete all rows while keeping 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 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.

like image 375
rnso Avatar asked Aug 31 '14 01:08

rnso


People also ask

How do you remove duplicate rows in Excel using Python?

We use drop_duplicates() function to remove duplicate records from a data frame in Python scripts.

How do I remove duplicates in a column in a Data frame?

To drop duplicate columns from pandas DataFrame use df. T. drop_duplicates(). T , this removes all columns that have the same data regardless of column names.

How do I remove duplicate rows in R?

Remove Duplicate rows in R using Dplyr – distinct () function. Distinct function in R is used to remove duplicate rows in R using Dplyr package. Dplyr package in R is provided with distinct() function which eliminate duplicates rows with single variable or with multiple variable.

How can I delete duplicate rows from one column in pandas?

To remove duplicates of only one or a subset of columns, specify subset as the individual column or list of columns that should be unique. To do this conditional on a different column's value, you can sort_values(colname) and specify keep equals either first or last .


1 Answers

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  
like image 132
user1981275 Avatar answered Sep 30 '22 06:09

user1981275