子集数据和计算时差

时间:2021-12-21 02:58:41

I have the following data frame and I want to calculate time difference between oldest and newest dates for each user.

我有以下的数据框架,我想计算每个用户的最老日期和最新日期之间的时间差。

> data1
      date    user  PC
1 1/4/2010 BAC0081 PC1
2 1/5/2010 BAC0081 PC2
3 1/6/2010 BAC0081 PC3
4 1/7/2010 BAC0081 PC4
5 1/4/2010 BAC0082 PC5
6 1/5/2010 BAC0082 PC6
7 1/6/2010 BAC0082 PC7
> 

Results I would expect is;

我期望的结果是;

BAC0081 3 days

BAC0081 3天

BAC0082 2 days

BAC0082 2天

I couldn't find an efficient way to perform this function. Can anyone suggests me a proper way of doing this pls.

我找不到一个有效的方法来执行这个函数。谁能给我一个合适的方法来做这件事吗?

Thanks

谢谢

1 个解决方案

#1


0  

if you have your date column formatted correctly:

如果你的日期栏格式正确:

lapply(split(data1[, 1], data1[, 2]), function(x) range(x)[2] - range(x)[1])

#1


0  

if you have your date column formatted correctly:

如果你的日期栏格式正确:

lapply(split(data1[, 1], data1[, 2]), function(x) range(x)[2] - range(x)[1])