R累计和计算NA问题

时间:2014-08-08 17:12:06

标签: r na cumulative-sum

我一直试图解决累积金额问题几天并且已经非常接近,但我仍遇到一些问题。

我试图为data.frame中的多个列向后计算累计和(从nrow到第一行)。当data.frame结尾没有NA / NaN值时,代码可以正常工作。但是如果存在NA值,则代码返回实际值,而我希望它返回NA。另外,我需要在我测量的最后一年存在结束值(df2中的RBH行)。

df2的样本测量:
2009 - 1.2
2010 - 1.8
2011年 - NaN
2012年 - NaN
RBH - 60.5

预期输出(将在df3中):
2008 - 57.5
2009 - 58.7
2010 - 60.5
2011年 - NaN
2012年 - NaN

我目前的代码为df3提供了什么:
2008 - 57.5
2009 - 58.7
2010 - 60.5
2011 - 59.5
2012 - 60.5

Code I&m;尝试:

#Build the function to deal with NA values (ex: died in 2010, NA for 2011 & 2012):
cumsum.alt <- function(x){
  res <- NaN*seq(x)
  for(i in seq(x)){
    if(sum(is.na(x[1])) == i){
      res[i] <- i
    } else {
      res[i] <- sum(x[1:i], na.rm=TRUE)
    }
  }
  res
}

#Run function to produce annual radius:
##STILL NEED TO FIX NA ISSUE
df3 <- apply(df2[nrow(df2):1,], 2, function(x) c(x[1], x[1]-cumsum.alt(x[-1])))
df3 <- df3[nrow(df3):1,]

可重复的Data.frame:

