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


i have data.frame objects in list output of custom function, , intend proceed duplicate removal first data.frame objects, while others shouldn't not effected. tried in lapply function control constrain, have subscript error instead. know easier in separately, not desired me. can point me out how make easier in functional programming ? knows useful trick of controlling constrain on specific objects in 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 easy this:

.first <- unique(mylist[[1l]]) res <- c(list(.first), mylist[- 1l]) 

but need constrain duplicate removal effect on first data.frame, while others doesn't remove duplicate, intend implement 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)) ) 

if need use lapply, can loop through sequence of list, , if/else modify list elements

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

or else can assign modified list element

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

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -