用 R 中相邻列中的值替换 NA

replacing NA with value in adjacent column in R

我想在必要时将 IMIAVG 列中的 NA 替换为同一行中 IMILEFT 或 IMIRIGHT 列中的值(即第 1、6、7 行)。我尝试了多种方法,但似乎没有任何效果。这需要循环吗?请注意错误不断出现在原子向量上。谢谢!

  IMILEFT       IMIRIGHT       IMIAVG
  NA            71.15127         NA
  72.18310      72.86607      72.52458
  70.61460      68.00766      69.31113
  69.39032      69.91261      69.65146
  72.58609      72.75168      72.66888
  70.85714         NA            NA
  NA            69.88203         NA
  74.47109      73.07963      73.77536
  70.44855      71.28647      70.86751
  NA            72.33503         NA
  69.82818      70.45144      70.13981
  68.66929      69.79866      69.23397
  72.46879      71.50685      71.98782
  71.11888      71.98336      71.55112
  NA            67.86667         NA

如果 IMILEFTIMIRIGHT 中只有一个值不是 NA(如您的示例),请尝试(df 是您的 data.frame ):

indx<-is.na(df$IMIAVG)
df$IMIAVG[indx]<-rowSums(df[indx,1:2],na.rm=TRUE)

顺便说一句,如果你想找到每行的平均值并排除过程中的NA值,你可以在函数中将na.rm参数设置为TRUE rowMeans。我想您可以获得最后一列:

rowMeans(df[,1:2],na.rm=TRUE)

从根本上解决问题。

数据

df<-structure(list(IMILEFT = c(NA, 72.1831, 70.6146, 69.39032, 72.58609, 
70.85714, NA, 74.47109, 70.44855, NA, 69.82818, 68.66929, 72.46879, 
71.11888, NA), IMIRIGHT = c(71.15127, 72.86607, 68.00766, 69.91261, 
72.75168, NA, 69.88203, 73.07963, 71.28647, 72.33503, 70.45144, 
69.79866, 71.50685, 71.98336, 67.86667), IMIAVG = c(NA, 72.52458, 
69.31113, 69.65146, 72.66888, NA, NA, 73.77536, 70.86751, NA, 
70.13981, 69.23397, 71.98782, 71.55112, NA)), .Names = c("IMILEFT", 
"IMIRIGHT", "IMIAVG"), class = "data.frame", row.names = c(NA, 
-15L))
df <- read.table(text = "IMILEFT       IMIRIGHT       IMIAVG
  NA            71.15127         NA
  72.18310      72.86607      72.52458
  70.61460      68.00766      69.31113
  69.39032      69.91261      69.65146
  72.58609      72.75168      72.66888
  70.85714         NA            NA
  NA            69.88203         NA
  74.47109      73.07963      73.77536
  70.44855      71.28647      70.86751
  NA            72.33503         NA
  69.82818      70.45144      70.13981
  68.66929      69.79866      69.23397
  72.46879      71.50685      71.98782
  71.11888      71.98336      71.55112
  NA            67.86667         NA" , header = T)

library("dplyr")

    df %>%
  mutate(
    IMIAVG = ifelse(
                      is.na(IMIAVG) , 
                      ifelse(is.na(IMIRIGHT) ,IMILEFT ,IMIRIGHT  ) , 
                      IMIAVG
                   )
         )

您也可以使用 pmax

indx <- is.na(df$IMIAVG)
df$IMIAVG[indx] <- do.call(pmax, c(df[indx, 1:2], na.rm=TRUE))

或使用data.table

library(data.table) 
setDT(df)[is.na(IMIAVG), IMIAVG:=pmax(IMILEFT, IMIRIGHT, na.rm=TRUE)]