我试图定义一个应用于数据帧disease_df
的函数,以实现两件事:
-
生成新列
cum_days
和week
。 -
生成新的数据帧
occurence_per_day
和occurence_per_week
。
我的代码是
# Import data and packages
library(outbreaks)
library(lubridate)
library(dplyr)
disease_df <- rename(measles_hagelloch_1861, date = date_of_prodrome)[, 3, drop = FALSE]
disease_df$date = sort(disease_df$date)
disease_df$disease <- 1
# Define functions to construct tables of number of disease occurence per day and per week respectively
function1 <- function(df)
{
df <- get(df)
# First date
first_date <- min(df$date)
# Last date
last_date <- max(df$date)
# Commulative number of days
df$cum_days <- as.numeric(difftime(df$date, first_date, units = 'days'))
# Week of the date
df$week <- ifelse(df$cum_days / 7 <= 1, 1, ceiling(df$cum_days / 7))
# Construct a table of number of disease occurence per day
occurence_per_day <- aggregate(df$disease, by = list(Category = df$date), FUN = sum)
# Construct a table of number of disease occurence per week
occurence_per_week <- aggregate(df$disease, by = list(Category = df$week), FUN = sum)
occurence_per_day
}
function1(disease_df)
当我运行它时,会出现错误Error in get(df) : invalid first argument
。
你能帮我实现目标吗?非常感谢!
这里,我们不需要get
(当对象名称以字符串形式传递时是必需的。这里,它是未引用的(
function1 <- function(df) {
# First date
first_date <- min(df[["date"]])
# Last date
last_date <- max(df[["date"]])
# Commulative number of days
df$cum_days <- as.numeric(difftime(df[["date"]], first_date, units = 'days'))
# Week of the date
df$week <- ifelse(df[["cum_days"]] / 7 <= 1, 1, ceiling(df[["cum_days"]] / 7))
# Construct a table of number of disease occurence per day
occurence_per_day <- aggregate(df["disease"], by = list(Category = df[["date"]]), FUN = sum)
# Construct a table of number of disease occurence per week
occurence_per_week <- aggregate(df["disease"],
by = list(Category = df[["week"]]), FUN = sum)
occurence_per_day
}
-测试
function1(disease_df)
# Category disease
#1 1861-10-30 1
#2 1861-11-01 1
#3 1861-11-07 2
#4 1861-11-08 1
#5 1861-11-11 2
#6 1861-11-12 1
#7 1861-11-13 1
#8 1861-11-15 2
#9 1861-11-17 1
#10 1861-11-18 1
#11 1861-11-19 1
#12 1861-11-20 4
#13 1861-11-21 14
#14 1861-11-22 12
# ..