簡體   English   中英

刪除特定變量中沒有符號更改的鍵的所有行

[英]Remove all rows for a key with no sign change in a specific variable

我試圖在幾個ID上運行xirr函數,但我收到一條錯誤消息:

        Error in uniroot(xnpv, interval = interval, cf = cf, d = d, tau = tau, :
no sign change found in 1000 iterations

有沒有辦法刪除沒有符號更改的ID的所有行(如下例中的ID 2)?

library(tvm)
library(dplyr)

exampledf<-data.frame(c(2, 2, 2, 3, 3, 3, 3, 3), c("2017-11-30", "2017-12-31", "2018-01-31", "2017-11-30", "2017-12-31", "2018-01-31", "2018-02-28", "2018-03-31"), c(65000, 33000, 33000, -40000, 10250, 10250, 10000, 10500))
names(exampledf)<-c("ID","Date","CashFlow")
exampledf$Date <- as.Date(exampledf$Date)

exampledf %>%
  group_by(ID) %>% 
  summarise(
    IRR = xirr(cf = CashFlow, d = Date, 
               tau = NULL, comp_freq = 12, interval = c(-1, 10)))

任何幫助都將不勝感激!

以下列方式組合any()sign()函數將測試CashFlow列中的任何值是否為正( any(sign(CashFlow) == 1否定( any(sign(CashFlow) == -1)

library(dplyr)

exampledf %>% 
    group_by(ID) %>% 
    filter(any(sign(CashFlow) == 1) && any(sign(CashFlow) == -1))

結果

    # A tibble: 5 x 3
    # Groups:   ID [1]
         ID Date       CashFlow
      <dbl> <fct>         <dbl>
    1     3 2017-11-30   -40000
    2     3 2017-12-31    10250
    3     3 2018-01-31    10250
    4     3 2018-02-28    10000
    5     3 2018-03-31    10500

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM