Advertisement
Edster

count data in other list

Nov 18th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.67 KB | None | 0 0
  1. library(magrittr)
  2.  
  3. X=list()
  4. X[[1]]= "1,2,3,4,5,6,7,8,9,19"
  5. X[[2]]= "1,3,4,6,8,9,10,15,17,18"
  6. X[[3]]= "4,5,7,11,20,24,25,27,28,29,39"
  7.  
  8. X = lapply(X, function(x){as.integer(strsplit(x, ",")[[1]])});
  9. Y = unique(sort(unlist(X)))
  10. Z = matrix(0, nrow=length(X), ncol=max(Y))
  11. Count=numeric(length(X))
  12.  
  13. for(i in 1:length(X))
  14.   for(j in 1:10)
  15.     Z[i,X[[i]][j]] = 1
  16.  
  17. for(i in 1:length(X))
  18.   Count[i] = sum(rowSums(Z[, X[[i]]]) >= 5) - 1
  19.  
  20. ##
  21. ps:
  22. the question below:
  23. https://www.ptt.cc/bbs/R_Language/M.1447825217.A.A04.html
  24.  
  25. and the reply by me:
  26. https://www.ptt.cc/bbs/R_Language/M.1447846663.A.E01.html
  27.  
  28. the modified code by celestialgod:
  29. http://pastebin.com/DQ6eczx2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement