函数根据条件语句迭代列表和求和列

时间:2017-03-13 22:32:13

标签: r

我有一个看起来像的数据帧列表(下面附带的示例输入):

[[1]]
                 SPY.HIGH   SPY.ADX  SPY.MA  SPY.CLOSE
2014-08-11        -1          1       1      5
2014-08-12        -1          1       1     10

[[2]]]
                  MSCI.HIGH MSCI.ADX MSCI.MA  MSCI.CLOSE 
2014-08-29        -1          -1       1      3
2014-09-01        -1           1       1      1
2014-09-02         0          -1       0      2

我试图创建一个函数,该函数只根据第一列非零的条件,第二列是正数而第三列是非负的,总结上述数据样本的最后一列。我做了以下尝试:

marketsum <- function(x) {
  #Conditional columns
  validrows <- FlaggingTrend[[x]][,1] != 0 | FlaggingTrend[[x]][,2] > 0 | FlaggingTrend[[x]][,3] != 0

  # Only calculates the sum of fourth column for the rows if above conditions are met
  sum(FlaggingTrend[[x]][validrows,4])
}

returnsum <- list()

# Use sapply to run function to work out sums 
returnsum <- sapply(1:length(FlaggingTrend), function(x) marketsum(x))

但是,我得到NA而不是求和值?

names close
1    EUR    NA
2    JPY    NA
3    GBP    NA
4    CHF    NA
5    AUD    NA
6    CAD    NA
7    NZD    NA

我希望得到关于我出错的地方的帮助或指导。样本输出如下:

