通过使用R中的列内容在数据框中重复行

问题描述 投票:0回答:1

我想通过使用数据帧中列的内容来重复行来创建数据帧。下面是源数据帧。

data.frame(c("a","b","c"), c(4,5,6), c(2,2,3)) -> df
colnames(df) <- c("sample", "measurement", "repeat")
df
  sample measurement repeat
1      a           4      2
2      b           5      2
3      c           6      3

我想通过使用“重复”列及其内容来重复行,以获得类似于以下内容的数据框。理想情况下,我希望对此具有功能。

  sample measurement repeat
1      a           4      2
2      a           4      2
3      b           5      2
4      b           5      2
5      c           6      3
6      c           6      3
7      c           6      3

谢谢!

dataframe repeat
1个回答
0
投票

已解决。 df[rep(rownames(df), df$repeat), ]完成了工作。

© www.soinside.com 2019 - 2024. All rights reserved.