Ниже приведены мои коды в R. Я использую пакет dplyr, чтобы упорядочить данные по ID и дате, и пытаюсь создать новый столбец SD с помощью mutate (). В столбце SD есть несколько критериев для результата в столбце SD, поэтому я использовал функции if () и if else (), но есть предупреждающие сообщения.
library(dplyr)
ID<-c("A01","A02","A03","A01","A01","A03","A02")
SA<-c(50,100,50,100,150,100,20)
a<-c("01/01/2012","01/01/2011","01/01/2012","01/01/2011","01/01/2013","01/01/2013","01/01/2012")
Date<-as.Date(a, format = "%d/%m/%Y")
df <- data.frame(ID,Date,SA)
start_date = as.Date("01/01/2012", format = "%d/%m/%Y")
end_date = as.Date("31/03/2012", format = "%d/%m/%Y")
df %>%
arrange(ID,Date) %>%
group_by(ID) %>%
mutate(start_date=start_date,
end_date=end_date,
period=as.numeric(end_date - start_date + 1),
SD = if(Date <= start_date & Date + 365 >= end_date) {1}
else if(Date + 365 <= start_date | Date >= end_date) {0}
else if(Date <= start_date & Date + 365 <= end_date) {(Date + 365 - start_date + 1)/period}
else if(Date >= start_date & Date + 365 >= end_date) {(end_date - Date + 1)/period})
Однако, есть предупреждающие сообщения, как показано ниже , Как мне решить это?
"Warning messages:
1: In if (Date <= start_date & Date + 365 >= end_date) { :
the condition has length > 1 and only the first element will be used
2: In if (Date + 365 <= start_date | Date >= end_date) { :
the condition has length > 1 and only the first element will be used
3: In if (Date <= start_date & Date + 365 >= end_date) { :
the condition has length > 1 and only the first element will be used
4: In if (Date + 365 <= start_date | Date >= end_date) { :
the condition has length > 1 and only the first element will be used
5: In if (Date <= start_date & Date + 365 >= end_date) { :
the condition has length > 1 and only the first element will be used"