根据具有相同 ID 的较少行中的缺失值,将 na 值分配给多行

Assign na values to multiple rows based on missing values in fewer rows with the same ID

我有一个数据框df

df<-structure(list(year = structure(c(2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 
2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002), .Dim = 365L, varnames = "year"), 
    T = structure(c(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, 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, -13.1354875564575, 
    -13.6129341125488, -12.7523603439331, -12.5383033752441, 
    -13.5281381607056, -12.8971185684204, -12.6322660446167, 
    -12.8605613708496, -13.6129341125488, -12.7523603439331, 
    -12.5383033752441, -13.5281381607056, -12.8971185684204, 
    -12.6322660446167, -12.8605613708496, -13.6129341125488, 
    -12.7523603439331, -12.5383033752441, -13.5281381607056, 
    -12.8971185684204, -12.6322660446167, -12.8605613708496, 
    -13.6129341125488, -12.7523603439331, -12.5383033752441, 
    -13.5281381607056, -12.8971185684204, -12.6322660446167, 
    -12.8605613708496, -13.6129341125488, -12.7523603439331, 
    -12.5383033752441, -13.5281381607056, -12.8971185684204, 
    -12.6322660446167, -12.8605613708496, -13.6129341125488, 
    -12.7523603439331, -12.5383033752441, -13.5281381607056, 
    -12.8971185684204, -12.6322660446167, -12.8605613708496, 
    -13.6129341125488, -12.7523603439331, -12.5383033752441, 
    -13.5281381607056, -12.8971185684204, -12.6322660446167, 
    -12.8605613708496, -13.6129341125488, -12.7523603439331, 
    -12.5383033752441, -13.5281381607056, -12.8971185684204, 
    -12.6322660446167, -12.8605613708496, -13.6129341125488, 
    -13.1387643814087, -13.1387643814087, -13.1399154663086, 
    -12.4283533096313, -12.234561920166, -14.7442922592163, -15.0792503356934, 
    -15.6122703552246, -12.7686243057251, -15.7085199356079, 
    -10.4563331604004, 4.07468748092651, 4.49679136276245, 4.71654844284058, 
    4.7201623916626, 4.7201623916626, 4.81460428237915, 4.87433099746704, 
    7.66762208938599, 7.66762208938599, 7.57682752609253, 13.2514791488647, 
    5.78852081298828, 2.89281249046326, 2.46829152107239, 1.01870834827423, 
    4.20060396194458, 9.74983310699463, 16.5035839080811, 15.1336450576782, 
    5.53631258010864, 6.45396280288696, 6.43788862228394, 6.43788862228394, 
    11.9333305358887, 20.9888668060303, 20.9888668060303, 20.9431972503662, 
    19.8988132476807, 7.96287488937378, 6.7201042175293, 7.90643739700317, 
    13.221978187561, 16.687520980835, 18.0008125305176, 17.2373752593994, 
    11.347770690918, 18.1233329772949, 10.1335000991821, 11.9143133163452, 
    12.5904579162598, 14.8801460266113, 14.7091255187988, 15.7528953552246, 
    12.478645324707, 16.5128536224365, 19.8117084503174, 20.9537086486816, 
    16.2603340148926, 17.6821250915527, 17.1010208129883, 20.5147075653076, 
    23.7007694244385, 19.0803966522217, 20.2046871185303, 24.925313949585, 
    25.5778121948242, 28.6273326873779, 27.6139163970947, 25.3842296600342, 
    23.51389503479, 16.1065196990967, 17.1792087554932, 22.4191875457764, 
    23.0452098846436, 22.286958694458, 18.835729598999, 16.8125839233398, 
    16.5770416259766, 18.6914577484131, 21.7424793243408, 23.5397090911865, 
    24.4372310638428, 25.2590198516846, 24.6128940582275, 17.8469371795654, 
    18.6824378967285, 22.8294162750244, 23.994499206543, 19.3857288360596, 
    15.0994157791138, 17.0552711486816, 17.5077075958252, 22.6148529052734, 
    20.3396244049072, 23.4169368743896, 22.3954792022705, 24.615104675293, 
    19.1652297973633, 20.1037082672119, 16.5935001373291, 15.3626050949097, 
    20.1960830688477, 16.0936870574951, 13.9411878585815, 15.381646156311, 
    19.4180202484131, 22.8893737792969, 21.1584167480469, 21.3523540496826, 
    20.5276870727539, 17.9288120269775, 19.040979385376, 18.2904586791992, 
    18.0497493743896, 16.7213115692139, 14.3521041870117, 14.1245622634888, 
    16.1998329162598, 16.478645324707, 14.9362077713013, 17.1162281036377, 
    19.9073543548584, 19.3842716217041, 18.8538951873779, 15.6980209350586, 
    18.0031661987305, 19.6169376373291, 18.5515213012695, 19.6553955078125, 
    20.0803756713867, 18.200834274292, 16.9684791564941, 15.166916847229, 
    17.4950008392334, 19.5030422210693, 24.4547901153564, 24.8403339385986, 
    23.7837085723877, 15.9861040115356, 16.5808963775635, 19.7263126373291, 
    15.068187713623, 14.8234586715698, 11.0970001220703, 16.4652709960938, 
    16.6585216522217, 17.7728538513184, 17.8136253356934, 14.3029584884644, 
    11.4533758163452, 7.61977052688599, 2.71749997138977, 6.56177091598511, 
    7.90195846557617, 6.61327123641968, 7.42372894287109, 7.73672914505005, 
    9.99620819091797, 17.6994571685791, 16.4701042175293, 5.43512487411499, 
    4.92818784713745, 8.14350032806396, 5.8305835723877, 4.29872941970825, 
    2.40664577484131, 5.64166641235352, 3.54239583015442, 7.72677087783813, 
    15.743332862854, 8.62441730499268, -0.472000002861023, 4.23868751525879, 
    1.46025002002716, -3.38889575004578, 0.802687466144562, 0.964958369731903, 
    1.58297920227051, -0.11906249076128, -0.334862500429153, 
    -0.680273830890656, -0.942218780517578, -1.05098223686218, 
    0.0301875006407499, 1.0349999666214, 0.0313541665673256, 
    0.819562494754791, 2.65635418891907, 0.409604161977768, -2.69658350944519, 
    -1.70570838451385, -1.49920833110809, -0.363833338022232, 
    -2.37735414505005, -3.01218771934509, -1.17566668987274, 
    -1.5924516916275, -1.59103500843048, -1.59103500843048, -3.78237724304199, 
    -4.6448974609375, -5.45899820327759, -5.45899820327759, -5.44416046142578, 
    -6.2167706489563, -5.15062475204468, -4.82256269454956, -1.30208337306976, 
    1.23983335494995, -2.12350010871887, -0.932020843029022, 
    -3.95872902870178, -0.439416646957397, -5.38956260681152, 
    -7.84178972244263, -7.82140016555786, -7.82140016555786, 
    -7.43291854858398, -4.32957315444946, -5.48298263549805, 
    -7.01420640945435, -7.08322381973267, -3.91328167915344, 
    -3.79383254051208, -2.88412356376648, 0.475962072610855, 
    0.168597742915154, 0.455960720777512, 0.455960720777512, 
    0.192490264773369, 0.192490264773369, 0.192490264773369, 
    0.185284718871117, 0.69179230928421, 0.679976046085358, 0.679976046085358, 
    0.679976046085358, 0.455960720777512, 0.455960720777512, 
    0.415949702262878, 0.741519451141357, 0.455960720777512, 
    0.455960720777512, 0.679976046085358, 0.455960720777512, 
    0.455960720777512, 0.415949702262878, 0.741519451141357, 
    0.455960720777512, 0.455960720777512, 0.679976046085358), .Dim = 365L, varnames = "Tair_f")), .Names = c("year", 
"T"), row.names = c(NA, -365L), class = "data.frame") 

我想将 T 列中的缺失值分配给同一年份的所有行,前提是可以找到该年份的缺失值。我已经试过了,但没有用。

ddply(df, "year",
      function(x)if(any(is.na(x$T))) NA else x)

有人可以帮我解决一下吗?

我们可以使用data.table。将 'data.frame' 转换为 'data.table',按 'year' 分组,如果 'T'.[=] 中有 any NA,我们将值更改为 NA 19=]

library(data.table)
setDT(df)[, T:=NA^any(is.na(T))*T, by = year]

或者我们可以提取 'year' 的行索引 (.I) 在 'T' 中具有任何 'NA' 值并分配 (:=) 这些元素在 'T' 到 NA。

setDT(df)[df[, .I[any(is.na(T))] , year]$V1, T:= NA]

或使用 ifelsedplyr

library(dplyr)
df %>% 
   group_by(year) %>%
   mutate(T= ifelse(any(is.na(T)), NA_real_, T))

数据

set.seed(24)
df <- data.frame(year= rep(2001:2010, each=5), 
    T= sample(c(NA, seq(0.1, 0.5, by=0.02)), 50, replace=TRUE))