简体   繁体   中英

How to get max and min datetimes + file names from other column with same uuid in R?

I have the following dataframe :

 structure(list(file_name = c("~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-25-24-969", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-25-38-884", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-26-01-234", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-26-23-952", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-26-37-520", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-27-00-154", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-27-22-204", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-27-39-318", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-28-06-422", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-28-30-670", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-28-57-492", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-12-803", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-13-950", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-15-182", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-16-359", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-17-531", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-18-917", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-20-145", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-21-257", 
"~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5fad61c99d/2018-06-23-18-29-22-394", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-49-32-124", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-49-48-754", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-49-59-367", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-50-47-729", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-50-52-040", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-50-58-451", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-05-001", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-13-309", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-16-512", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-27-163", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-33-459", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-40-163", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-49-846", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-51-57-522", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-03-679", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-10-994", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-16-210", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-21-959", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-29-112", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-35-695", 
"~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f79415605c6/2018-06-20-14-52-58-029", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-25-38-400", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-26-31-819", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-26-48-863", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-26-55-659", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-27-04-613", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-27-14-283", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-27-23-962", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-27-31-777", 
"~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2eeb88212155/2018-06-24-06-27-41-483"
), dt = structure(c(17705, 17705, 17705, 17705, 17705, 17705, 
17705, 17705, 17705, 17705, 17705, 17705, 17705, 17705, 17705, 
17705, 17705, 17705, 17705, 17705, 17702, 17702, 17702, 17702, 
17702, 17702, 17702, 17702, 17702, 17702, 17702, 17702, 17702, 
17702, 17702, 17702, 17702, 17702, 17702, 17702, 17702, 17706, 
17706, 17706, 17706, 17706, 17706, 17706, 17706, 17706), class = "Date"), 
    t = structure(c(66324, 66338, 66361, 66383, 66397, 66420, 
    66442, 66459, 66486, 66510, 66537, 66552, 66553, 66555, 66556, 
    66557, 66558, 66560, 66561, 66562, 53372, 53388, 53399, 53447, 
    53452, 53458, 53465, 53473, 53476, 53487, 53493, 53500, 53509, 
    53517, 53523, 53530, 53536, 53541, 53549, 53555, 53578, 23138, 
    23191, 23208, 23215, 23224, 23234, 23243, 23251, 23261), class = c("hms", 
    "difftime"), units = "secs"), uuid = c("0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "0b27ea5fad61c99d", "0b27ea5fad61c99d", 
    "0b27ea5fad61c99d", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "11485f79415605c6", "11485f79415605c6", 
    "11485f79415605c6", "326c2eeb88212155", "326c2eeb88212155", 
    "326c2eeb88212155", "326c2eeb88212155", "326c2eeb88212155", 
    "326c2eeb88212155", "326c2eeb88212155", "326c2eeb88212155", 
    "326c2eeb88212155"), date_time = structure(c(1529778324, 
    1529778338, 1529778361, 1529778383, 1529778397, 1529778420, 
    1529778442, 1529778459, 1529778486, 1529778510, 1529778537, 
    1529778552, 1529778553, 1529778555, 1529778556, 1529778557, 
    1529778558, 1529778560, 1529778561, 1529778562, 1529506172, 
    1529506188, 1529506199, 1529506247, 1529506252, 1529506258, 
    1529506265, 1529506273, 1529506276, 1529506287, 1529506293, 
    1529506300, 1529506309, 1529506317, 1529506323, 1529506330, 
    1529506336, 1529506341, 1529506349, 1529506355, 1529506378, 
    1529821538, 1529821591, 1529821608, 1529821615, 1529821624, 
    1529821634, 1529821643, 1529821651, 1529821661), class = c("POSIXct", 
    "POSIXt"), tzone = "UTC")), .Names = c("file_name", "dt", 
"t", "uuid", "date_time"), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

What I want is to get maximum and minimum date_time for the files BUT!: All my files are different but not the uuid. For example there can be 10 files with same uuid but different time stamps.

What I want is to get for same uuid maximum and minimum date_times and their corresponding file names. For example desired output for the first 20 rows (the file names corresponds to 0b27ea5fad61c99d uuid) I want to get 2 datetimes - the maximum and minumum.

Same for all others.

I have tried something like this but it doesn't answer my need:

df %>% group_by(date_time) %>% summarize(min_dt = min(dt), max_dt = max(dt))

I am trying to play with dplyr and tidyr. Please advise.

Perhaps I misunderstood, but isn't this just

df %>%
    group_by(uuid) %>%
    summarise(min_dt = min(dt), max_dt = max(dt))
## A tibble: 3 x 3
#  uuid             min_dt     max_dt
#  <chr>            <date>     <date>
#1 0b27ea5fad61c99d 2018-06-23 2018-06-23
#2 11485f79415605c6 2018-06-20 2018-06-20
#3 326c2eeb88212155 2018-06-24 2018-06-24

Storing the data you provided as a data frame df :

df %>% 
  dplyr::group_by(uuid) %>% 
  dplyr::filter(date_time %in% c(max(date_time), min(date_time)))

# A tibble: 6 x 5
# Groups:   uuid [3]
file_name                                                     dt         t       uuid        date_time          
<chr>                                                         <date>     <time>  <chr>       <dttm>             
1 ~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5~ 2018-06-23 18:25:~ 0b27ea5fad~ 2018-06-23 18:25:24
2 ~/Steves_Files/AWS/DeviceID/Android/0b27ea5fad61c99d/0b27ea5~ 2018-06-23 18:29:~ 0b27ea5fad~ 2018-06-23 18:29:22
3 ~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f7~ 2018-06-20 14:49:~ 11485f7941~ 2018-06-20 14:49:32
4 ~/Steves_Files/AWS/DeviceID/Android/11485f79415605c6/11485f7~ 2018-06-20 14:52:~ 11485f7941~ 2018-06-20 14:52:58
5 ~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2ee~ 2018-06-24 06:25:~ 326c2eeb88~ 2018-06-24 06:25:38
6 ~/Steves_Files/AWS/DeviceID/Android/326c2eeb88212155/326c2ee~ 2018-06-24 06:27:~ 326c2eeb88~ 2018-06-24 06:27:41

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM