通过从总数中减去其他列的总和,有条件地替换一列中的 NA

Conditionally replace NA in one column by subtracting the sum of other columns from the total

更新:

dat1 <- dat %>% 
   mutate(across(starts_with("Number"), ~case_when(is.na(.) ~
         Total - sum(.[complete.cases(.)]), TRUE ~ .)))

进行了计算,但结果不正确。如果计算正确,第一行应该是 2, 0, 0, 2TotalNumber_end_u .

head(dat1)
        ID Total Number_end_f Number_end_m Number_end_u
1 14_E_3_6     2        -1029         -678            2
2 32_E_6_6     2            2         -678         -417
3 32_W_8_6     1        -1030         -679            1
4 32_E_8_4    12        -1019            2           10
5 32_E_7_4     4            4         -676         -415
6 32_E_6_4     1            1         -679         -418

此代码

dat1 <- dat %>% 
   mutate(across(starts_with("Number"), ~case_when(is.na(.) ~
         sum(.[complete.cases(.)]) - Total, TRUE ~ .)))

产生

head(dat1)
        ID Total Number_end_f Number_end_m Number_end_u
1 14_E_3_6     2         1029          678            2
2 32_E_6_6     2            2          678          417
3 32_W_8_6     1         1030          679            1
4 32_E_8_4    12         1019            2           10
5 32_E_7_4     4            4          676          415
6 32_E_6_4     1            1          679          418

原始问题:

Number_end_f + Number_end_m + Number_end_u = Total 在我的数据集中。我想通过 Total 中减去非 NA 的总和来替换 NA 的 some。这里有2个案例:

一类是 NA:通过从 Total 中减去其他两列的总和来填充 NA。

两个类别不适用:

