gpt4 book ai didi

r - 使用 dplyr 有条件地将行插入数据帧

转载 作者:行者123 更新时间:2023-12-04 11:51:32 25 4
gpt4 key购买 nike

如何在 后插入新行每个 mtcars 数据集中 hp 超过 110 的行,仍然保留 hp <=110 的汽车?

library(tidyverse)
data(mtcars)

我尝试了以下但没有成功--
mtcars %>% modify_if(.p = .$hp>110,.f = add_row(.after=1)) #noe equal lengths
mtcars %>% filter(hp>110) %>% add_row(.after=1) #only gives an extra row for the first row meeting condition
mtcars %>% rownames_to_column() %>%
group_by(rowname) %>% modify_if(.p=.$hp>110,.f=add_row(.after=1)) #not egual length

以下 - 使用 purrr- 似乎有效:
foo <- function(df){
if (df$hp>110) {df<-add_row(.data=df,.after=1)}
df
}

mtcars %>% rownames_to_column(var = "make") %>% nest(-make) %>%
mutate(new=map(data,~ foo(.x))) %>% select (-data) %>% unnest(new)

任何名为 add_row_if 的函数???

最佳答案

这会在与您的过滤器匹配的每一行下方插入一行新的随机数( runif(length(.)) :

mtcars %>% filter(hp > 110) %>%
group_by(row_number()) %>%
do (
rbind(., runif(length(.)))
) %>%
ungroup()

# # A tibble: 36 × 12
# mpg cyl disp hp drat wt qsec vs
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 18.70000000 8.0000000 360.0000000 175.0000000 3.15000000 3.440000000 17.02000000 0.0000000
# 2 0.98189043 0.5940565 0.7401747 0.7300069 0.73460416 0.102802673 0.06632293 0.3771534
# 3 14.30000000 8.0000000 360.0000000 245.0000000 3.21000000 3.570000000 15.84000000 0.0000000
# 4 0.02192815 0.4811006 0.6456729 0.2900382 0.69145964 0.741733891 0.34932004 0.1356568
# 5 19.20000000 6.0000000 167.6000000 123.0000000 3.92000000 3.440000000 18.30000000 1.0000000
# 6 0.95568849 0.6532453 0.8744450 0.3346875 0.66217223 0.776532606 0.24731722 0.9633778
# 7 17.80000000 6.0000000 167.6000000 123.0000000 3.92000000 3.440000000 18.90000000 1.0000000
# 8 0.43592634 0.6036127 0.3185138 0.1035780 0.48505561 0.007380369 0.24154177 0.4978268
# 9 16.40000000 8.0000000 275.8000000 180.0000000 3.07000000 4.070000000 17.40000000 0.0000000
# 10 0.10970496 0.9063285 0.4659718 0.2793090 0.04670105 0.337342230 0.85691425 0.2758889
# # ... with 26 more rows, and 4 more variables: am <dbl>, gear <dbl>, carb <dbl>,
# # `row_number()` <dbl>

关于r - 使用 dplyr 有条件地将行插入数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43427786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com