我有以下定义:

label  id_row ID    Well  WT_group_1 WT_group_2 WT_group_3 WT_group_4 
<chr>   <int> <chr> <chr> <dbl> <dbl> <dbl>
1 well_1 169 PBS H1 2728. 2521 2360. 2670
2 well_2 361 PBS H1 2688 2534 2418 NA
3 well_3 553 PBS H1 3221 3320.5 2835 3247
4 well_4 745 PBS H1 2838 3362 2832. 2975
5 well_5 937 PBS H1 2857 2654 NA 2637
6 well_6 1129 PBS H1 2834 2678 2287 2713
7 well_7 1189 PBS B7 2374 2625 2444 2622

well_1-well_2, well_3-well_4, well_5-well_6是相关的.然而,因为well_7是单独的,所以如果我有NA值,我不需要将它与任何行相关联.

我需要用相关行的值替换NA.例如:

well_1 2670
well_2 NA
after the operation:
well_1 2670
well_2 2670
or 
for well_4 and 5:
well_4 2832.
well_5 NA
after the operation: 
well_4 2832.
well_5 2832.

我试着用这个来做这个手术:

pbs_in_v2 %>%
    group_by(label) %>%
    fill(everything(), .direction = "downup") %>%
    ungroup()

然而,使用这个 idea 涉及group_by,当我需要做的时候,有特定的条件:

well_1-well_2, well_3-well_4, well_5-well_6 and for well_7, leave it like that.

我认为完成if_else分可能会对此有所裨益.然而,如果我需要使用if,我如何实现我的目标?

有没有一种方法来执行任务与我的主要 idea ,这涉及group_byfill

有什么主意吗?

推荐答案

如果你每隔两行分组,我想你可以得到你想要的结果.

library(tidyverse)

df <- read.table(text = "label  id_row ID    Well  `WT_group_1` `WT_group_2` `WT_group_3`
well_1    169 PBS   H1                   2728.              2360.              2670
well_2    361 PBS   H1                   2688               2418               NA
well_3    553 PBS   H1                   3221               2835               3247
well_4    745 PBS   H1                   2838               2832.              2975
well_5    937 PBS   H1                   2857               NA               2637
well_6   1129 PBS   H1                   2834               2287               2713
well_7   1189 PBS   B7                    2374              2444               NA",
header = TRUE)

df %>%
  group_by(grp = rep(1:nrow(.), each = 2)[1:nrow(.)]) %>%
  mutate(across(contains("WT"), ~vctrs::vec_fill_missing(.x, direction = "downup"))) %>%
  select(-grp)
#> Adding missing grouping variables: `grp`
#> # A tibble: 7 × 8
#> # Groups:   grp [4]
#>     grp label  id_row ID    Well  X.WT_group_1. X.WT_group_2. X.WT_group_3.
#>   <int> <chr>   <int> <chr> <chr>         <dbl>         <dbl>         <int>
#> 1     1 well_1    169 PBS   H1             2728          2360          2670
#> 2     1 well_2    361 PBS   H1             2688          2418          2670
#> 3     2 well_3    553 PBS   H1             3221          2835          3247
#> 4     2 well_4    745 PBS   H1             2838          2832          2975
#> 5     3 well_5    937 PBS   H1             2857          2287          2637
#> 6     3 well_6   1129 PBS   H1             2834          2287          2713
#> 7     4 well_7   1189 PBS   B7             2374          2444            NA

创建于2024-02-22,共reprex v2.1.0

(我在第7行添加了NA,以表明它不会被替换;这种方法适用于您的实际数据吗?)

R相关问答推荐

将一个载体的值相加,直到达到另一个载体的值

在ggplot Likert条中添加水平线

以R中的正确顺序将日期时间字符列转换为posixct

在特定Quarto(reveal.js)幻灯片上隐藏徽标

如何动态更新selectizeInput?

在连续尺度上转置标签[瀑布图,R]

如果某些列全部为NA,则更改列

如何将SAS数据集的列名和列标签同时包含在r中GT表的表首?

当我添加美学时,geom_point未对齐

从多个可选列中选取一个值到一个新列中

R -使用矩阵reshape 列表

R中Gamma回归模型均方误差的两种计算方法不一致

在散点图中使用geom_point放置线图例

按两个因素将观测值分组后计算单独的百分比

如何判断代码是否在R Markdown(RMD)上下文中交互运行?

隐藏基于 case 总数的值

使用其他DF中的文件名将列表中的每个元素保存到文件中

reshape 数据帧-基于组将行转换为列

如何在一个GGPLATE中绘制多个灰度平滑?

真实世界坐标的逆st_变换