Andy.Jian Andy.Jian - 29 days ago 11
R Question

How to constrain duplicate removal for specific data.frame in the list more elegantly?

I have data.frame objects in the list as an output of custom function, and I intend to proceed duplicate removal only first data.frame objects, while others shouldn't not be effected. I tried this in lapply function to control this constrain, but I have subscript error instead. I know this easier to do it in separately, but this is not desired for me. Can anyone point me out how to make this easier in functional programming ? Does anyone knows any useful trick of controlling the constrain on specific objects in the list ?

mini example:

myList <- list(
bar = data.frame(v1=c(12,21,37,21,37), v2=c(14,29,45,29,45)),
cat = data.frame(v1=c(18,42,18,42,81), v2=c(27,46,27,46,114)),
foo = data.frame(v1=c(3,3,33,3,33,91), v2=c(26,26,42,26,42,107))
)


it is easy to do like this:

.first <- unique(myList[[1L]])
res <- c(list(.first), myList[- 1L])


but I need to constrain duplicate removal only effect on the first data.frame, while others doesn't do remove duplicate, I intend to implement this in function more elegant way.

desired output:

myOutput <- list(
bar = data.frame(v1=c(12,21,37),v2=c(14,29,45)),
cat = data.frame(v1=c(18,42,18,42,81), v2=c(27,46,27,46,114)),
foo = data.frame(v1=c(3,3,33,3,33,91), v2=c(26,26,42,26,42,107))
)

Answer

If we need to use lapply, then we can loop through the sequence of list, and with if/else modify the list elements

lapply(seq_along(myList), function(i) if(i==1) unique(myList[[i]]) else myList[[i]])  

Or else we can assign the modified list element

myList[[1]] <- unique(myList[[1]])