df2 <- structure(list(AP2D005 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, 1.896, 4.221, 1.204, 1.934, 1.859, 1.575, 1.602, 1.705, 
1.413, 0.786, 1.352, 0.903, 0.821, 1.0855, 1.3375, 1.554, 1.605, 
1.192, 1.395, 1.6965, 1.016, 1.0835, 1.464, 2.0505, 1.719, 2.067, 
2.0025, 1.9245, 2.4895, 2.3465, 2.0105, 0.897, 1.004, 1.6785, 
2.4405, 3.0625, 2.173, 2.629, 3.014, 2.7245, 3.2625, 3.115, 1.515, 
2.632, 2.067, 2.8155, 2.914, 2.3865, 1.976, 2.3085, 3.1135, 3.476, 
3.671, 2.1465, 3.0125, 2.129, 1.8335, 0.689, 0.8775, 1, 1.616, 
1.618, 2.5385, 1.9465, 1.799, 1.194, 0.7295, 0.7425, 0.5895, 
131.85), AP2D006 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, 3.64, 2.972, 1.402, 1.421, 
1.622, 1.648, 2.379, 2.014, 2.182, 0.802, 1.812, 1.139, 1.042, 
1.5435, 2.097, 2.064, 1.205, 1.955, 1.2985, 1.6255, 1.697, 2.3645, 
2.6805, 2.2965, 2.3095, 2.082, 2.4395, 1.863, 1.879, 2.2505, 
2.648, 2.5805, 2.6895, 2.587, 3.393, 3.1505, 3.543, 2.765, 0.7355, 
0.508, 0.5035, 0.681, 1.0305, 1.308, 1.966, 2.32, 1.814, 2.847, 
2.5295, 1.262, 2.058, 1.5235, 2.1625, 2.1215, 1.3525, 1.368, 
1.574, 2.1725, 2.8545, 2.219, 1.717, 2.0185, 1.128, 1.1475, 0.591, 
0.4725, 0.44, 0.485, 0.5375, 0.5215, 0.5845, 0.565, 0.5065, 0.367, 
0.353, 0.2545, 121.5), AP2D009 = c(NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1.485, 1.695, 1.655, 
1.1835, 1.324, 1.0755, 0.7495, 1.014, 1.2435, 1.841, 1.8845, 
1.148, 1.066, 1.926, 2.5395, 1.5005, 1.59, 1.3565, 1.5405, 1.7205, 
1.5825, 1.245, 1.883, 1.907, 2.149, 1.512, 0.8935, 0.6925, 0.687, 
1.265, 1.5055, 0.4295, 0.3495, 0.4275, 0.4615, 0.5665, 0.4045, 
0.309, 0.187, 0.2205, 0.2705, 0.6155, 0.9485, 0.977, 0.7205, 
1.3575, 1.4925, 1.43, 1.1535, 1.3195, 1.184, 1.1885, 0.5415, 
0.7375, 0.7455, 1.08, 1.2335, 1.269, 1.1135, 1.193, 0.535, 0.4935, 
0.349, 0.2665, 71.1), AP2D101 = c(NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 2.549, 1.393, 1.54, 
1.821, 1.65, 1.357, 1.742, 1.629, 2.11, 2.11, 1.972, 1.58, 1.88, 
1.9745, 1.3035, 1.0575, 1.5935, 1.6695, 1.4555, 2.306, 2.4825, 
2.1905, 3.2565, 3.599, 3.058, 1.5925, 0.8025, 0.4385, 0.514, 
0.6395, 0.581, 0.476, 0.5115, 0.864, 1.348, 0.6565, 0.3845, 0.35, 
0.2895, 0.4045, 0.471, 0.2795, 0.365, 0.256, 0.2685, 0.444, 0.329, 
0.1945, 0.1995, 0.307, 0.28, 0.1935, 0.1925, 0.176, 0.156, 0.1955, 
0.1915, 0.2485, 0.236, 0.192, 0.1785, 0.1745, NaN, 77.85), AP2D102 = c(NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, 1.083, 0.596, 0.8295, 0.341, 0.302, 0.1795, 0.3505, 0.2935, 
0.792, 0.796, 0.794, 0.5485, 0.6185, 1.1145, 0.6725, 0.542, 0.5935, 
0.92, 1.058, 1.3855, 1.089, 1.1255, 1.5755, 1.096, 0.865, 0.771, 
0.359, 0.5065, 0.6805, 1.011, 0.6695, 0.916, 0.9635, 0.997, 1.223, 
1.2305, 0.549, 0.5075, 0.3985, 0.6935, 0.8915, 0.592, 1.0005, 
0.9545, 1.0675, 1.0905, 1.3205, 0.849, 0.9155, 0.759, 1.131, 
0.545, 0.6075, 0.696, 0.7745, 0.707, 1.095, 1.081, 1.0935, 0.771, 
0.407, 0.417, 0.2815, 58.05), AP2D103 = c(NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, 1.89, 0.637, 0.655, 0.728, 0.496, 0.6405, 
0.647, 0.519, 0.5245, 0.784, 0.5065, 0.3155, 0.888, 1.29, 1.078, 
2.117, 1.9445, 0.537, 1.483, 0.72, 1.4035, 1.875, 1.5105, 1.917, 
2.2765, 3.26, 4.4505, 2.934, 2.176, 3.1805, 3.9025, 2.613, 0.704, 
1.123, 0.8075, 1.241, 1.146, 1.3415, 0.9385, 1.264, 0.9355, 0.5185, 
0.515, 0.3635, 67.05), AP3B012 = c(NaN, NaN, NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, 0.384, 1.387, 0.913, 2.094, 1.9315, 1.6805, 
1.786, 1.9035, 0.9345, 1.1825, 0.745, 0.402, 0.4425, 1.06, 0.796, 
0.865, 2.0025, 1.217, 2.362, 2.5695, 2.6205, 2.046, 2.886, 1.7505, 
3.9255, 2.385, 3.291, 1.9035, 3.952, 0.9955, 1.1625, 0.8605, 
0.5925, 0.894, 0.645, 0.808, 0.848, 1.126, 0.9, 0.842, 1.3375, 
0.987, 0.715, 1.0145, 1.181, 1.282, 0.781, 1.0705, 1.198, 1.1105, 
1.361, 1.523, 1.367, 2.099, 1.632, 1.482, 1.109, 0.915, 0.7505, 
1.041, 1.362, 1.2815, 1.452, 0.8735, 0.7945, 1.4145, 1.053, 0.604, 
0.496, 0.5095, 0.6825, 0.692, 0.765, 0.8125, 0.6225, 0.704, 0.8455, 
0.8555, 0.9605, 1.374, 0.9885, 1.0875, 0.818, 0.608, 0.3745, 
0.477, 0.493, 0.389, 0.5445, 0.5195, 0.416, 0.3045, 0.388, 0.475, 
117.45), AP3C003 = c(NaN, NaN, NaN, 0.864, 1.303, 1.526, 1.755, 
1.6755, 1.966, 0.9955, 1.826, 2.419, 1.3455, 2.674, 1.2985, 1.136, 
1.2045, 1.4395, 1.207, 1.6155, 0.747, 0.3255, 0.5825, 0.6715, 
0.7875, 0.5075, 0.7915, 0.6295, 1.0015, 1.0655, 0.791, 0.7365, 
0.811, 0.8255, 0.976, 0.886, 0.742, 0.6495, 1.174, 0.7135, 0.5695, 
0.4335, 0.403, 0.7665, 0.7705, 0.7535, 0.7935, 0.816, 0.648, 
0.609, 0.804, 0.868, 0.6895, 0.633, 0.8025, 0.952, 0.5745, 0.7275, 
0.9395, 0.9125, 1.1655, 1.1725, 1.167, 1.716, 1.7405, 0.899, 
0.689, 1.2195, 0.566, 1.056, 1.3895, 1.5445, 1.6875, 0.9655, 
0.738, 0.9635, 1.0905, 0.5625, 0.555, 0.499, 0.723, 1.0425, 1.143, 
0.9495, 0.991, 1.1495, 1.119, 1.637, 1.4185, 1.8495, 1.617, 1.5595, 
0.8665, 0.693, 0.5455, 0.4755, 0.4495, 0.4355, 0.461, 0.437, 
0.4485, 0.3075, 0.4915, 0.324, 97.2), AP3C004 = c(NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, 1.213, 2.051, 1.9785, 2.014, 
1.8175, 1.521, 1.41, 1.6, 1.1845, 1.523, 0.7555, 0.49, 0.3245, 
0.3685, 0.396, 0.386, 0.6635, 0.7135, 1.3875, 1.303, 0.6915, 
1.26, 1.047, 1.717, 2.556, 1.3405, 1.8075, 1.1115, 1.9395, 0.956, 
1.2815, 1.182, 0.986, 1.3365, 0.85, 1.133, 1.2705, 1.44, 1.1495, 
0.9655, 1.019, 1.1335, 0.8955, 1.0525, 0.9475, 0.777, 0.5705, 
0.841, 0.7975, 0.8365, 0.997, 0.8865, 1.072, 1.1055, 1.1845, 
0.769, 0.713, 0.423, 0.557, 0.5115, 0.616, 0.591, 0.8395, 0.834, 
0.603, 1.0795, 0.8225, 0.6915, 0.389, 0.587, 0.599, 0.678, 0.541, 
0.724, 0.8325, 0.929, 0.955, 1.341, 1.2635, 1.265, 1.1235, 1.29, 
0.889, 0.901, 0.589, 0.5495, 1.116, 0.945, 1.084, 1.097, 0.9305, 
0.636, 1.1145, 1.0885, 107.55), AP3C006 = c(NaN, NaN, NaN, NaN, 
NaN, NaN, NaN, NaN, NaN, NaN, NaN, 2.192, 1.629, 1.7385, 1.529, 
1.0845, 1.385, 2.1, 1.262, 1.6985, 1.178, 0.4605, 0.246, 0.395, 
0.3085, 0.3435, 0.4205, 0.3575, 0.6065, 0.845, 0.7185, 0.4835, 
0.374, 0.841, 1.1355, 0.88, 1.6065, 0.938, 1.951, 1.294, 1.1305, 
0.6615, 0.532, 0.991, 0.7385, 0.72, 0.6515, 1.016, 0.701, 0.649, 
0.745, 1.064, 0.8215, 0.7775, 0.7215, 0.6425, 0.531, 0.715, 0.5485, 
0.5125, 0.535, 0.556, 0.646, 0.761, 0.8585, 0.502, 0.433, 0.3585, 
0.288, 0.3925, 0.4115, 0.4905, 0.5765, 0.3925, 0.296, 0.447, 
0.466, 0.355, 0.2435, 0.203, 0.2455, 0.276, 0.2345, 0.241, 0.262, 
0.2295, 0.2775, 0.367, 0.4045, 0.3855, 0.436, 0.486, 0.391, 0.331, 
0.2745, 0.202, 0.2225, 0.252, 0.142, 0.161, NaN, NaN, NaN, NaN, 
71.55), AP3C007 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
0.309, 1.271, 1.951, 1.188, 1.279, 0.993, 0.712, 0.751, 1.01, 
0.9855, 1.1135, 1.0285, 0.8715, 0.491, 0.6965, 0.712, 0.564, 
0.761, 0.5115, 0.9185, 1.415, 0.668, 0.915, 0.561, 1.469, 1.8795, 
1.6, 2.2705, 1.307, 2.2295, 1.7, 0.7895, 0.5585, 0.4355, 0.6825, 
0.7255, 0.8215, 0.977, 0.8305, 0.658, 0.763, 0.776, 0.569, 0.4475, 
0.4725, 0.7665, 0.632, 0.5215, 0.6645, 0.7025, 0.7235, 0.872, 
0.6635, 0.8305, 1.112, 0.9745, 0.6345, 0.605, 0.325, 0.333, 0.489, 
0.4165, 0.5165, 0.681, 0.63, 0.494, 0.633, 0.5205, 0.3675, 0.3925, 
0.357, 0.3945, 0.355, 0.3895, 0.522, 0.4945, 0.4045, 0.4335, 
0.5165, 0.534, 0.703, 0.6705, 0.902, 0.5525, 0.499, 0.298, 0.2415, 
0.1995, 0.217, 0.2215, 0.2945, 0.3755, 0.2775, 0.299, 0.243, 
74.7), AP3C009 = c(NaN, NaN, NaN, NaN, NaN, NaN, 1.27, 1.569, 
1.835, 0.497, 0.868, 1.247, 0.8285, 1.2515, 0.933, 0.9325, 0.89, 
1.053, 1.1155, 1.534, 1.1725, 0.509, 0.453, 0.669, 0.6005, 0.4645, 
0.764, 0.9665, 1.6815, 2.199, 1.459, 1.819, 1.3145, 1.6195, 2.505, 
2.5875, 3.046, 2.106, 3.367, 1.8815, 2.1315, 1.559, 1.3835, 2.3815, 
1.894, 2.088, 2.3115, 2.7445, 2.0005, 1.383, 1.92, 2.1055, 1.532, 
1.6305, 2.055, 1.7215, 1.4205, 1.4015, 1.459, 1.53, 2.0205, 1.496, 
1.362, 1.923, 1.9535, 1.4275, 1.0955, 0.6085, 0.5295, 0.634, 
0.9845, 1.1095, 1.4335, 0.6545, 0.5525, 0.842, 0.949, 0.5215, 
0.3105, 0.311, 0.4625, 0.4255, 0.326, 0.419, 0.318, 0.336, 0.456, 
0.502, 0.69, 0.953, 0.5705, 0.913, 0.5185, 0.5145, 0.3585, 0.2685, 
0.334, 0.2435, 0.3295, 0.32, 0.32, 0.225, 0.268, 0.1815, 116.1
)), .Names = c("AP2D005", "AP2D006", "AP2D009", "AP2D101", "AP2D102", 
"AP2D103", "AP3B012", "AP3C003", "AP3C004", "AP3C006", "AP3C007", 
"AP3C009"), row.names = c("1909", "1910", "1911", "1912", "1913", 
"1914", "1915", "1916", "1917", "1918", "1919", "1920", "1921", 
"1922", "1923", "1924", "1925", "1926", "1927", "1928", "1929", 
"1930", "1931", "1932", "1933", "1934", "1935", "1936", "1937", 
"1938", "1939", "1940", "1941", "1942", "1943", "1944", "1945", 
"1946", "1947", "1948", "1949", "1950", "1951", "1952", "1953", 
"1954", "1955", "1956", "1957", "1958", "1959", "1960", "1961", 
"1962", "1963", "1964", "1965", "1966", "1967", "1968", "1969", 
"1970", "1971", "1972", "1973", "1974", "1975", "1976", "1977", 
"1978", "1979", "1980", "1981", "1982", "1983", "1984", "1985", 
"1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", 
"1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", 
"2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", 
"2010", "2011", "2012", "RBHinBarkmm"), class = "data.frame")

