按指定数量重复 df 列中的值,并将整数计数连接到重复值

发布于 2025-01-13 23:29:31 字数 527 浏览 0 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2025-01-20 23:29:31

我们可以使用 uncount 来展开行,然后在添加 599

library(dplyr)
library(tidyr)
library(data.table)
library(stringr)
template_df %>% 
   uncount(Count) %>% 
   transmute(Expanded_ID = str_c(Initial_ID, 599 + rowid(Initial_ID), sep = '-'))

-output

 Expanded_ID
1        a-600
2        a-601
3        b-600
4        b-601
5        b-602
6        c-600
7        d-600
8        d-601
9        d-602
10       d-603

后​​获取 rowid ('Initial_ID' 的 paste或使用 <带有 reppaste 的代码>base R

data.frame(Expanded_ID = with(template_df, paste0(rep(Initial_ID, Count), "-", 
       599 + sequence(Count))))

-输出

   Expanded_ID
1        a-600
2        a-601
3        b-600
4        b-601
5        b-602
6        c-600
7        d-600
8        d-601
9        d-602
10       d-603

数据

template_df <- structure(list(Initial_ID = c("a", "b", "c", "d"), Count = c(2L, 
3L, 1L, 4L)), class = "data.frame", row.names = c(NA, -4L))

We may use uncount to expand the rows and then get the rowid (of the 'Initial_ID' to paste after adding 599

library(dplyr)
library(tidyr)
library(data.table)
library(stringr)
template_df %>% 
   uncount(Count) %>% 
   transmute(Expanded_ID = str_c(Initial_ID, 599 + rowid(Initial_ID), sep = '-'))

-output

 Expanded_ID
1        a-600
2        a-601
3        b-600
4        b-601
5        b-602
6        c-600
7        d-600
8        d-601
9        d-602
10       d-603

Or using base R with rep and paste

data.frame(Expanded_ID = with(template_df, paste0(rep(Initial_ID, Count), "-", 
       599 + sequence(Count))))

-output

   Expanded_ID
1        a-600
2        a-601
3        b-600
4        b-601
5        b-602
6        c-600
7        d-600
8        d-601
9        d-602
10       d-603

data

template_df <- structure(list(Initial_ID = c("a", "b", "c", "d"), Count = c(2L, 
3L, 1L, 4L)), class = "data.frame", row.names = c(NA, -4L))
最美的太阳 2025-01-20 23:29:31

另一种 dplyr 解决方案:

library(dplyr)

template_df %>% 
  group_by(Initial_ID) %>% 
  slice(rep(1:n(), each = Count)) %>% 
  mutate(row = 600 + row_number()-1) %>% 
  ungroup() %>% 
  transmute(Expanded_ID = paste(Initial_ID,row, sep = "-")) 
   Expanded_ID
   <chr>      
 1 a-600      
 2 a-601      
 3 b-600      
 4 b-601      
 5 b-602      
 6 c-600      
 7 d-600      
 8 d-601      
 9 d-602      
10 d-603 

An alternative dplyr solution:

library(dplyr)

template_df %>% 
  group_by(Initial_ID) %>% 
  slice(rep(1:n(), each = Count)) %>% 
  mutate(row = 600 + row_number()-1) %>% 
  ungroup() %>% 
  transmute(Expanded_ID = paste(Initial_ID,row, sep = "-")) 
   Expanded_ID
   <chr>      
 1 a-600      
 2 a-601      
 3 b-600      
 4 b-601      
 5 b-602      
 6 c-600      
 7 d-600      
 8 d-601      
 9 d-602      
10 d-603 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文