代码之家  ›  专栏  ›  技术社区  ›  DanielMc

内联R返回0而不是正确的值?

  •  1
  • DanielMc  · 技术社区  · 3 年前

    内联代码只接受一个已经存在的变量。R代码块返回正确的值,但内联代码返回0。

    我的代码:

    ---
    title: "Test"
    output: html_document
    ---
    
    ```{r include=FALSE, echo=FALSE, message=FALSE, warning=FALSE}
    library(tidyverse)
    library(lubridate)
    
    first_date <- ymd("2019-01-28")
    last_date <- ymd("2020-03-12")
    last_date - first_date
    
    my_interval <- interval(first_date, last_date)
    number_of_days <- as.period(my_interval, unit = "day")
    number_of_days
    
    without lubridate, the time difference is `r last_date - first_date` days and with lubridate it's `r number_of_days` days
    

    我的输出:

    enter image description here

    1 回复  |  直到 3 年前
        1
  •  3
  •   Karl Forner    3 年前

    它可能被转换为整数:

    > number_of_days
    [1] "409d 0H 0M 0S"
    > as.integer(number_of_days)
    [1] 0
    

    试试这个:

    
     without lubridate, the time difference is `r last_date - first_date` days and with lubridate it's `r as.character(number_of_days)` days