Skip to content

Instantly share code, notes, and snippets.

@ondrejhlavacek
Created January 22, 2016 14:30
Show Gist options
  • Save ondrejhlavacek/74b6af6afeb62e13e470 to your computer and use it in GitHub Desktop.
Save ondrejhlavacek/74b6af6afeb62e13e470 to your computer and use it in GitHub Desktop.
Text Splitter Demo
library(data.table)
data <- data.table(id = c("10", "20"), str = c("abcd", "efghijkl"));
substringer <- function(string, splitLength = 10) {
start <- 1
end <- splitLength
out <- c()
while (start < nchar(string)) {
out <- c(out, substr(string,start,end))
end <- end + splitLength
start <- start + splitLength
}
out
}
# note this expects columns id and str
textSplitter <- function(data) {
strings <- data$str
output <- data.table()
for (i in 1:length(strings)) {
newstrings <- substringer(
as.character(data[i,str]))
tmptable <- data.table(id=rep(data[i,id], length(newstrings)), str=newstrings)
output <- rbind(output,tmptable)
}
output
}
print(textSplitter(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment