使用R减去每行中最后两个非NA值的总和

时间:2015-05-21 22:11:57

标签: r dataframe

我有一个数据集如下:

class MyClass: UIViewController {
    private var backButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        backButton = UIBarButtonItem(title: "Back", style: .Plain, target: self, action: "goBack")

        navigationItem.leftBarButtonItem = backButton
    }

    func goBack() {
        navigationController?.popViewControllerAnimated(true)
    }

    func toggleBackButton() {
        backButton.enabled = (backButton.enabled == false) ? true : false
    }
}

数据如下:

test <- dput(head(v_nc044_D1))
structure(list(datetime = structure(c(1407084000, 1407084600, 
1407085200, 1407085800, 1407086400, 1407087000), class = c("POSIXct", 
"POSIXt"), tzone = "America/New_York"), Year = c(2014, 2014, 
2014, 2014, 2014, 2014), Month = c(8, 8, 8, 8, 8, 8), Day = c(3, 
3, 3, 3, 3, 3), Hour = c(12, 12, 13, 13, 13, 13), Minute = c(40, 
50, 0, 10, 20, 30), Second = c(0, 0, 0, 0, 0, 0), `0.62m` = c(-0.261002744513958, 
-0.0046253490894381, -0.0227783314435456, -0.06754116493121, 
-0.0931571522133919, -0.113669656310167), `0.87m` = c(-0.0618646621734571, 
0.0211107567169875, -0.0365161577717565, 0.00963063146095258, 
-0.0758947476955339, -0.0222807653886873), `1.12m` = c(-0.0740150182535496, 
0.0455566628870912, 0.0351571760389381, -0.0534912819492359, 
-0.00564931582642438, 0.0133347204843959), `1.37m` = c(-0.0912957439302997, 
0.0111353046585499, -0.019273556678102, -0.000435167144034057, 
-0.0458170575781751, -0.0127271725200709), `1.62m` = c(-0.0650139770676744, 
0.0317113336031828, -0.0187554140951846, -0.012784427650523, 
-0.126181347618965, -0.0523057994185569), `1.87m` = c(-0.0427856910137444, 
0.0386148969096894, 0.0503451562952946, 0.0118530848750186, 0.0574113679524447, 
-0.0665927418664546), `2.12m` = c(-0.0111353046585499, 0.0365161577717565, 
0.0995905716142262, 0.015496206082472, 0.027778425247989, -0.0468755350871988
), `2.37m` = c(-0.0351728611534744, 0.0655895910888281, 0.0815334923082611, 
0.045760450363849, 0.0119110621782513, 0.0261053211276419), `2.62m` = c(NA, 
0.0983424205765916, 0.159535632946263, 0.124269045865499, 0.0473250447988371, 
0.0586014404175147), `2.87m` = c(NA, 0.167157666855389, 0.147665129292641, 
0.130290173025915, 0.107253142909869, 0.0627856966447743), `3.12m` = c(NA, 
0.154186264422501, 0.056209197183751, 0.187778773521987, 0.168156692017287, 
0.129029225299673), `3.37m` = c(NA, 0.105317887506694, 0.0211631718690514, 
0.12958105590107, 0.179678754275477, 0.160614204091005), `3.62m` = c(NA, 
0.086969880877962, 0.0941624306654757, 0.124648169592599, 0.118932949331823, 
0.156319953636931), `3.87m` = c(NA, 0.149899568622527, 0.0625970474331041, 
0.0418047000340771, 0.0196472666036648, 0.122967849632311), `4.12m` = c(NA, 
0.0556952729892951, 0.00319610691263074, 0.00791055933089695, 
0.00663293299751877, 0.067328914815342), `4.37m` = c(NA, -0.0225699705139911, 
-0.080927124841696, -0.0777769693302853, -0.0246402623331592, 
0.0136867292966706), `4.62m` = c(NA, -0.0615751565757508, -0.134495945951693, 
-0.0618646621734571, -0.0229374734020511, 0.00307472214818529
), `4.87m` = c(NA, -0.0997517837680451, -0.0990856729632718, 
-0.110864185483203, -0.021837541396776, -0.00809910978549967), 
    `5.12m` = c(NA, -0.144544115032173, -0.127082121978963, -0.13656886282602, 
    -0.0534912819492359, -0.148018775913873), `5.37m` = c(NA, 
    -0.147702341670209, -0.0949168019010879, -0.0977416158802601, 
    -0.0460568084817089, -0.0414637657434323), `5.62m` = c(NA, 
    NA, NA, -0.14780578851506, -0.00518572463282063, 0.117655983133694
    ), `5.87m` = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), `6.12m` = c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_), `6.37m` = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), `6.62m` = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), `6.87m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `7.12m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `7.37m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `7.62m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `7.87m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `8.12m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `8.37m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `8.62m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `8.87m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `9.12m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `9.37m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `9.62m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `9.87m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `10.12m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `10.37m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `10.62m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `10.87m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `11.12m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), `11.37m` = c(NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("datetime", 
"Year", "Month", "Day", "Hour", "Minute", "Second", "0.62m", 
"0.87m", "1.12m", "1.37m", "1.62m", "1.87m", "2.12m", "2.37m", 
"2.62m", "2.87m", "3.12m", "3.37m", "3.62m", "3.87m", "4.12m", 
"4.37m", "4.62m", "4.87m", "5.12m", "5.37m", "5.62m", "5.87m", 
"6.12m", "6.37m", "6.62m", "6.87m", "7.12m", "7.37m", "7.62m", 
"7.87m", "8.12m", "8.37m", "8.62m", "8.87m", "9.12m", "9.37m", 
"9.62m", "9.87m", "10.12m", "10.37m", "10.62m", "10.87m", "11.12m", 
"11.37m"), class = c("tbl_df", "data.frame"), row.names = c(NA, 
-6L))