这是我基于这个问题的粗略代码思路 and this problem R: selecting rows that contain a given number of NAs

    dat2 <- dat %>%
      mutate(is.na(.[,c(3:5)]) = Total - !is.na(.[,c(3:5)]))
    Error: unexpected '=' in:
    "dat2 <- dat %>%
      mutate(is.na(.[,c(3:5)]) ="

dat2 <- dat %>%
  mutate(across(starts_with("Number"),
                ~replace(., rowsum(is.na(.), Total - !is.na(.)))))
Error: Problem with `mutate()` input `..1`.
x argument "values" is missing, with no default
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Run `rlang::last_error()` to see where the error occurred.

和数据

dput(dat)
structure(list(ID = c("14_E_3_6", "32_E_6_6", "32_W_8_6", "32_E_8_4", 
"32_E_7_4", "32_E_6_4", "39_E_6_3", "22_W_2_3", "22_E_4_3", "22_E_2_3", 
"35_E_4_2", "22_E_6_3", "19_E_6_2", "19_E_5_2", "19_E_3_2", "35_E_3_2", 
"35_E_5_2", "17_W_6_3", "41_W_5_3", "22_E_3_3", "41_W_6_3", "17_E_4_3", 
"17_W_7_3", "19_E_7_2", "35_E_2_2", "19_E_1_2", "22_E_1_3", "17_W_3_3", 
"45_E_7_3", "41_E_1_3", "19_W_8_2", "19_W_5_2", "45_W_6_2", "17_W_5_3", 
"22_W_3_3", "27_E_1_3", "23_E_4_4", "45_E_8_3", "45_E_5_3", "48_E_3_4", 
"17_E_3_3", "17_W_2_3", "27_W_2_3", "38_W_2_4", "42_W_7_3", "11_E_5_4", 
"16_E_2_3", "16_E_4_3", "16_E_6_3", "17_E_1_3", "21_W_2_3", "38_E_1_4", 
"38_W_8_4", "11_W_5_4", "16_E_5_3", "21_W_6_3", "21_W_7_3", "23_W_5_4", 
"27_E_7_3", "27_W_5_3", "45_E_1_3", "45_W_1_3", "35_E_6_2", "14_E_6_1", 
"14_E_5_1", "14_E_3_1", "14_E_4_1", "14_E_7_1", "17_W_2_4", "32_E_6_1", 
"17_W_5_4", "45_E_6_6", "33_E_3_6", "33_W_3_6", "42_W_2_6", "49_W_8_3", 
"32_E_8_3", "49_E_3_3", "49_E_6_3", "32_W_3_3", "29_W_6_3", "49_E_4_3", 
"49_W_4_3", "29_E_5_3", "29_W_1_3", "29_E_7_3", "32_W_7_3", "49_W_7_3", 
"32_W_5_3", "32_W_6_3", "14_E_1_3", "49_W_2_3", "14_E_8_3", "32_W_4_3", 
"49_E_7_3", "49_W_6_3", "49_E_1_3", "29_W_5_3", "49_W_5_3", "45_E_7_4", 
"14_E_4_3", "45_W_8_2", "32_E_2_3", "29_W_4_3", "29_W_7_3", "32_E_4_3", 
"14_W_3_3", "29_W_8_3", "32_E_1_3", "32_E_3_3", "49_E_2_3", "49_E_5_3", 
"14_E_6_3", "14_E_7_3", "33_E_2_4", "35_E_2_4", "45_W_1_4", "45_W_5_4", 
"35_E_1_4", "35_E_5_4", "19_E_3_4", "21_W_2_4", "25_E_1_4", "25_E_5_4", 
"25_W_2_4", "25_W_3_4", "25_W_4_4", "31_W_3_4", "33_W_7_4", "35_W_1_4", 
"42_W_3_4", "45_E_8_4", "45_W_6_4", "19_E_1_4", "19_W_1_2", "25_E_2_4", 
"25_E_4_4", "25_E_8_4", "25_W_8_4", "31_E_3_4", "33_E_7_4", "33_E_8_4", 
"33_W_3_4", "35_E_3_4", "42_W_6_4", "45_E_2_4", "29_W_7_1", "29_W_5_1", 
"39_W_3_4", "29_W_8_1", "19_E_1_1", "33_E_1_2", "33_E_7_2", "17_W_4_1", 
"33_E_8_2", "33_W_2_2", "42_W_3_2", "33_E_2_2", "16_E_4_2", "16_E_1_2", 
"16_E_2_2", "42_E_7_2", "42_W_5_2", "42_W_7_2", "35_W_1_2", "25_E_1_2", 
"35_W_2_2", "42_E_8_2", "17_W_5_1", "42_W_4_2", "25_E_4_2", "42_E_6_2", 
"25_W_1_2", "42_E_4_2", "42_E_3_2", "42_E_5_2", "42_W_1_2", "41_E_8_4", 
"41_W_5_4", "42_W_2_2", "42_W_8_2", "25_W_3_2", "33_E_4_2", "25_E_5_2", 
"16_W_5_2", "31_E_1_2", "33_E_6_2", "33_W_1_2", "42_E_1_2", "12_E_8_2", 
"16_E_5_2", "16_W_1_2", "16_W_3_2", "25_W_4_2", "33_E_5_2", "39_W_2_4", 
"45_W_8_4", "12_E_4_2", "16_E_6_2", "16_W_4_2", "25_E_8_2", "42_E_2_2", 
"12_W_2_2", "16_W_2_2", "17_W_2_1", "21_W_2_2", "27_E_1_4", "27_W_3_4", 
"27_W_7_4", "17_W_6_1", "38_W_7_1", "38_W_1_1", "38_E_8_1", "19_E_5_1", 
"17_E_8_2", "19_E_4_1", "17_E_5_2", "48_E_3_1", "27_E_8_1", "38_W_2_1", 
"17_W_4_2", "48_W_7_1", "38_E_7_1", "48_E_2_1", "17_E_3_2", "16_E_4_1", 
"19_E_6_1", "14_E_1_2", "38_E_1_1", "41_E_2_2", "38_W_3_1", "14_E_2_2", 
"25_W_4_1", "48_W_6_1", "11_W_8_1", "29_W_7_2", "39_W_4_2", "25_W_5_1", 
"11_W_5_1", "29_W_5_2", "14_W_3_2", "11_E_3_1", "16_E_3_1", "17_W_7_2", 
"11_W_2_1", "49_W_8_2", "23_W_4_1", "29_W_4_2", "23_W_7_1", "38_W_4_1", 
"29_E_6_2", "48_W_2_1", "38_E_2_1", "39_E_6_2", "32_E_7_2", "35_W_5_1", 
"23_W_5_1", "32_W_5_2", "38_E_5_1", "11_W_1_1", "11_W_4_1", "16_E_2_1", 
"33_E_2_1", "14_E_8_2", "38_W_8_1", "49_E_3_2", "11_E_1_1", "41_E_1_2", 
"19_E_8_1", "32_W_8_2", "32_E_6_2", "22_E_4_2", "23_W_1_1", "29_E_8_2", 
"32_W_3_2", "23_W_8_1", "39_W_1_2", "48_W_1_1", "23_W_2_1", "29_E_5_2", 
"11_E_5_1", "14_E_3_2", "14_W_2_2", "33_W_2_1", "49_E_6_2", "22_E_2_2", 
"23_W_3_1", "27_W_6_1", "41_E_8_2", "27_W_3_1", "22_E_6_2", "29_E_7_2", 
"41_W_2_2", "19_W_3_1", "22_W_2_2", "11_E_2_1", "49_W_5_2", "25_E_3_1", 
"32_E_5_2", "35_E_2_1", "41_E_4_2", "29_W_8_2", "11_E_8_1", "17_W_3_2", 
"19_W_1_1", "11_E_4_1", "33_W_6_1", "14_W_4_2", "35_E_1_1", "17_E_4_2", 
"33_E_7_1", "25_W_1_1", "35_E_6_1", "16_E_6_1", "22_E_1_2", "25_W_2_1", 
"23_W_6_1", "42_W_7_1", "19_E_7_1", "32_W_4_2", "38_W_6_1", "49_E_8_2", 
"27_W_5_1", "41_E_3_2", "25_W_3_1", "33_E_1_1", "35_E_4_1", "39_E_1_2", 
"16_W_3_1", "25_E_2_1", "42_W_4_1", "42_W_5_1", "41_W_4_2", "16_W_2_1", 
"32_E_3_2", "39_W_3_2", "16_E_1_1", "25_E_1_1", "25_E_6_1", "33_W_3_1", 
"27_W_2_1", "38_W_5_1", "14_E_5_2", "22_W_3_2", "16_W_7_1", "35_E_5_1", 
"41_W_5_2", "42_W_3_1", "33_E_3_1", "33_W_1_1", "49_E_5_2", "12_E_8_1", 
"17_E_1_2", "21_E_7_1", "25_E_8_1", "42_W_2_1", "45_E_5_1", "45_E_8_1", 
"16_W_1_1", "45_E_6_1", "45_E_7_1", "16_W_8_1", "17_W_6_2", "21_E_5_1", 
"12_E_1_1", "21_E_4_1", "31_W_2_1", "39_E_7_1", "39_W_2_1", "39_E_8_1", 
"39_E_6_1", "29_E_8_5", "29_E_6_5", "29_E_7_5", "29_W_5_5", "29_W_6_5", 
"49_E_1_5", "29_E_4_5", "49_E_7_5", "29_W_7_5", "49_W_1_5", "49_W_4_5", 
"35_E_2_5", "14_W_2_5", "14_E_2_5", "14_W_4_5", "35_E_8_2", "23_W_5_2", 
"38_W_8_2", "11_E_3_2", "48_W_4_2", "35_W_6_2", "48_E_3_2", "41_E_6_4", 
"48_E_1_2", "45_E_8_2", "35_W_3_2", "38_E_8_2", "35_W_7_2", "11_W_5_2", 
"11_W_8_2", "48_W_2_2", "35_E_7_2", "45_W_1_2", "35_W_5_2", "41_E_5_4", 
"22_E_3_4", "22_E_6_4", "41_E_3_4", "22_E_2_4", "19_W_6_2", "23_W_1_2", 
"17_W_6_5", "23_W_4_2", "38_W_3_2", "41_E_1_4", "41_E_2_4", "17_W_2_5", 
"45_E_5_2", "49_E_3_1", "49_E_7_1", "49_E_6_1", "49_W_7_1", "49_W_5_1", 
"49_E_8_1", "49_W_6_1", "49_E_2_1", "49_W_8_1", "27_E_3_2", "27_W_6_2", 
"23_W_5_3", "27_E_4_2", "49_W_1_1", "49_W_4_1", "49_W_3_1", "11_E_3_3", 
"48_W_6_3", "27_E_7_2", "33_E_8_3", "49_E_5_1", "14_W_1_4", "49_E_1_1", 
"27_W_4_2", "27_W_7_2", "27_W_8_2", "33_E_7_3", "45_W_4_3", "45_W_7_3", 
"11_E_2_3", "45_W_3_3", "29_W_7_4", "48_W_2_3", "14_E_2_3", "29_E_1_4", 
"32_W_7_4", "39_E_6_5", "33_E_2_3", "42_E_3_3", "45_E_4_2", "45_W_6_3", 
"25_W_4_3", "27_W_2_2", "29_W_4_4", "45_W_5_3", "45_W_8_3", "33_W_2_3", 
"35_E_2_3", "38_W_3_3", "35_E_3_3", "45_W_2_3", "42_W_3_3", "45_E_1_2", 
"25_W_3_3", "35_E_5_3", "35_E_6_3", "35_W_6_3", "14_W_3_4", "19_E_7_3", 
"27_W_3_2", "27_W_5_2", "29_W_5_4", "29_W_8_4", "38_E_8_3", "41_E_7_5", 
"42_W_4_3", "19_E_2_3", "22_E_2_5", "22_E_3_5", "25_E_4_3", "25_E_5_3", 
"35_E_4_3", "35_E_8_3", "35_W_5_3", "42_E_5_3", "42_W_5_3", "19_E_5_3", 
"22_E_1_5", "25_E_3_3", "39_W_1_5", "42_E_7_3", "42_W_2_3", "25_W_5_3", 
"33_W_8_3", "35_E_7_3", "41_W_6_5", "11_E_4_3", "12_E_5_3", "14_W_5_4", 
"19_E_4_3", "19_W_7_3", "31_W_2_3", "35_W_3_3", "35_W_7_3", "41_W_8_5", 
"42_W_1_3", "43_W_4_3", "12_E_3_3", "19_W_6_3", "25_E_2_3", "25_E_6_3", 
"31_E_5_3", "33_E_4_3", "33_W_3_3", "39_W_2_5", "32_E_8_1", "14_W_3_1", 
"14_W_1_1", "14_E_1_1", "14_W_4_1", "32_E_1_1", "32_E_7_1", "14_W_6_1", 
"32_E_4_1", "32_E_2_1", "32_W_2_1", "14_W_5_1", "14_W_7_1", "32_E_3_1", 
"32_W_5_1", "32_W_6_1", "32_W_3_1", "32_W_4_1", "22_E_3_1", "32_E_5_1", 
"29_W_4_1", "29_E_5_1", "29_E_6_1", "29_E_7_1", "29_E_2_1", "49_E_6_4", 
"17_E_4_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 2L, 1L, 4L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 
5L, 3L, 3L, 2L, 2L, 1L, 3L, 4L, 3L, 1L, 1L, 3L, 2L, 1L, 3L, 1L, 
4L, 4L, 1L, 4L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 24L, 2L, 
19L, 12L, 4L, 22L, 7L, 8L, 1L, 2L, 2L, 5L, 2L, 1L, 5L, 2L, 6L, 
2L, 7L, 16L, 7L, 15L, 16L, 1L, 2L, 1L, 1L, 1L, 6L, 3L, 3L, 1L, 
1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L), Number_end_f = c(NA, 2L, NA, 
NA, 4L, 1L, 8L, 10L, 10L, 4L, NA, 13L, NA, 1L, NA, NA, 1L, 3L, 
10L, 1L, 6L, 10L, NA, NA, NA, NA, 3L, 3L, NA, 1L, NA, NA, 1L, 
3L, NA, NA, 2L, NA, NA, 3L, NA, 2L, NA, 1L, NA, 1L, NA, NA, NA, 
1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 3L, 1L, 
22L, 4L, NA, 6L, 5L, NA, NA, NA, NA, NA, 4L, 8L, 4L, NA, 2L, 
2L, 2L, 3L, 7L, NA, 1L, NA, 1L, 3L, NA, 2L, 1L, 3L, 1L, 2L, 2L, 
3L, 8L, NA, NA, 4L, NA, 1L, NA, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 
NA, NA, 9L, 13L, 19L, 5L, 3L, NA, NA, 3L, 1L, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, 3L, NA, NA, 4L, 1L, NA, NA, 2L, NA, NA, 
NA, NA, NA, 1L, 1L, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, 
NA, NA, 5L, 5L, 5L, 7L, 4L, 2L, 2L, 1L, NA, NA, 3L, 7L, NA, 5L, 
1L, 7L, 3L, 1L, 8L, 1L, 7L, 4L, 7L, 1L, 1L, NA, NA, 1L, NA, 10L, 
15L, 17L, 3L, NA, 3L, NA, 2L, 5L, 2L, NA, 3L, 1L, 4L, 5L, 2L, 
4L, 1L, 7L, NA, 3L, 4L, 2L, NA, NA, 9L, 2L, 1L, 1L, 2L, 5L, 3L, 
5L, 12L, NA, 5L, 4L, NA, 2L, 2L, 1L, 1L, 1L, 7L, 3L, 4L, 3L, 
2L, NA, 1L, 1L, 6L, 5L, 5L, NA, NA, 6L, 7L, 1L, 2L, 5L, NA, 2L, 
1L, 2L, 10L, NA, 6L, NA, 2L, 2L, 16L, NA, 1L, 1L, NA, NA, 2L, 
3L, 1L, 5L, 1L, NA, 1L, NA, 1L, 1L, NA, 1L, 1L, NA, 1L, 1L, NA, 
6L, 2L, NA, NA, 1L, 2L, 1L, 1L, 3L, 3L, 1L, NA, 2L, 1L, 1L, 2L, 
NA, NA, 1L, 1L, 2L, 2L, 1L, NA, 2L, 2L, NA, 1L, NA, NA, NA, 1L, 
NA, 1L, 1L, 2L, 2L, 1L, 1L, NA, 1L, 1L, 1L, NA, 3L, 1L, NA, 1L, 
NA, NA, NA, 2L, 1L, NA, NA, 12L, NA, 2L, 4L, 3L, NA, 1L, 1L, 
NA, NA, 1L, NA, 2L, 1L, 3L, NA, NA, NA, NA, 6L, 2L, NA, 2L, 3L, 
1L, NA, 3L, NA, NA, 1L, 1L, 12L, 11L, 7L, 1L, 3L, 7L, 4L, 4L, 
2L, 1L, 1L, 1L, NA, NA, 2L, 1L, 4L, NA, 1L, NA, NA, 12L, 11L, 
NA, 2L, 2L, NA, NA, NA, NA, NA, NA, 2L, NA, 10L, 1L, NA, 2L, 
NA, NA, 4L, NA, 3L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, 1L, 1L, NA, 5L, 3L, 2L, 2L, NA, NA, 2L, 3L, 
NA, NA, NA, NA, NA, NA, 2L, NA, 1L, NA, 1L, NA, 2L, NA, NA, NA, 
1L, 1L, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, 1L, 6L, 14L, 2L, 11L, 7L, 1L, 10L, 5L, 6L, 1L, NA, 
2L, 2L, 1L, 1L, NA, 2L, 4L, 2L, 5L, 10L, 6L, 10L, 8L, 1L, NA, 
NA, 1L, NA, 3L, 3L, NA, 1L, 1L, 2L, NA, 1L, NA, NA, NA, NA), 
    Number_end_m = c(NA, NA, NA, 2L, NA, NA, 9L, 1L, NA, 2L, 
    NA, 10L, 1L, NA, 1L, NA, NA, 2L, 2L, 1L, 2L, 10L, 1L, 2L, 
    NA, 1L, 3L, 2L, NA, NA, NA, NA, 2L, 1L, 1L, NA, NA, NA, NA, 
    2L, 4L, 1L, NA, NA, 1L, NA, NA, NA, NA, 1L, 1L, 1L, 1L, 1L, 
    NA, NA, NA, 1L, NA, NA, NA, NA, 2L, NA, NA, NA, 5L, 3L, 5L, 
    3L, 1L, NA, NA, NA, NA, 4L, 6L, 6L, 1L, 5L, NA, 2L, 2L, 6L, 
    1L, 1L, 3L, NA, 2L, 1L, 2L, 1L, 5L, 2L, 2L, NA, NA, 3L, 3L, 
    NA, 1L, 3L, NA, 1L, NA, NA, 1L, NA, NA, 3L, NA, NA, NA, 1L, 
    NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 2L, 9L, 4L, 4L, NA, 2L, 4L, 1L, NA, 4L, NA, NA, 2L, 
    1L, NA, 1L, NA, 2L, 1L, NA, NA, 1L, NA, NA, 1L, NA, 1L, NA, 
    NA, NA, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, 2L, 2L, 1L, NA, 
    NA, NA, NA, NA, 1L, 1L, 1L, NA, 1L, NA, NA, 1L, NA, NA, NA, 
    NA, NA, NA, NA, 1L, NA, 2L, NA, NA, 2L, 1L, 1L, NA, 1L, 1L, 
    2L, 2L, 1L, 1L, NA, 7L, NA, 1L, NA, NA, 2L, 8L, 9L, NA, NA, 
    1L, 3L, NA, 1L, 7L, 5L, 3L, NA, 1L, 3L, 3L, NA, 2L, NA, 2L, 
    2L, 1L, 1L, 3L, NA, 7L, NA, 10L, 1L, NA, 5L, NA, NA, 4L, 
    16L, 2L, NA, NA, 2L, 2L, 3L, 1L, 1L, 1L, NA, 1L, 1L, 2L, 
    1L, NA, 5L, NA, 18L, NA, 1L, 1L, 1L, 1L, 2L, NA, 3L, 11L, 
    7L, 1L, NA, 4L, 2L, 1L, 3L, 3L, NA, NA, NA, NA, 3L, NA, 4L, 
    NA, NA, 2L, 8L, 1L, NA, NA, NA, 3L, NA, NA, 2L, 6L, NA, 2L, 
    NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 1L, 2L, 1L, 
    NA, 2L, NA, NA, 3L, NA, NA, 1L, 3L, 2L, NA, 1L, 1L, 1L, 1L, 
    NA, NA, 1L, NA, 2L, 3L, NA, 1L, NA, 1L, 1L, 1L, NA, 1L, NA, 
    NA, 2L, 1L, NA, NA, 4L, NA, NA, NA, 1L, NA, NA, 1L, NA, 1L, 
    1L, NA, NA, NA, NA, NA, 3L, 1L, 3L, 1L, NA, 1L, NA, NA, 4L, 
    NA, NA, 1L, NA, NA, NA, NA, 2L, 1L, 2L, NA, NA, 1L, NA, NA, 
    NA, 1L, NA, 1L, 1L, NA, NA, 12L, 6L, 1L, NA, 1L, 2L, NA, 
    1L, 4L, NA, NA, 1L, 1L, 1L, NA, NA, NA, 1L, NA, 1L, 4L, 5L, 
    NA, 4L, NA, NA, NA, 2L, NA, 1L, 1L, 4L, 1L, 1L, NA, NA, NA, 
    NA, NA, NA, 2L, NA, NA, NA, NA, NA, 1L, 2L, NA, 2L, NA, 1L, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, NA, NA, 
    NA, NA, 1L, NA, NA, NA, NA, 1L, NA, 1L, NA, 3L, NA, NA, 2L, 
    1L, 1L, NA, NA, NA, NA, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, 
    NA, 1L, NA, NA, NA, NA, NA, NA, NA, 2L, 10L, NA, 8L, 5L, 
    3L, 12L, 2L, 2L, NA, 2L, NA, 3L, 1L, NA, 5L, NA, NA, NA, 
    2L, 6L, 1L, 5L, 8L, NA, 2L, 1L, NA, 1L, 3L, NA, 3L, NA, NA, 
    NA, NA, NA, NA, NA, 1L, NA), Number_end_u = c(2L, NA, 1L, 
    10L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 3L, 6L, 
    NA, NA, NA, NA, NA, NA, 1L, 3L, NA, NA, NA, 3L, NA, 1L, 1L, 
    NA, NA, NA, 1L, NA, 1L, 1L, NA, NA, NA, 1L, NA, NA, NA, 1L, 
    1L, 2L, NA, NA, NA, NA, NA, 1L, 1L, 1L, NA, 1L, 1L, NA, 1L, 
    13L, 4L, NA, 1L, NA, NA, NA, NA, NA, 2L, 1L, 1L, 1L, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, 7L, NA, 2L, NA, NA, NA, NA, 
    NA, NA, 1L, NA, NA, NA, 1L, NA, 3L, 2L, 1L, 3L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 1L, 1L, NA, 1L, 1L, 1L, NA, 2L, 1L, NA, 1L, 
    NA, 1L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, NA, NA, NA, 
    9L, 5L, NA, 7L, 6L, 8L, 4L, 4L, 1L, 2L, 3L, 6L, 9L, NA, 1L, 
    3L, 4L, NA, NA, 2L, 5L, NA, 1L, 4L, 1L, 2L, NA, NA, 1L, 4L, 
    3L, 2L, NA, 4L, NA, 1L, NA, 1L, 2L, 3L, 1L, 1L, 2L, 2L, NA, 
    3L, NA, 1L, 2L, NA, 1L, 1L, 1L, NA, 1L, 1L, 1L, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 
    NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 2L, NA, 
    NA, NA, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, 
    NA, NA, NA, NA, NA, NA, 1L, NA, NA, 1L, NA, NA, 2L, NA, NA, 
    NA, NA, NA, 2L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, 1L, NA, NA, 1L, NA, NA, 1L, 1L, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, 2L, NA, NA, NA, NA, 
    NA, NA, NA, NA, 2L, 1L, NA, 5L, NA, NA, NA, 1L, NA, NA, NA, 
    NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA, 
    NA, 1L, 8L, NA, NA, NA, NA, 2L, 1L, 9L, 5L, 6L, NA, 4L, NA, 
    NA, NA, NA, 4L, NA, 3L, 5L, NA, 3L, 2L, NA, NA, 10L, 2L, 
    1L, 4L, NA, 4L, 2L, 2L, 1L, 4L, 1L, 2L, 1L, 2L, 1L, 1L, NA, 
    NA, NA, NA, NA, 2L, 1L, 1L, NA, 3L, 1L, 1L, 3L, 1L, 1L, NA, 
    1L, NA, 4L, NA, 2L, NA, 1L, 2L, 1L, NA, NA, NA, NA, 1L, NA, 
    1L, 1L, 1L, NA, 2L, 1L, NA, 1L, 1L, 1L, 1L, 1L, 1L, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, 2L, NA, 1L, 1L, NA, 1L)), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 9L, 11L, 13L, 14L, 15L, 17L, 18L, 20L, 21L, 
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 
36L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 
50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 
63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 74L, 75L, 76L, 77L, 
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 
93L, 94L, 95L, 96L, 97L, 98L, 100L, 101L, 102L, 103L, 104L, 105L, 
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 127L, 128L, 
129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 
140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 
151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 
162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 
173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 
184L, 185L, 187L, 188L, 189L, 191L, 192L, 193L, 194L, 195L, 196L, 
197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 205L, 206L, 207L, 
208L, 209L, 210L, 212L, 213L, 214L, 215L, 216L, 217L, 218L, 219L, 
220L, 221L, 222L, 223L, 224L, 225L, 226L, 227L, 228L, 229L, 231L, 
232L, 233L, 234L, 236L, 237L, 238L, 239L, 240L, 241L, 242L, 244L, 
246L, 248L, 249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 
258L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 
270L, 271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 
281L, 282L, 284L, 285L, 286L, 287L, 289L, 290L, 291L, 292L, 294L, 
296L, 297L, 298L, 300L, 301L, 302L, 303L, 304L, 305L, 306L, 307L, 
308L, 309L, 310L, 311L, 312L, 313L, 314L, 315L, 316L, 317L, 318L, 
319L, 320L, 321L, 322L, 323L, 324L, 325L, 326L, 327L, 328L, 329L, 
330L, 331L, 332L, 333L, 334L, 336L, 337L, 338L, 339L, 340L, 341L, 
342L, 343L, 344L, 346L, 347L, 348L, 349L, 351L, 352L, 353L, 354L, 
355L, 356L, 357L, 358L, 359L, 361L, 362L, 363L, 364L, 365L, 366L, 
367L, 368L, 369L, 370L, 371L, 372L, 373L, 374L, 375L, 376L, 377L, 
378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L, 
389L, 390L, 391L, 392L, 393L, 394L, 395L, 396L, 397L, 398L, 399L, 
400L, 401L, 402L, 403L, 404L, 405L, 406L, 407L, 408L, 409L, 410L, 
411L, 412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 420L, 421L, 
422L, 423L, 424L, 425L, 426L, 427L, 428L, 429L, 431L, 432L, 433L, 
434L, 435L, 436L, 437L, 438L, 439L, 440L, 441L, 442L, 443L, 444L, 
445L, 446L, 447L, 448L, 449L, 450L, 451L, 452L, 455L, 456L, 457L, 
458L, 459L, 461L, 462L, 463L, 465L, 466L, 467L, 468L, 469L, 470L, 
471L, 472L, 473L, 474L, 475L, 476L, 478L, 480L, 481L, 482L, 483L, 
484L, 485L, 486L, 487L, 488L, 489L, 490L, 491L, 492L, 493L, 494L, 
495L, 496L, 497L, 498L, 499L, 500L, 501L, 502L, 503L, 504L, 505L, 
506L, 507L, 508L, 509L, 510L, 511L, 513L, 514L, 515L, 516L, 517L, 
518L, 519L, 520L, 521L, 522L, 524L, 525L, 526L, 527L, 528L, 529L, 
530L, 531L, 532L, 533L, 534L, 535L, 536L, 537L, 538L, 539L, 540L, 
541L, 542L, 543L, 544L, 545L, 546L, 547L, 548L, 549L, 550L, 551L, 
552L, 553L, 554L, 555L, 556L, 557L, 558L, 559L, 560L, 561L, 562L, 
563L, 564L, 565L, 566L, 567L, 569L, 570L, 572L, 573L, 574L, 575L, 
576L, 577L, 578L, 579L, 580L, 581L, 582L, 583L, 584L, 585L, 586L, 
587L, 588L, 589L, 590L, 591L, 592L, 593L, 594L, 595L, 596L, 597L, 
598L, 599L, 600L, 601L, 602L, 603L, 604L, 605L, 606L, 607L, 608L
), class = "data.frame")

如何修复我的代码?提前谢谢你。

这在 base R 中更容易。子集名称中以 'Number' 前缀开头的列,将该子集的每一行 (rowSums) 的总和与 'Total' 列进行比较(如果有一些 NA,请替换那些NA 与 FALSE,然后在该子集 ('i2') 上获得 NA 的逻辑矩阵。使用具有选定列的第一个和第二个索引将这些 NA 分配给 0

nm1 <- grep("^Number", names(dat), value = TRUE)
i1 <- rowSums(dat[nm1], na.rm = TRUE) == dat$Total
i1[is.na(i1)] <- FALSE
i2 <- is.na(dat[i1, nm1])
dat[i1, nm1][i2] <- 0

-输出

head(dat)
#        ID Total Number_end_f Number_end_m Number_end_u
#1 14_E_3_6     2            0            0            2
#2 32_E_6_6     2            2            0            0
#3 32_W_8_6     1            0            0            1
#4 32_E_8_4    12            0            2           10
#5 32_E_7_4     4            4            0            0
#6 32_E_6_4     1            1            0            0