input <- read.table(header=F, text=\"abc 2
def 3
pq 2\")
colnames(input) <- c(\"text\",\"count\")
I have input
by should make quick work of this. Just process the input by row and then join it all up after:
output <- do.call(rbind, by(input, rownames(input), function(x) {
data.frame(text=rep(x$text, x$count), x$count)
}))
rownames(output) <- NULL
colnames(output) <- c("text","count")
print(output)
## text count
## 1 abc 2
## 2 abc 2
## 3 def 3
## 4 def 3
## 5 def 3
## 6 pq 2
## 7 pq 2