我想要的是每行中不包括NA的最后两个值的总和。例如,在第一行中,我想要列> test Source: local data frame [6 x 51] datetime Year Month Day Hour Minute Second 0.62m 0.87m 1.12m 1.37m 1.62m 1.87m 2.12m 2.37m 2.62m 1 2014-08-03 12:40:00 2014 8 3 12 40 0 -0.261002745 -0.061864662 -0.074015018 -0.0912957439 -0.06501398 -0.04278569 -0.01113530 -0.03517286 NA 2 2014-08-03 12:50:00 2014 8 3 12 50 0 -0.004625349 0.021110757 0.045556663 0.0111353047 0.03171133 0.03861490 0.03651616 0.06558959 0.09834242 3 2014-08-03 13:00:00 2014 8 3 13 0 0 -0.022778331 -0.036516158 0.035157176 -0.0192735567 -0.01875541 0.05034516 0.09959057 0.08153349 0.15953563 4 2014-08-03 13:10:00 2014 8 3 13 10 0 -0.067541165 0.009630631 -0.053491282 -0.0004351671 -0.01278443 0.01185308 0.01549621 0.04576045 0.12426905 5 2014-08-03 13:20:00 2014 8 3 13 20 0 -0.093157152 -0.075894748 -0.005649316 -0.0458170576 -0.12618135 0.05741137 0.02777843 0.01191106 0.04732504 6 2014-08-03 13:30:00 2014 8 3 13 30 0 -0.113669656 -0.022280765 0.013334720 -0.0127271725 -0.05230580 -0.06659274 -0.04687554 0.02610532 0.05860144 Variables not shown: 2.87m (dbl), 3.12m (dbl), 3.37m (dbl), 3.62m (dbl), 3.87m (dbl), 4.12m (dbl), 4.37m (dbl), 4.62m (dbl), 4.87m (dbl), 5.12m (dbl), 5.37m (dbl), 5.62m (dbl), 5.87m (dbl), 6.12m (dbl), 6.37m (dbl), 6.62m (dbl), 6.87m (dbl), 7.12m (dbl), 7.37m (dbl), 7.62m (dbl), 7.87m (dbl), 8.12m (dbl), 8.37m (dbl), 8.62m (dbl), 8.87m (dbl), 9.12m (dbl), 9.37m (dbl), 9.62m (dbl), 9.87m (dbl), 10.12m (dbl), 10.37m (dbl), 10.62m (dbl), 10.87m (dbl), 11.12m (dbl), 11.37m (dbl) 2.12m的总和。在第二行中,我想要列2.37m5.12m的总和。

我在

之后做的最后一个值
5.37m

但我不知道如何处理最后2个值。第一行的最后两个值是lastValue <- function(x) tail(x[!is.na(x)], 1) test$last <- as.numeric(apply(test, 1, lastValue)) -0.11135301

1 个答案:

答案 0 :(得分:2)

如果我理解正确,您只需使用NA从每行(或列)中删除na.omit,然后将最后一行添加到每行(或列)的值,例如

apply(my_df, 1, function(x) tail(na.omit(x), n = 2)) # for rows
apply(my_df, 2, function(x) tail(na.omit(x), n = 2)) # for columns