R中同一个人的首个日期和最后一个日期之间的差异 [英] difference between the first date and last date within same individual in R
本文介绍了R中同一个人的首个日期和最后一个日期之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下午好
我不是R用户,但是我需要获取RFID中第一个日期和最后一个日期之间的差,以创建一个新列X。因此,第一个值必须为1(不为零),第二个2,...,n。
Good afternoon I am not an R user, but I need to get the difference between the first date and last date within RFID, to create a new column X. Therefore, the first value needs to be 1 (not zero), the second 2, ..., n.
以下是数据示例。
感谢您。
RFID visit_date ADFI location
985152014315936 2017-11-25 2133 16
985152014315936 2017-11-26 2186 16
985152014315936 2017-11-27 3489 16
985152014315936 2017-11-28 2432 16
985152014315937 2017-11-24 15 17
985152014315937 2017-11-25 1512 17
985152014315937 2017-11-26 2378 17
985152014315937 2017-11-27 3241 17
985152014315938 2017-11-24 584 17
985152014315938 2017-11-25 1689 17
985152014315938 2017-11-26 2807 17
985152014315938 2017-11-27 2369 17
985152014315938 2017-11-28 2576 17
985152014315939 2017-11-25 1084 17
985152014315939 2017-11-26 3489 17
985152014315939 2017-11-27 2630 17
985152014315939 2017-11-28 3585 17
985152014315939 2017-11-29 3433 17
985152014315939 2017-11-30 2962 17
推荐答案
这是使用 dplyr
和 lubridate
的解决方案:
Here is a solution using dplyr
and lubridate
:
require(tidyverse);
require(lubridate);
df %>% group_by(RFID) %>% mutate(X = max(ymd(visit_date)) - min(ymd(visit_date)));
## A tibble: 19 x 5
## Groups: RFID [4]
# RFID visit_date ADFI location X
# <dbl> <fct> <int> <int> <time>
# 1 985152014315936 2017-11-25 2133 16 3
# 2 985152014315936 2017-11-26 2186 16 3
# 3 985152014315936 2017-11-27 3489 16 3
# 4 985152014315936 2017-11-28 2432 16 3
# 5 985152014315937 2017-11-24 15 17 3
# 6 985152014315937 2017-11-25 1512 17 3
# 7 985152014315937 2017-11-26 2378 17 3
# 8 985152014315937 2017-11-27 3241 17 3
# 9 985152014315938 2017-11-24 584 17 4
#10 985152014315938 2017-11-25 1689 17 4
#11 985152014315938 2017-11-26 2807 17 4
#12 985152014315938 2017-11-27 2369 17 4
#13 985152014315938 2017-11-28 2576 17 4
#14 985152014315939 2017-11-25 1084 17 5
#15 985152014315939 2017-11-26 3489 17 5
#16 985152014315939 2017-11-27 2630 17 5
#17 985152014315939 2017-11-28 3585 17 5
#18 985152014315939 2017-11-29 3433 17 5
#19 985152014315939 2017-11-30 2962 17 5
样本数据
Sample data
df <- read.table(text =
"RFID visit_date ADFI location
985152014315936 2017-11-25 2133 16
985152014315936 2017-11-26 2186 16
985152014315936 2017-11-27 3489 16
985152014315936 2017-11-28 2432 16
985152014315937 2017-11-24 15 17
985152014315937 2017-11-25 1512 17
985152014315937 2017-11-26 2378 17
985152014315937 2017-11-27 3241 17
985152014315938 2017-11-24 584 17
985152014315938 2017-11-25 1689 17
985152014315938 2017-11-26 2807 17
985152014315938 2017-11-27 2369 17
985152014315938 2017-11-28 2576 17
985152014315939 2017-11-25 1084 17
985152014315939 2017-11-26 3489 17
985152014315939 2017-11-27 2630 17
985152014315939 2017-11-28 3585 17
985152014315939 2017-11-29 3433 17
985152014315939 2017-11-30 2962 17", header = T)
这篇关于R中同一个人的首个日期和最后一个日期之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文