list(structure(list(EUR.CLOSE = c(-1, -1, -1, 0, 1, 0, 0, -1, 
0, 1), EUR.CLOSE.1 = c(1, -1, -1, 1, 1, 1, 1, -1, -1, -1), EUR.50D = c(-1, 
-1, -1, -1, -1, -1, -1, -1, -1, -1), EUR.CLOSE.2 = c(-0.0011268664916733, 
-0.00423709498833686, -0.00188893142678216, 0.00217196364717242, 
0.00686254162079234, -0.00544858088184653, -0.000188412624203348, 
-0.00737591996472312, 0.00208590196044455, 0.00227057808051545
)), .Names = c("EUR.CLOSE", "EUR.CLOSE.1", "EUR.50D", "EUR.CLOSE.2"
), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", "2017-02-15", 
"2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", "2017-02-22", 
"2017-02-23"), class = "data.frame"), structure(list(JPY.CLOSE = c(0, 
0, 1, 0, -1, -1, 0, 1, 0, -1), JPY.CLOSE.1 = c(-1, -1, -1, -1, 
-1, -1, -1, -1, -1, -1), JPY.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), JPY.CLOSE.2 = c(-0.000264935754629825, 0.00458231327013969, 
0.00456141141766331, -0.000875580127735809, -0.00809151292488242, 
-0.00353857410990255, 0.00230149698827908, 0.00511510066677001, 
-0.00326005839636956, -0.00619690368354142)), .Names = c("JPY.CLOSE", 
"JPY.CLOSE.1", "JPY.50D", "JPY.CLOSE.2"), row.names = c("2017-02-10", 
"2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", 
"2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(GBP.CLOSE = c(0, 1, 0, 0, 1, -1, 0, 0, 0, 
    1), GBP.CLOSE.1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), GBP.50D = c(-1, 
    -1, -1, -1, -1, -1, -1, -1, -1, -1), GBP.CLOSE.2 = c(-0.00048023051987453, 
    0.00279809911944895, -0.00464112219813048, -0.000561594944360089, 
    0.0022444899202175, -0.00618451029481418, 0.00410050825810596, 
    0.000802053299332334, -0.00184568523285542, 0.00847801606899046
    )), .Names = c("GBP.CLOSE", "GBP.CLOSE.1", "GBP.50D", "GBP.CLOSE.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        CHF.OPEN = c(1, 0, 0, 0, -1, 0, 0, 1, 0, -1), CHF.OPEN.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, 1, -1), CHF50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), CHF.OPEN.2 = c(0.000698567964139939, 
        0.00328669186857626, 0.00049704260678339, -0.000695928844511353, 
        -0.00838914337795957, 0.00550083899465574, 0.000199461454735274, 
        0.00685716972613716, 0.000594059423411153, -0.00396707849439904
        )), .Names = c("CHF.OPEN", "CHF.OPEN.1", "CHF50D", "CHF.OPEN.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        AUD.CLOSE = c(1, 0, 0, 1, 0, -1, 0, 0, 1, 0), AUD.CLOSE.1 = c(1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1), AUD.50D = c(-1, -1, -1, -1, 
        -1, -1, -1, -1, -1, -1), AUD.CLOSE.2 = c(0.00640566933128534, 
        -0.00444038864633145, 0.00300594880983807, 0.0061146355869709, 
        -0.00207738324513479, -0.00390676366154397, 0.00312663099943206, 
        -0.00169237819473061, 0.00364156989756792, 0.00155662244429539
        )), .Names = c("AUD.CLOSE", "AUD.CLOSE.1", "AUD.50D", 
    "AUD.CLOSE.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(CAD.CLOSE = c(-1, 0, 0, 0, 0, 1, 0, 1, 0, 
    -1), CAD.CLOSE.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 
    -1), CAD.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CAD.CLOSE.2 = c(-0.00472778065310547, 
    -0.000917641725324381, 0.000382453058549392, 0.000305857167088586, 
    -0.000688310225637978, 0.00191080426541917, 0.000763300548471235, 
    0.00266697304419594, 0.00174871741959248, -0.00449199430572389
    )), .Names = c("CAD.CLOSE", "CAD.CLOSE.1", "CAD.50D", "CAD.CLOSE.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        NZD.CLOSE = c(0, 0, 0, 1, 0, -1, 0, -1, 0, 1), NZD.CLOSE.1 = c(1, 
        1, 1, 1, 1, 1, 1, 1, 1, 1), NZD.50D = c(-1, -1, -1, -1, 
        -1, -1, -1, -1, -1, -1), NZD.CLOSE.2 = c(0.00041733324664972, 
        -0.00208841008727045, -0.000836586774949688, 0.00750420419797471, 
        -0.00180142777185216, -0.00389105549296664, 0.00111327592906424, 
        -0.00404153570815258, 0.00390244397693174, 0.00568695616893883
        )), .Names = c("NZD.CLOSE", "NZD.CLOSE.1", "NZD.50D", 
    "NZD.CLOSE.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(SEK.OPEN = c(1, 1, 0, 0, -1, 0, 1, 1, 0, 0
    ), SEK.OPEN.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 1), 
        SEK.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), SEK.OPEN.2 = c(0.00163829609003363, 
        0.0023181988913219, -0.000716164076936199, -0.00145628918509555, 
        -0.00677152707834994, 0.00582941106111079, 0.00270059947077783, 
        0.00455540960162004, -0.000256248852460228, 0.00199255363165785
        )), .Names = c("SEK.OPEN", "SEK.OPEN.1", "SEK.50D", "SEK.OPEN.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        NOK.CLOSE = c(1, 0, 0, -1, -1, 0, 0, 1, 0, 0), NOK.CLOSE.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1), NOK.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), NOK.CLOSE.2 = c(0.00323283885094217, 
        0.00303175214315887, 0.000941075635438882, -0.00716968871997281, 
        -0.00617111408142934, 0.00665069589528811, -0.000467589464977269, 
        0.00246736261239189, 0.000837040274633605, -0.00324444329976892
        )), .Names = c("NOK.CLOSE", "NOK.CLOSE.1", "NOK.50D", 
    "NOK.CLOSE.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(CZK.CLOSE = c(1, 1, 1, 0, -1, 0, 0, 1, 0, 
    -1), CZK.CLOSE.1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CZK.50D = c(1, 
    1, 1, -1, -1, 1, -1, 1, -1, -1), CZK.CLOSE.2 = c(0.00144601789180365, 
    0.00379617459269976, 0.00202579528819546, -0.00220231606416821, 
    -0.00688897736863447, 0.00560532297819627, 0.000184602212216056, 
    0.00710284457208665, -0.00197121312914339, -0.00224919172846905
    )), .Names = c("CZK.CLOSE", "CZK.CLOSE.1", "CZK.50D", "CZK.CLOSE.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        HUF.LOW.1 = c(0, 1, 1, 0, -1, 0, 0, 1, 0, 0), HUF.LOW.1.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, 1, -1, 1), HUF.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), HUF.LOW.1.2 = c(0.0013470806658562, 
        0.00413351833762921, 0.000927691660614904, -0.00161542594720476, 
        -0.00908798735735505, 0.00843420139734707, -0.00110208028787984, 
        0.00632043873779775, -0.00133632598823841, 0.000719782039952932
        )), .Names = c("HUF.LOW.1", "HUF.LOW.1.1", "HUF.50D", 
    "HUF.LOW.1.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(ILS.LOW.1 = c(0, 0, 0, -1, -1, -1, 0, 0, 0, 
    0), ILS.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    ), ILS.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ILS.LOW.1.2 = c(0.000933818987030266, 
    0.000959590648321962, -0.00159982969276018, -0.00296644925366696, 
    -0.00509821862018822, -0.00393531505179312, 0.00242770943578163, 
    -0.00285991992901691, -0.000567559913074556, 0.00091874516690349
    )), .Names = c("ILS.LOW.1", "ILS.LOW.1.1", "ILS.50D", "ILS.LOW.1.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        PLN.LOW.1 = c(0, 1, 0, 0, -1, 1, 0, 0, 0, 0), PLN.LOW.1.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1), PLN.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), PLN.LOW.1.2 = c(-0.00274633685326608, 
        0.00755279414139087, 0.000417931735706034, -0.000491702529882065, 
        -0.00490565626173578, 0.00929781633628868, -0.00365469691428677, 
        0.00186580959045224, -0.00137446945036213, -0.00149934278232444
        )), .Names = c("PLN.LOW.1", "PLN.LOW.1.1", "PLN.50D", 
    "PLN.LOW.1.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(RUB.LOW.1 = c(-1, -1, -1, -1, 0, 0, 1, 0, 
    -1, 1), RUB.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 
    -1), RUB.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), RUB.LOW.1.2 = c(-0.00446008580014645, 
    -0.0121748200414373, -0.00422818336649922, -0.0153488129381678, 
    0.0022241309795028, 0.00590173583792808, 0.014758577509606, 
    -0.00824954514944221, -0.00941636498473652, 0.0111898627811513
    )), .Names = c("RUB.LOW.1", "RUB.LOW.1.1", "RUB.50D", "RUB.LOW.1.2"
    ), row.names = c("2017-02-09", "2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22"), class = "data.frame"), structure(list(
        TRY.HIGH.1 = c(0, 0, -1, 0, 0, -1, -1, 0, -1, -1), TRY.HIGH.1.1 = c(1, 
        1, 1, 1, 1, -1, -1, -1, -1, -1), TRY.50D = c(-1, -1, 
        -1, -1, -1, -1, -1, -1, -1, -1), TRY.HIGH.1.2 = c(0.00417356089701015, 
        -0.00686579177053992, -0.00568012668941198, 0.00229778255761226, 
        0.00204713949728741, -0.0100024127023963, -0.00176420367173535, 
        -0.00113183078791668, -0.011249649909296, -0.00307744199905646
        )), .Names = c("TRY.HIGH.1", "TRY.HIGH.1.1", "TRY.50D", 
    "TRY.HIGH.1.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(ZAR.LOW.1 = c(-1, 0, -1, -1, 0, 0, 0, 0, -1, 
    -1), ZAR.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 
    -1), ZAR.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), ZAR.LOW.1.2 = c(-0.0050112296343281, 
    -0.000119978703923884, -0.0173679300039469, -0.0145343910929765, 
    0.00897924634581271, 0.000421926368602055, 0.000521408427958114, 
    0.00716476609655059, -0.0126608692527035, -0.00785440574245699
    )), .Names = c("ZAR.LOW.1", "ZAR.LOW.1.1", "ZAR.50D", "ZAR.LOW.1.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        BRL.LOW.1 = c(0, 0, -1, -1, 0, 1, 0, 0, -1, -1), BRL.LOW.1.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1), BRL.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), BRL.LOW.1.2 = c(-0.00371652356533581, 
        -0.00160621962638552, -0.00832934597660584, -0.00902065874268554, 
        0.010543197464703, 0.00316721872930192, -0.00362049854021018, 
        0.00278119317749237, -0.0100617531753731, -0.0006199931673041
        )), .Names = c("BRL.LOW.1", "BRL.LOW.1.1", "BRL.50D", 
    "BRL.LOW.1.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(CLP.LOW.1 = c(-1, 0, 0, 0, 0, 1, 0, 0, 0, 
    0), CLP.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    ), CLP.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CLP.LOW.1.2 = c(-0.00993671434599985, 
    0.00456853190734918, -0.00172828592427443, -0.00404432121904819, 
    -0.00048516719656444, 0.00933326604643625, -0.00561445365426927, 
    0.00353409520664982, -0.00208469536279665, -0.00105956972687871
    )), .Names = c("CLP.LOW.1", "CLP.LOW.1.1", "CLP.50D", "CLP.LOW.1.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        COP.CLOSE = c(-1, 1, 0, 0, 0, 1, 0, 1, 0, -1), COP.CLOSE.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1), COP.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), COP.CLOSE.2 = c(-0.00197341552292052, 
        0.00662211307234362, 0.000580874632634121, -0.00202234392720335, 
        0.00204668459925017, 0.00543743450560008, -0.00266994190200176, 
        0.00607751193077277, -0.00217715796204931, -0.00905605289811184
        )), .Names = c("COP.CLOSE", "COP.CLOSE.1", "COP.50D", 
    "COP.CLOSE.2"), row.names = c("2017-02-10", "2017-02-13", 
    "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(MXN.LOW.1 = c(0, 0, -1, 0, 0, 1, -1, -1, -1, 
    -1), MXN.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 
    -1), MXN.50D = c(-1, -1, 1, 1, 1, 1, 1, 1, 1, 1), MXN.LOW.1.2 = c(-0.000211320384603475, 
    -0.00314066280938086, -0.00107540683430729, 0.000705557901050646, 
    0.00518510570127306, 0.00240140867288519, -0.00281365838425973, 
    -0.0177295559643031, -0.00536567713104841, -0.0121193389083811
    )), .Names = c("MXN.LOW.1", "MXN.LOW.1.1", "MXN.50D", "MXN.LOW.1.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        PEN.HIGH.1 = c(-1, 0, -1, -1, 0, 1, -1, 0, -1, 0), PEN.HIGH.1.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, -1, -1, -1), PEN.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), PEN.HIGH.1.2 = c(-0.00429316820091041, 
        0.00214888801033619, -0.000859001157250239, -0.0038438522704316, 
        0.00101621330746293, 0.00460618041489735, -0.00522194398115183, 
        0, -0.000462071629322658, -0.000154071335332739)), .Names = c("PEN.HIGH.1", 
    "PEN.HIGH.1.1", "PEN.50D", "PEN.HIGH.1.2"), row.names = c("2017-02-10", 
    "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", 
    "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(CNY.CLOSE = c(1, 0, -1, 0, -1, 1, 1, 1, 0, 
    -1), CNY.CLOSE.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, 
    -1), CNY.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), CNY.CLOSE.2 = c(0.00135295379134326, 
    0.000247116371263401, -0.00184758230288495, 0.000524070878021865, 
    -0.0026376972616271, 0.001968002384513, 0.00174608993071379, 
    0.000799273430379666, -0.00090104498241117, -0.00170257391661588
    )), .Names = c("CNY.CLOSE", "CNY.CLOSE.1", "CNY.50D", "CNY.CLOSE.2"
    ), row.names = c("2017-02-10", "2017-02-13", "2017-02-14", 
    "2017-02-15", "2017-02-16", "2017-02-17", "2017-02-20", "2017-02-21", 
    "2017-02-22", "2017-02-23"), class = "data.frame"), structure(list(
        IDR.LOW.1 = c(-1, 0, 0, 0, 0, 0, 1, 1, 0, 0), IDR.LOW.1.1 = c(-1, 
        -1, -1, -1, -1, -1, -1, 1, -1, -1), IDR.50D = c(1, 1, 
        1, 1, 1, 1, 1, 1, 1, 1), IDR.LOW.1.2 = c(-0.00225360672301989, 
        0.00142787383820853, 0.000750694427567922, 0.000375135991193432, 
        -0.000825485019296579, 0.000975500021079156, 0.00157380030235466, 
        0.00142178345498323, -0.000448765901323256, -0.00119769458137853
        )), .Names = c("IDR.LOW.1", "IDR.LOW.1.1", "IDR.50D", 
    "IDR.LOW.1.2"), row.names = c("2017-02-09", "2017-02-10", 
    "2017-02-13", "2017-02-14", "2017-02-16", "2017-02-17", "2017-02-20", 
    "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"), 
    structure(list(INR.LOW.1 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1
    ), INR.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    ), INR.50D = c(-1, -1, -1, -1, -1, -1, 0, -1, 0, -1), INR.LOW.1.2 = c(-0.000466530150061928, 
    -0.000531550320954643, -0.000443420116883253, -0.000392023361358795, 
    -0.000386278564721287, -0.000386427833512393, -0.000244913256195467, 
    -0.000181510566847898, -0.000126929400856568, -0.000132850348706803
    )), .Names = c("INR.LOW.1", "INR.LOW.1.1", "INR.50D", "INR.LOW.1.2"
    ), row.names = c("2017-02-08", "2017-02-09", "2017-02-10", 
    "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", 
    "2017-02-20", "2017-02-22"), class = "data.frame"), structure(list(
        KRW.LOW.1 = c(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1), 
        KRW.LOW.1.1 = c(-1, -1, -1, -1, -1, -1, -1, 1, -1, -1
        ), KRW.50D = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), KRW.LOW.1.2 = c(0.000517860571988438, 
        0.000112542907102053, 0.00134952787013343, 0, -0.000337211266051574, 
        0.00112359562382647, 0.000449084996881899, 0.000785413785115541, 
        -0.00085276367906495, -0.000718681250895026)), .Names = c("KRW.LOW.1", 
    "KRW.LOW.1.1", "KRW.50D", "KRW.LOW.1.2"), row.names = c("2017-02-10", 
    "2017-02-13", "2017-02-14", "2017-02-15", "2017-02-16", "2017-02-17", 
    "2017-02-20", "2017-02-21", "2017-02-22", "2017-02-23"), class = "data.frame"))

