Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
- attr type
- 1 1 foo_and_bar
- 2 30 foo_and_bar_2
- 3 4 foo_and_bar
- 4 6 foo_and_bar_2
- attr type_1 type_2
- 1 1 foo bar
- 2 30 foo bar_2
- 3 4 foo bar
- 4 6 foo bar_2
- > strsplit(as.character(before$type),'_and_')
- [[1]]
- [1] "foo" "bar"
- [[2]]
- [1] "foo" "bar_2"
- [[3]]
- [1] "foo" "bar"
- [[4]]
- [1] "foo" "bar_2"
- library(stringr)
- str_split_fixed(before$type, "_and_", 2)
- library(dplyr)
- library(tidyr)
- before <- data.frame(
- attr = c(1, 30 ,4 ,6 ),
- type = c('foo_and_bar', 'foo_and_bar_2')
- )
- before %>%
- separate(type, c("foo", "bar"), "_and_")
- ## attr foo bar
- ## 1 1 foo bar
- ## 2 30 foo bar_2
- ## 3 4 foo bar
- ## 4 6 foo bar_2
- before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
- out <- strsplit(as.character(before$type),'_and_')
- do.call(rbind, out)
- [,1] [,2]
- [1,] "foo" "bar"
- [2,] "foo" "bar_2"
- [3,] "foo" "bar"
- [4,] "foo" "bar_2"
- data.frame(before$attr, do.call(rbind, out))
- library(data.table) ## v 1.9.6+
- setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_")]
- before
- # attr type type1 type2
- # 1: 1 foo_and_bar foo bar
- # 2: 30 foo_and_bar_2 foo bar_2
- # 3: 4 foo_and_bar foo bar
- # 4: 6 foo_and_bar_2 foo bar_2
- setDT(before)[, paste0("type", 1:2) := tstrsplit(type, "_and_", type.convert = TRUE, fixed = TRUE)]
- do.call(rbind, str_split(before$type, '_and_'))
- before$type_1 <- sapply(strsplit(as.character(before$type),'_and_'), "[", 1)
- before$type_2 <- sapply(strsplit(as.character(before$type),'_and_'), "[", 2)
- before$type <- NULL
- before$type_1 <- gsub("_and_.+$", "", before$type)
- before$type_2 <- gsub("^.+_and_", "", before$type)
- before$type <- NULL
- library(splitstackshape)
- cSplit(before, "type", "_and_")
- # attr type_1 type_2
- # 1: 1 foo bar
- # 2: 30 foo bar_2
- # 3: 4 foo bar
- # 4: 6 foo bar_2
- before <- data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
- out <- strsplit(as.character(before$type),'_and_')
- > data.frame(t(sapply(out, `[`)))
- X1 X2
- 1 foo bar
- 2 foo bar_2
- 3 foo bar
- 4 foo bar_2
- after <- with(before, data.frame(attr = attr))
- after <- cbind(after, data.frame(t(sapply(out, `[`))))
- names(after)[2:3] <- paste("type", 1:2, sep = "_")
- > after
- attr type_1 type_2
- 1 1 foo bar
- 2 30 foo bar_2
- 3 4 foo bar
- 4 6 foo bar_2
- out <- setNames(data.frame(before$attr,
- do.call(rbind, strsplit(as.character(before$type),
- split="_and_"))),
- c("attr", paste0("type_", 1:2)))
- out
- attr type_1 type_2
- 1 1 foo bar
- 2 30 foo bar_2
- 3 4 foo bar
- 4 6 foo bar_2
- tmp <- matrix(unlist(strsplit(as.character(before$type), '_and_')), ncol=2,
- byrow=TRUE)
- after <- cbind(before$attr, as.data.frame(tmp))
- names(after) <- c("attr", "type_1", "type_2")
- out <- strcapture(
- "(.*)_and_(.*)",
- as.character(before$type),
- data.frame(type_1 = character(), type_2 = character())
- )
- cbind(before["attr"], out)
- # attr type_1 type_2
- # 1 1 foo bar
- # 2 30 foo bar_2
- # 3 4 foo bar
- # 4 6 foo bar_2
- 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'))
- attr type
- 1 1 foo_and_bar
- 2 30 foo_and_bar_2
- 3 4 foo_and_bar_2_and_bar_3
- 4 6 foo_and_bar
- split_into_multiple <- function(column, pattern = ", ", into_prefix){
- cols <- str_split_fixed(column, pattern, n = Inf)
- # Sub out the ""'s returned by filling the matrix to the right, with NAs which are useful
- cols[which(cols == "")] <- NA
- cols <- as.tibble(cols)
- # name the 'cols' tibble as 'into_prefix_1', 'into_prefix_2', ..., 'into_prefix_m'
- # where m = # columns of 'cols'
- m <- dim(cols)[2]
- names(cols) <- paste(into_prefix, 1:m, sep = "_")
- return(cols)
- }
- after <- before %>%
- bind_cols(split_into_multiple(.$type, "_and_", "type")) %>%
- # selecting those that start with 'type_' will remove the original 'type' column
- select(attr, starts_with("type_"))
- >after
- attr type_1 type_2 type_3
- 1 1 foo bar <NA>
- 2 30 foo bar_2 <NA>
- 3 4 foo bar_2 bar_3
- 4 6 foo bar <NA>
- after %>%
- gather(key, val, -attr, na.rm = T)
- attr key val
- 1 1 type_1 foo
- 2 30 type_1 foo
- 3 4 type_1 foo
- 4 6 type_1 foo
- 5 1 type_2 bar
- 6 30 type_2 bar_2
- 7 4 type_2 bar_2
- 8 6 type_2 bar
- 11 4 type_3 bar_3
- library(reshape2)
- before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
- newColNames <- c("type1", "type2")
- newCols <- colsplit(before$type, "_and_", newColNames)
- after <- cbind(before, newCols)
- after$type <- NULL
- after
- tp <- c("a-c","d-e-f","g-h-i","m-n")
- temp = strsplit(as.character(tp),'-')
- x=c();
- y=c();
- z=c();
- #tab=data.frame()
- #tab= cbind(tab,c(x,y,z))
- for(i in 1:length(temp) )
- {
- l = length(temp[[i]]);
- if(l==2)
- {
- x=c(x,temp[[i]][1]);
- y=c(y,"NA")
- z=c(z,temp[[i]][2]);
- df= as.data.frame(cbind(x,y,z))
- }else
- {
- x=c(x,temp[[i]][1]);
- y=c(y,temp[[i]][2]);
- z=c(z,temp[[i]][3]);
- df= as.data.frame(cbind(x,y,z))
- }
- }
Add Comment
Please, Sign In to add comment