R - creating rows based upon counter value

前端 未结 5 1971
一个人的身影
一个人的身影 2021-01-26 22:22
input <- read.table(header=F, text=\"abc 2 
                def 3 
                pq 2\")
colnames(input) <- c(\"text\",\"count\")

I have input

5条回答
  •  情书的邮戳
    2021-01-26 23:02

    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
    

提交回复
热议问题