Search code examples
rlistfunctionlapplynested-datalist

List of data frames with the same number of variables and delete duplicates inside one variable and do the same in the rest of the data frames


I have the following list of data frames and each data frame has 3 variables (a, b and c)

my.list <- list(d1, d2, d3, d4)

Inside my data frame, I have duplicated strings in "a" and I want to delete the rows with duplicated values

The current code i am using:

my.listnew <- lapply(my.list, function(x) unique(x["a"]))

The problem i have with this code is that the other 2 columns "b" and "c" are gone and I want to keep them, while the duplicated rows are deleted


Solution

  • Use duplicated to remove the duplicated values in column a while keeping other columns.

    my.listnew <- lapply(my.list, function(x) x[!duplicated(x$a), ])