Count new items in vector list

I want to count new elements that were not in previous years. In the example

Sample data:

var1 <- list('2003' = 1:3, '2004' = c(4:3), '2005' = c(6,4,1), '2006' = 1:4 ) 

I would like to get a conclusion

 newcount <- list('2003' = 0, '2004' = 1, '2005' = 1, '2006' = 0) 

Bad code:

 newcount <- mapply(setdiff, var1, seq_along(var1), function(i) {if (i > 1) {Reduce(union, var1[i-1], accumulate=T)}}, length) 
+8
r lapply
source share
2 answers

Almost there, but it is better to use vector indexing to work with the offset and after that add the always-known initial element:

 lapply(c(list(`2003`=integer(0)), mapply(setdiff,var1[-1], Reduce(union,var1,accumulate=TRUE)[-length(var1)])),length) $`2003` [1] 0 $`2004` [1] 1 $`2005` [1] 1 $`2006` [1] 0 
+5
source share

Assuming var1 sorted by year, and for 2003 you would like 3 instead of 1 , you could try

 newcount <- lapply(seq_along(var1),function(x){ prev<-unlist(var1[seq_len(x-1)]) # Improvement suggested by plannapus sum(!var1[[x]]%in%prev) # length(which(!var1[[x]]%in%prev)) }) names(newcount)<-names(var1) newcount # $`2003` # [1] 3 # $`2004` # [1] 1 # $`2005` # [1] 1 # $`2006` # [1] 0 

Well, if you are absolutely sure that in 2003 there should be 0 (which I see as an exception for your logic), you can do the following:

 newcount <- c(0, lapply(seq_along(var1)[-1],function(x){ prev<-unlist(var1[seq_len(x-1)]) sum(!var1[[x]]%in%prev) })) 
+4
source share

All Articles