在dplyr中重复data.frame行

编程入门 行业动态 更新时间:2024-10-17 17:22:21
本文介绍了在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行

本文发布于:2023-11-22 06:33:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:dplyr   data   frame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!