在dplyr中重复data.frame行 [英] Repeating rows of data.frame in dplyr

查看:111
本文介绍了在dplyr中重复data.frame行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 dplyr 重复我的实际数据行,我遇到麻烦。这里已经有另外的一个帖子 repeat-rows-of-a-data-frame 但没有解决方案 dplyr

I have a trouble with repeating rows of my real data using dplyr. There is already another post in here repeat-rows-of-a-data-frame but no solution for dplyr.

这里我只是想知道如何解决 dplyr
但失败,出现错误:

Here I just wonder how could be the solution for dplyr but failed with error:


错误:错误的结果大小(16)预期4或1

Error: wrong result size (16), expected 4 or 1



library(dplyr)
    df <- data.frame(column = letters[1:4])

    df_rep <- df%>%
      mutate(column=rep(column,each=4))

预期输出

>df_rep 
    column
    #a
    #a
    #a
    #a
    #b
    #b
    #b
    #b
    #*
    #*
    #*


推荐答案

如果data.frame有其他列(在那里,我说过!),但这个风险很大,但是 do bl ock将允许您在 dplyr 管道(虽然, ceci n'est pas un pipe )之前生成派生数据框架:

This is rife with peril if the data.frame has other columns (there, I said it!), but the do block will allow you to generate a derived data.frame within a dplyr pipe (though, ceci n'est pas un pipe):

library(dplyr)
df <- data.frame(column = letters[1:4], stringsAsFactors = FALSE)
df %>%
  do( data.frame(column = rep(.$column, each = 4), stringsAsFactors = FALSE) )
#    column
# 1       a
# 2       a
# 3       a
# 4       a
# 5       b
# 6       b
# 7       b
# 8       b
# 9       c
# 10      c
# 11      c
# 12      c
# 13      d
# 14      d
# 15      d
# 16      d

这篇关于在dplyr中重复data.frame行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