looping to make new variables R -
is there cleaner way following using loop solution in r? of now, resorting cutting , pasting, changing reference column want result attached existing data set new variable. thank in advance.
library(tm) library(snowballc) data <- data.frame(c("asd"), c(3)) list <- c("eat", "drink") text <- c("i eat , drink day") text <- corpus(vectorsource(text)) text <- tm_map(text, content_transformer(tolower)) text <- tm_map(text, removepunctuation) text <- tm_map(text, removenumbers) text <- tm_map(text, removewords, stopwords('english')) text <- tm_map(text, stemdocument) text <- tm_map(text, stripwhitespace) text <- as.data.frame(text) text <- text$text text <- strsplit(as.matrix(text), ' +') text <- lapply(text, lapply, function(z) paste0(' ', z)) text <- lapply(text, unlist) implode <- function(..., sep='') { paste(..., collapse=sep) } wordproportion <- function(x, corpus) { y <- length(grep(corpus, x)) return(y/ length(x)) } estimateproportion <- function (corpus, domain) { x <- as.data.frame(extractcorpus2(domain)) %>% distinct(text) x <- implode(x$text, sep=" | ") x <- unlist(lapply(corpus, wordproportion, x)) x <- findinterval(x, quantile(x[x > 0], seq(0, .9, .1))) } data$topic_1 <- estimateproportion(text, list)
here loop, have add column titles later because can't use $ way
for (i in 1:n) { train[,(i + y)] <- estimateproportion(train_corpus, train_lda_term[, i]) # y number of preceding columns , n many columns want add } colnames(train[(1+y):(n+y)] <- paste0("topic_0",1:n)
edit: unless train list, replace train[,(i + y)]
train[(i + y)]
, colnames
names
Comments
Post a Comment