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.
We use drop_duplicates() function to remove duplicate records from a data frame in Python scripts.
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.
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With