用同一列的单个值替换列的 NA

replace NAs of a column with the single value of the same column

我对 R 还是个新手,正在为一些可能非常简单的事情而苦苦挣扎。 我有以下数据框:

df = data.frame (trial_number  = c("41", "61", "141", "161"),
                 participant_id = c("sub-x", "sub-x","sub-x", "sub-x"),
                 B3 = c("1809.154","NA","NA", "NA"),
                 B4 = c("NA","1442.476","NA", "NA"),
                 B6 = c("NA","NA","1174.818", "NA"),
                 B7 = c("NA","NA","NA", "909.5714"))

我希望对于每一列,NA 都替换为该列中给出的单个值。 IE。我想获得以下资料:

df = data.frame (trial_number  = c("41", "61", "141", "161"),
                 participant_id = c("sub-x", "sub-x","sub-x", "sub-x"),
                 B3 = c("1809.154","1809.154","1809.154", "1809.154"),
                 B4 = c("1442.476","1442.476","1442.476", "1442.476"),
                 B6 = c("1174.818","1174.818","1174.818", "1174.818"),
                 B7 = c("909.5714","909.5714","909.5714", "909.5714"))

我该怎么做?

非常感谢您的回复!

您可以使用 for 循环:

for(k in 3:6) df[,k] <- df[,k][which(df[,k]!= "NA")]

  trial_number participant_id       B3       B4       B6       B7
1           41          sub-x 1809.154 1442.476 1174.818 909.5714
2           61          sub-x 1809.154 1442.476 1174.818 909.5714
3          141          sub-x 1809.154 1442.476 1174.818 909.5714
4          161          sub-x 1809.154 1442.476 1174.818 909.5714

函数的作用:

  1. which(df[,k]!= "NA") 查找 location k-th 列中任何不等于“NA”的值。例如,在第 5 列中,值位于第三行,因此此行:which(df[,5]!= "NA") 将 return 3
  2. df[,k][which(df[,k]!= "NA")] returns 不是“NA”的值。
  3. df[,k] <- 将值分配给 k-th 列的所有元素。
  4. for(k in 3:6) 将 3 到 6 分配给 k,然后将上面的步骤 1 到 3 应用到循环中的第 3 到第 6 列。

这是 tidyr::fill() 的典型案例。

library(tidyr)

fill(df, B3:B6, .direction = "updown")

这是一个仅 dyplr 的方法,使用 acrossi_felse 语句:

library(dplyr)

df %>%   
  mutate(across(B3:B7, ~if_else(. == "NA", min(.), .)))
  trial_number participant_id       B3       B4       B6       B7
1           41          sub-x 1809.154 1442.476 1174.818 909.5714
2           61          sub-x 1809.154 1442.476 1174.818 909.5714
3          141          sub-x 1809.154 1442.476 1174.818 909.5714
4          161          sub-x 1809.154 1442.476 1174.818 909.5714