Guest User

Untitled

a guest
Jan 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. pick <- function(x, v1, v2, v3, v4) {
  2. ifelse(x == 1, v1,
  3. ifelse(x == 2, v2,
  4. ifelse(x == 3, v3,
  5. ifelse(x == 4, v4, NA))))
  6. }
  7.  
  8. library(dplyr)
  9. df.faithful <- tbl_df(faithful)
  10. df.faithful$x <- sample(1:4, 272, rep=TRUE)
  11. df.faithful$y1 <- rnorm(n=272, mean=7, sd=2)
  12. df.faithful$y2 <- rnorm(n=272, mean=5, sd=2)
  13. df.faithful$y3 <- rnorm(n=272, mean=7, sd=1)
  14. df.faithful$y4 <- rnorm(n=272, mean=5, sd=1)
  15.  
  16. mutate(df.faithful, y = pick(x, y1, y2, y3, y4))
  17. Source: local data frame [272 x 8]
  18.  
  19. eruptions waiting x y1 y2 y3 y4 y
  20. 1 3.600 79 1 8.439092 5.7753006 8.319372 5.078558 8.439092
  21. 2 1.800 54 2 13.515956 6.1971512 6.343157 4.962349 6.197151
  22. 3 3.333 74 4 7.693941 6.8973365 5.406684 5.425404 5.425404
  23. 4 2.283 62 4 12.595852 6.9953995 7.864423 3.730967 3.730967
  24. 5 4.533 85 3 11.952922 5.1512987 9.177687 5.511899 9.177687
  25. 6 2.883 55 3 7.881350 1.0289711 6.304004 3.554056 6.304004
  26. 7 4.700 88 4 8.636709 6.3046198 6.788619 5.748269 5.748269
  27. 8 3.600 85 1 8.027371 6.3535056 7.152698 7.034976 8.027371
  28. 9 1.950 51 1 5.863370 0.1707758 5.750440 5.058107 5.863370
  29. 10 4.350 85 1 7.761653 6.2176610 8.348378 1.861112 7.761653
  30. .. ... ... . ... ... ... ... ...
  31.  
  32. mutate(df.faithful, y = switch(x, y1, y2, y3, 4))
  33.  
  34. Error in switch(c(1L, 2L, 4L, 4L, 3L, 3L, 4L, 1L, 1L, 1L, 4L, 3L, 1L, :
  35. EXPR must be a length 1 vector
  36.  
  37. mutate(df.faithful, y = list(y1, y2, y3, y4)[[x]])
  38. Error in list(c(8.43909205142925, 13.5159559591257, 7.69394050059568, :
  39. recursive indexing failed at level 2
  40.  
  41. mutate(df.faithful, y = c(y1, y2, y3, y4)[x])
  42. Source: local data frame [272 x 8]
  43.  
  44. eruptions waiting x y1 y2 y3 y4 y
  45. 1 3.600 79 1 8.439092 5.7753006 8.319372 5.078558 8.439092
  46. 2 1.800 54 2 13.515956 6.1971512 6.343157 4.962349 13.515956
  47. 3 3.333 74 4 7.693941 6.8973365 5.406684 5.425404 12.595852
  48. 4 2.283 62 4 12.595852 6.9953995 7.864423 3.730967 12.595852
  49. 5 4.533 85 3 11.952922 5.1512987 9.177687 5.511899 7.693941
  50. 6 2.883 55 3 7.881350 1.0289711 6.304004 3.554056 7.693941
  51. 7 4.700 88 4 8.636709 6.3046198 6.788619 5.748269 12.595852
  52. 8 3.600 85 1 8.027371 6.3535056 7.152698 7.034976 8.439092
  53. 9 1.950 51 1 5.863370 0.1707758 5.750440 5.058107 8.439092
  54. 10 4.350 85 1 7.761653 6.2176610 8.348378 1.861112 8.439092
  55. .. ... ... . ... ... ... ... ...
  56.  
  57. data_frame(
  58. x = sample(1:4, 10, replace=TRUE),
  59. y1 = rnorm(n=10, mean=7, sd=2),
  60. y2 = rnorm(n=10, mean=5, sd=2),
  61. y3 = rnorm(n=10, mean=7, sd=1),
  62. y4 = rnorm(n=10, mean=5, sd=1)
  63. ) %>%
  64. mutate(y = recode(x,y1,y2,y3,y4))
  65.  
  66. # A tibble: 10 x 6
  67. x y1 y2 y3 y4 y
  68. <int> <dbl> <dbl> <dbl> <dbl> <dbl>
  69. 1 2 6.950106 6.986780 7.826778 6.317968 6.986780
  70. 2 1 5.776381 7.706869 7.982543 5.048649 5.776381
  71. 3 2 7.315477 2.213855 6.079149 6.070598 2.213855
  72. 4 3 7.461220 5.100436 7.085912 4.440829 7.085912
  73. 5 3 5.780493 4.562824 8.311047 5.612913 8.311047
  74. 6 3 5.373197 7.657016 7.049352 4.470906 7.049352
  75. 7 2 6.604175 9.905151 8.359549 6.430572 9.905151
  76. 8 3 11.363914 4.721148 7.670825 5.317243 7.670825
  77. 9 3 10.123626 7.140874 6.718351 5.508875 6.718351
  78. 10 4 5.407502 4.650987 5.845482 4.797659 4.797659
  79.  
  80. library(data.table)
  81.  
  82. dt = data.table(x = c(1,1,2,2), a = 1:4, b = 4:7)
  83.  
  84. dt[, newcol := switch(as.character(x), '1' = a, '2' = b, NA), by = x]
  85. dt
  86. # x a b newcol
  87. #1: 1 1 4 1
  88. #2: 1 2 5 2
  89. #3: 2 3 6 6
  90. #4: 2 4 7 7
  91.  
  92. map <- data.frame(i=1:2,v=10:11)
  93. # i v
  94. # 1 1 10
  95. # 2 2 11
  96.  
  97. set.seed(1)
  98. x <- sample(1:3,10,rep=T)
  99. # [1] 1 2 2 3 1 3 3 2 2 1
  100.  
  101. i <- match(x,map$i)
  102. ifelse(is.na(i),x,map$v[i])
  103. # [1] 10 11 11 3 10 3 3 11 11 10
  104.  
  105. multipleReplace <- function(x, what, by) {
  106. stopifnot(length(what)==length(by))
  107. ind <- match(x, what)
  108. ifelse(is.na(ind),x,by[ind])
  109. }
  110.  
  111. # Create a sample data set
  112. d <- structure(list(x = c(1L, 2L, 2L, 3L, 1L, 3L, 3L, 2L, 2L, 1L), y = c(1L, 2L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 1L)), .Names = c("x", "y"), row.names = c(NA, -10L), class = "data.frame")
  113.  
  114. d %>%
  115. mutate(z = multipleReplace(x, what=c(1,3), by=c(101,103)))
  116. # x y z
  117. # 1 1 1 101
  118. # 2 2 2 2
  119. # 3 2 2 2
  120. # 4 3 3 103
  121. # 5 1 3 101
  122. # 6 3 1 103
  123. # 7 3 3 103
  124. # 8 2 2 2
  125. # 9 2 2 2
  126. # 10 1 1 101
  127.  
  128. require(data.table)
  129. key = data.table(x = 1:2, col = c("a", "b"))
  130.  
  131. setkey(dt, x)
  132. dt[key, new_col := get(i.col), by=.EACHI]
  133. # x a b new_col
  134. # 1: 1 1 4 1
  135. # 2: 1 2 5 2
  136. # 3: 2 3 6 6
  137. # 4: 2 4 7 7
  138.  
  139. df %>%
  140. mutate(row = row_number()) %>%
  141. gather(n, y, y1:y4) %>%
  142. mutate(n = as.integer(str_extract(n, "[0-9]+"))) %>%
  143. filter(x == n) %>%
  144. arrange(row) %>%
  145. select(-c(row, n))
  146.  
  147. vswitch <- function(x, ...) {
  148. mapply(FUN = function(x, ...) {
  149. switch(x, ...)
  150. }, x, ...)
  151. }
  152.  
  153. mutate(df.faithful, y = vswitch(x, y1, y2, y3, y4))
  154.  
  155. library(dplyr)
  156.  
  157. df.faithful <- tbl_df(faithful)
  158. df.faithful$x <- sample(1:4, 272, rep=TRUE)
  159. df.faithful$y1 <- rnorm(n=272, mean=7, sd=2)
  160. df.faithful$y2 <- rnorm(n=272, mean=5, sd=2)
  161. df.faithful$y3 <- rnorm(n=272, mean=7, sd=1)
  162. df.faithful$y4 <- rnorm(n=272, mean=5, sd=1)
  163.  
  164. pick2 <- function(x, v1, v2, v3, v4) {
  165. out = case_when(
  166. x == 1 ~ v1,
  167. x == 2 ~ v2,
  168. x == 3 ~ v3,
  169. x == 4 ~ v4
  170. )
  171. return(out)
  172. }
  173.  
  174. df.faithful %>%
  175. mutate(y = pick2(x, y1, y2, y3, y4))
  176.  
  177. # A tibble: 272 x 8
  178. eruptions waiting x y1 y2 y3 y4 y
  179. <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
  180. 1 3.6 79 3 8.73 7.23 8.89 4.04 8.89
  181. 2 1.8 54 3 9.97 4.31 7.06 5.05 7.06
  182. 3 3.33 74 1 6.65 7.23 4.46 6.49 6.65
  183. 4 2.28 62 1 6.40 4.39 5.41 3.49 6.40
  184. 5 4.53 85 4 3.96 8.85 7.43 6.51 6.51
  185. 6 2.88 55 4 6.36 8.08 5.82 5.06 5.06
  186. 7 4.7 88 1 5.91 6.47 6.43 5.88 5.91
  187. 8 3.6 85 1 7.77 4.55 6.56 5.05 7.77
  188. 9 1.95 51 4 5.74 6.46 6.95 4.26 4.26
  189. 10 4.35 85 1 7.04 1.73 5.71 2.53 7.04
  190. # ... with 262 more rows
Add Comment
Please, Sign In to add comment