2015-05-16 94 views
1

我希望在必要時(例如第1,6,7行)將IMIAVG列中的NA替換爲同一行中IMILEFT或IMIRIGHT列中的值。我嘗試過很多東西,但似乎沒有任何工作。這是否需要循環?請注意錯誤不斷出現原子向量。謝謝!用R中相鄰列中的值替換NA

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 

回答

4

如果只有一個值不NA之間IMILEFTIMIRIGHT(如你的例子),只要嘗試(df是你data.frame):

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

順便說一句,如果你想找到每行的平均值並排除過程中的值NA,則可以在函數rowMeans中將na.rm參數設置爲TRUE。我想你可以得到你的最後一列,只是:

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)) 
+0

您好感謝@nicola。所以只有在左欄中有值時才改變NA。關於如何爲RIGHT列做太多或分開的任何想法? – Jackie

+0

不,這應該改變'IMILEFT'和'IMIRIGHT'之間'IMIAVG'列的值不是'NA'。它適用於樣本數據,也許你的數據有點不同。 – nicola

+0

謝謝!!!!! @nicola !!!!! rowmeans完全工作!!!!!我一直試圖得到這個好幾天!謝謝! – Jackie

0
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 
        ) 
     ) 
1

你也可以使用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)]