R删除索引中的nrow小于特定值的数据帧中的行(R delete rows in data frame where nrow of index is smaller than certain valu

系统教程 行业动态 更新时间:2024-06-14 16:57:41
R删除索引中的nrow小于特定值的数据帧中的行(R delete rows in data frame where nrow of index is smaller than certain value)

当具有相同索引的行数小于预先指定的值时,我想删除数据框中的某些行。

> fof.6.5[1:15, 1:3] draw Fund.ID Firm.ID 1 1 1667 666 2 1 1572 622 3 1 1392 553 4 1 248 80 5 1 3223 332 6 2 2959 1998 7 2 2659 1561 8 2 14233 2517 9 2 10521 12579 10 2 3742 1045 11 3 9093 10121 12 3 15681 21626 13 3 26371 70170 14 4 27633 52720 15 4 13751 656

在这个例子中,我希望每个索引有5行。 第三次抽奖(这是我的索引)少于5行。 如果抽奖少于5行,我如何删除第三个抽奖?

I want to delete certain rows in a data frame when the number of rows with the same index is smaller than a pre-specified value.

> fof.6.5[1:15, 1:3] draw Fund.ID Firm.ID 1 1 1667 666 2 1 1572 622 3 1 1392 553 4 1 248 80 5 1 3223 332 6 2 2959 1998 7 2 2659 1561 8 2 14233 2517 9 2 10521 12579 10 2 3742 1045 11 3 9093 10121 12 3 15681 21626 13 3 26371 70170 14 4 27633 52720 15 4 13751 656

In this example, I want each index to have 5 rows. The third draw (which is my index) has fewer than 5 rows. How can I delete the draws like the third one if they have fewer than 5 rows?

最满意答案

您可以使用dplyr执行此操作(假设您的数据位于名为dt的数据框中:

dt %>% group_by(draw) %>% filter(n() >= 5) %>% ungroup()

或者你可以使用table或xtabs :

tab <- xtabs(~ draw, dt) dt[!dt$draw %in% as.numeric(names(which(tab < 5))), ]

You could do this using dplyr (assuming your data is in a data frame called dt:

dt %>% group_by(draw) %>% filter(n() >= 5) %>% ungroup()

Or you could use table or xtabs:

tab <- xtabs(~ draw, dt) dt[!dt$draw %in% as.numeric(names(which(tab < 5))), ]

更多推荐

本文发布于:2023-04-13 12:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/fce78e3f1b30b0a492c82ad04945cf98.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   定值   数据   nrow   delete

发布评论

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

>www.elefans.com

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