Guest User

Untitled

a guest
Jan 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
  2. attr type
  3. 1 1 foo_and_bar
  4. 2 30 foo_and_bar_2
  5. 3 4 foo_and_bar
  6. 4 6 foo_and_bar_2
  7.  
  8. attr type_1 type_2
  9. 1 1 foo bar
  10. 2 30 foo bar_2
  11. 3 4 foo bar
  12. 4 6 foo bar_2
  13.  
  14. > strsplit(as.character(before$type),'_and_')
  15. [[1]]
  16. [1] "foo" "bar"
  17.  
  18. [[2]]
  19. [1] "foo" "bar_2"
  20.  
  21. [[3]]
  22. [1] "foo" "bar"
  23.  
  24. [[4]]
  25. [1] "foo" "bar_2"
  26.  
  27. library(stringr)
  28. str_split_fixed(before$type, "_and_", 2)
  29.  
  30. library(dplyr)
  31. library(tidyr)
  32.  
  33. before <- data.frame(
  34. attr = c(1, 30 ,4 ,6 ),
  35. type = c('foo_and_bar', 'foo_and_bar_2')
  36. )
  37.  
  38. before %>%
  39. separate(type, c("foo", "bar"), "_and_")
  40.  
  41. ## attr foo bar
  42. ## 1 1 foo bar
  43. ## 2 30 foo bar_2
  44. ## 3 4 foo bar
  45. ## 4 6 foo bar_2
  46.  
  47. before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
  48. out <- strsplit(as.character(before$type),'_and_')
  49. do.call(rbind, out)
  50.  
  51. [,1] [,2]
  52. [1,] "foo" "bar"
  53. [2,] "foo" "bar_2"
  54. [3,] "foo" "bar"
  55. [4,] "foo" "bar_2"
  56.  
  57. data.frame(before$attr, do.call(rbind, out))
  58.  
  59. library(data.table) ## v 1.9.6+
  60. setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_")]
  61. before
  62. # attr type type1 type2
  63. # 1: 1 foo_and_bar foo bar
  64. # 2: 30 foo_and_bar_2 foo bar_2
  65. # 3: 4 foo_and_bar foo bar
  66. # 4: 6 foo_and_bar_2 foo bar_2
  67.  
  68. setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_", type.convert = TRUE, fixed = TRUE)]
  69.  
  70. do.call(rbind, str_split(before$type, '_and_'))
  71.  
  72. before$type_1 <- sapply(strsplit(as.character(before$type),'_and_'), "[", 1)
  73. before$type_2 <- sapply(strsplit(as.character(before$type),'_and_'), "[", 2)
  74. before$type <- NULL
  75.  
  76. before$type_1 <- gsub("_and_.+$", "", before$type)
  77. before$type_2 <- gsub("^.+_and_", "", before$type)
  78. before$type <- NULL
  79.  
  80. library(splitstackshape)
  81. cSplit(before, "type", "_and_")
  82. # attr type_1 type_2
  83. # 1: 1 foo bar
  84. # 2: 30 foo bar_2
  85. # 3: 4 foo bar
  86. # 4: 6 foo bar_2
  87.  
  88. before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
  89. out <- strsplit(as.character(before$type),'_and_')
  90.  
  91. > data.frame(t(sapply(out, `[`)))
  92. X1 X2
  93. 1 foo bar
  94. 2 foo bar_2
  95. 3 foo bar
  96. 4 foo bar_2
  97.  
  98. after <- with(before, data.frame(attr = attr))
  99. after <- cbind(after, data.frame(t(sapply(out, `[`))))
  100. names(after)[2:3] <- paste("type", 1:2, sep = "_")
  101.  
  102. > after
  103. attr type_1 type_2
  104. 1 1 foo bar
  105. 2 30 foo bar_2
  106. 3 4 foo bar
  107. 4 6 foo bar_2
  108.  
  109. out <- setNames(data.frame(before$attr,
  110. do.call(rbind, strsplit(as.character(before$type),
  111. split="_and_"))),
  112. c("attr", paste0("type_", 1:2)))
  113. out
  114. attr type_1 type_2
  115. 1 1 foo bar
  116. 2 30 foo bar_2
  117. 3 4 foo bar
  118. 4 6 foo bar_2
  119.  
  120. tmp <- matrix(unlist(strsplit(as.character(before$type), '_and_')), ncol=2,
  121. byrow=TRUE)
  122. after <- cbind(before$attr, as.data.frame(tmp))
  123. names(after) <- c("attr", "type_1", "type_2")
  124.  
  125. out <- strcapture(
  126. "(.*)_and_(.*)",
  127. as.character(before$type),
  128. data.frame(type_1 = character(), type_2 = character())
  129. )
  130.  
  131. cbind(before["attr"], out)
  132. # attr type_1 type_2
  133. # 1 1 foo bar
  134. # 2 30 foo bar_2
  135. # 3 4 foo bar
  136. # 4 6 foo bar_2
  137.  
  138. before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2', 'foo_and_bar_2_and_bar_3', 'foo_and_bar'))
  139. attr type
  140. 1 1 foo_and_bar
  141. 2 30 foo_and_bar_2
  142. 3 4 foo_and_bar_2_and_bar_3
  143. 4 6 foo_and_bar
  144.  
  145. split_into_multiple <- function(column, pattern = ", ", into_prefix){
  146. cols <- str_split_fixed(column, pattern, n = Inf)
  147. # Sub out the ""'s returned by filling the matrix to the right, with NAs which are useful
  148. cols[which(cols == "")] <- NA
  149. cols <- as.tibble(cols)
  150. # name the 'cols' tibble as 'into_prefix_1', 'into_prefix_2', ..., 'into_prefix_m'
  151. # where m = # columns of 'cols'
  152. m <- dim(cols)[2]
  153.  
  154. names(cols) <- paste(into_prefix, 1:m, sep = "_")
  155. return(cols)
  156. }
  157.  
  158. after <- before %>%
  159. bind_cols(split_into_multiple(.$type, "_and_", "type")) %>%
  160. # selecting those that start with 'type_' will remove the original 'type' column
  161. select(attr, starts_with("type_"))
  162.  
  163. >after
  164. attr type_1 type_2 type_3
  165. 1 1 foo bar <NA>
  166. 2 30 foo bar_2 <NA>
  167. 3 4 foo bar_2 bar_3
  168. 4 6 foo bar <NA>
  169.  
  170. after %>%
  171. gather(key, val, -attr, na.rm = T)
  172.  
  173. attr key val
  174. 1 1 type_1 foo
  175. 2 30 type_1 foo
  176. 3 4 type_1 foo
  177. 4 6 type_1 foo
  178. 5 1 type_2 bar
  179. 6 30 type_2 bar_2
  180. 7 4 type_2 bar_2
  181. 8 6 type_2 bar
  182. 11 4 type_3 bar_3
  183.  
  184. library(reshape2)
  185. before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
  186. newColNames <- c("type1", "type2")
  187. newCols <- colsplit(before$type, "_and_", newColNames)
  188. after <- cbind(before, newCols)
  189. after$type <- NULL
  190. after
  191.  
  192. tp <- c("a-c","d-e-f","g-h-i","m-n")
  193.  
  194. temp = strsplit(as.character(tp),'-')
  195.  
  196. x=c();
  197. y=c();
  198. z=c();
  199.  
  200. #tab=data.frame()
  201. #tab= cbind(tab,c(x,y,z))
  202.  
  203. for(i in 1:length(temp) )
  204. {
  205. l = length(temp[[i]]);
  206.  
  207. if(l==2)
  208. {
  209. x=c(x,temp[[i]][1]);
  210. y=c(y,"NA")
  211. z=c(z,temp[[i]][2]);
  212.  
  213. df= as.data.frame(cbind(x,y,z))
  214.  
  215. }else
  216. {
  217. x=c(x,temp[[i]][1]);
  218. y=c(y,temp[[i]][2]);
  219. z=c(z,temp[[i]][3]);
  220.  
  221. df= as.data.frame(cbind(x,y,z))
  222. }
  223. }
Add Comment
Please, Sign In to add comment