1 个答案:

答案 0 :(得分:0)

以下是解决问题的方法,d是您的dput的内容:

vapply(d, function(x) {
        valid_rows <- x[, 1] != 0 | x[, 2] > 0 |  x[, 3] != 0
        sum(x[valid_rows, 4])
 }, numeric(1))

有几点需要注意:

  • 首先你说&#34;第一列是非零,第二列是正数,第三列是非负的&#34;但是你在条件中使用||表示OR,如果您需要AND,则需要使用&。但是,在这种情况下,您的某些示例不会与任何行匹配,因此您将获得0的总和。如果它不是您想要的,则需要修改代码以返回NA当没有符合您条件的行时,例如:

    vapply(d, function(x) {
              valid_rows <- x[, 1] != 0 &  x[, 2] > 0 &   x[, 3] != 0
              if (sum(valid_rows)  > 0)
                sum(x[valid_rows, 4])
              else
                NA_real_
              }, numeric(1))
    
  • 第二,对于你的最后一个条件,你说&#34;第三列是非负的&#34;,但是你写FlaggingTrend[[x]][,3] != 0来测试不同于0的值,而不是正值。

  • 第三,最好不要在函数中使用全局变量。这可能会导致很多混乱。
  • 这里,我使用vapply来强制返回对象的数据类型。它比sapply更安全,其行为可以根据返回对象的数据类型而改变。