gpt4 book ai didi

将 NA 替换为 R 中相邻列中的值

转载 作者:行者123 更新时间:2023-12-02 21:16:35 25 4
gpt4 key购买 nike

我想在必要时将 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

最佳答案

如果只有一个值不是 NA其中IMILEFTIMIRIGHT (如您的示例),只需尝试( 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))

关于将 NA 替换为 R 中相邻列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30272080/

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