按指定数量重复 df 列中的值,并将整数计数连接到重复值
我想使用 R 从 template_df
创建一个 expanded_df
,其中每行重复 template_df< 中单独列中指定的次数。 /code>,并且整数计数连接到
expanded_df
中的 ID 列,指定该行在 expanded_df
中重复的数字。
我希望每个 ID 类别的计数从 600 开始。
例如,template_df
:
Initial_ID Count
a 2
b 3
c 1
d 4
expanded_df
:
Expanded_ID
a-600
a-601
b-600
b-601
b-602
c-600
d-600
d-601
d-602
d-603
有人有什么想法吗?谢谢!
I would like to use R to create an expanded_df
from a template_df
, where each row is repeated by a number of times specified in a separate column in the template_df
, and an integer count is concatenated to the ID column in the expanded_df
, specifying the number this row has been repeated in the expanded_df
.
I would like this count to start at 600 for each ID class.
E.g., template_df
:
Initial_ID Count
a 2
b 3
c 1
d 4
expanded_df
:
Expanded_ID
a-600
a-601
b-600
b-601
b-602
c-600
d-600
d-601
d-602
d-603
Anyone have any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以使用
uncount
来展开行,然后在添加 599-output
后获取
rowid
('Initial_ID' 的paste
或使用 <带有rep
和paste
的代码>base R-输出
数据
We may use
uncount
to expand the rows and then get therowid
(of the 'Initial_ID' topaste
after adding 599-output
Or using
base R
withrep
andpaste
-output
data
另一种 dplyr 解决方案:
An alternative
dplyr
solution: