我有下面的資料框,想計算自學生上次(最近)離開當前日期以來的天數,并將其添加到原始資料框中。
Student ID Absent Date Subject
4567 08/30/2018 M
4567 09/22/2019 M
8345 09/01/2019 S
8345 03/30/2019 PE
8345 07/18/2017 M
5601 01/08/2019 SS
這是所需的輸出:
Student ID Absent Date Subject # of Days Since Last Absence
4567 08/30/2018 M 816
4567 09/22/2019 M 816
8345 09/01/2019 S 837
8345 03/30/2019 PE 837
8345 07/18/2017 M 837
5601 01/08/2019 SS 1073
感謝您的任何幫助。
uj5u.com熱心網友回復:
包 dplyr 和max日期上的一個會給你答案。R中的當前日期是Sys.Date()
library(dplyr)
df1 %>%
group_by(Student_ID) %>%
mutate(days_since_last_absence = Sys.Date() - max(as.Date(Absent_Date, format = "%m/%d/%Y")))
# A tibble: 6 x 4
# Groups: Student_ID [3]
Student_ID Absent_Date Subject days_since_last_absence
<int> <chr> <chr> <drtn>
1 4567 08/30/2018 M 816 days
2 4567 09/22/2019 M 816 days
3 8345 09/01/2019 S 837 days
4 8345 03/30/2019 PE 837 days
5 8345 07/18/2017 M 837 days
6 5601 01/08/2019 SS 1073 days
資料:
df1 <- structure(list(Student_ID = c(4567L, 4567L, 8345L, 8345L, 8345L,
5601L), Absent_Date = c("08/30/2018", "09/22/2019", "09/01/2019",
"03/30/2019", "07/18/2017", "01/08/2019"), Subject = c("M", "M",
"S", "PE", "M", "SS")), class = "data.frame", row.names = c(NA,
-6L))
uj5u.com熱心網友回復:
假設您的資料幀被稱為df,在基礎 R 中,嘗試
df$difference<-as.numeric(difftime(Sys.Date(),as.POSIXct(paste(df[,2]),format="%m/%d/%Y"),tz="UTC"))
tz 必須根據您的位置進行調整。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383798.html
上一篇:將一列資料框拆分為多列
