Guest User

Untitled

a guest
Jan 18th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. $`1`
  2. [1] 1 2
  3. $`2`
  4. [1] 1 2 3
  5. $`3`
  6. [1] 1
  7.  
  8. ID Obs
  9. 1 1
  10. 1 2
  11. 2 1
  12. 2 2
  13. 2 3
  14. 3 1
  15.  
  16. ## your list
  17. ll <- list("1" = 1:2, "2" = 1:3, "3" = 1:2)
  18. ## convert to data.frame
  19. dl <- data.frame(ID = rep(names(ll), sapply(ll, length)),
  20. Obs = unlist(ll))
  21.  
  22. > dl
  23. ID Obs
  24. 11 1 1
  25. 12 1 2
  26. 21 2 1
  27. 22 2 2
  28. 23 2 3
  29. 31 3 1
  30. 32 3 2
  31.  
  32. .list <- list(`1` = 1:2, `2` = 1:3, `3` = 1:2)
  33. library(reshape2)
  34. melt(.list)
  35. ## value L1
  36. ## 1 1 1
  37. ## 2 2 1
  38. ## 3 1 2
  39. ## 4 2 2
  40. ## 5 3 2
  41. ## 6 1 3
  42. ## 7 2 3
  43.  
  44. List <- list('1'=c(1,2), '2'= c(1,2,3), '3'=1)
  45. x <- unlist(List) # as suggested by Gavin Simpson
  46. data.frame(ID=substr(names(x),1,1), Obs=x)
  47. ID Obs
  48. 11 1 1
  49. 12 1 2
  50. 21 2 1
  51. 22 2 2
  52. 23 2 3
  53. 3 3 1
  54.  
  55. data.frame(ID=substr(names(x),1,1), Obs=setNames(x, NULL))
  56. ID Obs
  57. 1 1 1
  58. 2 1 2
  59. 3 2 1
  60. 4 2 2
  61. 5 2 3
  62. 6 3 1
  63.  
  64. List2 <- list('week1'=c(1,2), 'week2'= c(1,2,3), 'week3'=1)
  65. x <- unlist(List2)
  66. data.frame(ID=substr(names(x),1,nchar(names(x)[1])-1), Obs=setNames(x, NULL))
  67.  
  68. ID Obs
  69. 1 week1 1
  70. 2 week1 2
  71. 3 week2 1
  72. 4 week2 2
  73. 5 week2 3
  74. 6 week3 1
Add Comment
Please, Sign In to add comment