任何帮助都会很棒。谢谢!

2 个答案:

答案 0 :(得分:2)

我相信这就是你描述的内容

cumsum.alt<-function(x) {
    rh <- x[length(x)]
    rx <- rev(x)[-1]
    r <- rep(NA, length(x))
    dx <- rh-cumsum(c(0,rx[!is.na(rx)]))
    r[c(!is.na(rx), FALSE)] <- dx[-length(dx)]
    r[max(which(!is.na(r)))+1] <- dx[length(dx)]
    rev(r)
}

cumsum.alt(c(1,2,3,NA,50))
# [1] 44 45 47 50 NA
cumsum.alt(c(NA,1,2,3,50))
# [1] NA 44 45 47 50

答案 1 :(得分:0)

我不确定我是否正确理解了这个问题。它只是一个用NA填充NA条目而不是之前已知值的cumsum,对吗?

cumsum.alt <- function(x){
  res <- rep(NA,length(x))
  sumtohere <- 0
  for(i in seq(x)){
    if (!is.na(x[i])){
      sumtohere <- sumtohere+x[i]
      res[i] <- sumtohere
    } else {
      res[i] <- NA 
    }  
  }  
  res
}

关于需要最后一行有价值的话题是什么?所有这些示例的最后一行都有值。如果它是NA,它应该填充什么?