R vaqtida belgilangan vaqt oralig'ida vaqt tamg'asini qo'shish

ID va vaqt tamg'asi bilan ma'lumotlar to'plami bor.

Har bir IDda odatda har bir daqiqa uchun 12 marta vaqt belgilari kuzatiladi. Har bir daqiqa uchun har bir vaqt tamg'asiga 5 soniya oralig'i qo'shib, qiyin vaqt o'tkazyapman.

Actual Data                     Desired Format

    ID       Time           ID        Time     
    4466    12/1/14 19:56   4466    12/1/14 19:56:00
    4466    12/1/14 19:56   4466    12/1/14 19:56:05
    4466    12/1/14 19:56   4466    12/1/14 19:56:10
    4466    12/1/14 19:56   4466    12/1/14 19:56:15
    4466    12/1/14 19:56   4466    12/1/14 19:56:20
    4466    12/1/14 19:56   4466    12/1/14 19:56:25
    4466    12/1/14 19:56   4466    12/1/14 19:56:30
    4466    12/1/14 19:56   4466    12/1/14 19:56:35
    4466    12/1/14 19:56   4466    12/1/14 19:56:40
    4466    12/1/14 19:56   4466    12/1/14 19:56:45
    4466    12/1/14 19:56   4466    12/1/14 19:56:50
    4466    12/1/14 19:56   4466    12/1/14 19:56:55
    1136    3/23/15 23:00   1136    3/23/15 23:00:00
    1136    3/23/15 23:00   1136    3/23/15 23:00:05
    1136    3/23/15 23:00   1136    3/23/15 23:00:10
    1136    3/23/15 23:01   1136    3/23/15 23:01:00
    1136    3/23/15 23:01   1136    3/23/15 23:01:05
    1136    3/23/15 23:01   1136    3/23/15 23:01:10
    1136    3/23/15 23:01   1136    3/23/15 23:01:15
    1136    3/23/15 23:01   1136    3/23/15 23:01:20
    1136    3/23/15 23:01   1136    3/23/15 23:01:25
    1136    3/23/15 23:01   1136    3/23/15 23:01:30
    1136    3/23/15 23:01   1136    3/23/15 23:01:35
    1136    3/23/15 23:01   1136    3/23/15 23:01:40
    1136    3/23/15 23:01   1136    3/23/15 23:01:45
    1136    3/23/15 23:01   1136    3/23/15 23:01:50
    1136    3/23/15 23:01   1136    3/23/15 23:01:55
1
DAT $ Time <- as.POSIXct (dat $ Time, format = "% m /% d /% y") ma'lumotlaringizni haqiqiy POSIXct datetime ob'ektiga aylantira boshlashni boshlayman. % H:% M ", tz =" UTC ") - matni faqat xatolar uchun kutmoqda.
qo'shib qo'ydi muallif thelatemail, manba

6 javoblar

Buni ma'lumotlar.table bilan qilishimiz mumkin. "ID" va "Vaqt" bilan guruhlangan "ma'lumotlar.trad" ( setDT (df1) ) ga 'ma'lumotlar.frame' faylini aylantirish uchun 'Vaqtni' har 5 soniyada sprintf "NewTime" ustunini yaratish uchun

library(ma'lumotlar.table)
setDT(df1)[, NewTime := sprintf("%s:%02d", Time, 
          seq(0, 55, by = 5)[seq_len(.N)]) , .(ID, Time)]
df1
#      ID          Time          NewTime
# 1: 4466 12/1/14 19:56 12/1/14 19:56:00
# 2: 4466 12/1/14 19:56 12/1/14 19:56:05
# 3: 4466 12/1/14 19:56 12/1/14 19:56:10
# 4: 4466 12/1/14 19:56 12/1/14 19:56:15
# 5: 4466 12/1/14 19:56 12/1/14 19:56:20
# 6: 4466 12/1/14 19:56 12/1/14 19:56:25
# 7: 4466 12/1/14 19:56 12/1/14 19:56:30
# 8: 4466 12/1/14 19:56 12/1/14 19:56:35
# 9: 4466 12/1/14 19:56 12/1/14 19:56:40
#10: 4466 12/1/14 19:56 12/1/14 19:56:45
#11: 4466 12/1/14 19:56 12/1/14 19:56:50
#12: 4466 12/1/14 19:56 12/1/14 19:56:55
#13: 1136 3/23/15 23:00 3/23/15 23:00:00
#14: 1136 3/23/15 23:00 3/23/15 23:00:05
#15: 1136 3/23/15 23:00 3/23/15 23:00:10
#16: 1136 3/23/15 23:01 3/23/15 23:01:00
#17: 1136 3/23/15 23:01 3/23/15 23:01:05
#18: 1136 3/23/15 23:01 3/23/15 23:01:10
#19: 1136 3/23/15 23:01 3/23/15 23:01:15
#20: 1136 3/23/15 23:01 3/23/15 23:01:20
#21: 1136 3/23/15 23:01 3/23/15 23:01:25
#22: 1136 3/23/15 23:01 3/23/15 23:01:30
#23: 1136 3/23/15 23:01 3/23/15 23:01:35
#24: 1136 3/23/15 23:01 3/23/15 23:01:40
#25: 1136 3/23/15 23:01 3/23/15 23:01:45
#26: 1136 3/23/15 23:01 3/23/15 23:01:50
#27: 1136 3/23/15 23:01 3/23/15 23:01:55

ma'lumotlar

df1 <- structure(list(ID = c(4466L, 4466L, 4466L, 4466L, 4466L, 4466L, 
4466L, 4466L, 4466L, 4466L, 4466L, 4466L, 1136L, 1136L, 1136L, 
1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 
1136L, 1136L, 1136L), Time = c("12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "3/23/15 23:00", "3/23/15 23:00", 
"3/23/15 23:00", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01")), .Names = c("ID", "Time"), row.names = c(NA, 
-27L), class = "ma'lumotlar.frame")
1
qo'shib qo'ydi

Buni ma'lumotlar.table bilan qilishimiz mumkin. "ID" va "Vaqt" bilan guruhlangan "ma'lumotlar.trad" ( setDT (df1) ) ga 'ma'lumotlar.frame' faylini aylantirish uchun 'Vaqtni' har 5 soniyada sprintf "NewTime" ustunini yaratish uchun

library(ma'lumotlar.table)
setDT(df1)[, NewTime := sprintf("%s:%02d", Time, 
          seq(0, 55, by = 5)[seq_len(.N)]) , .(ID, Time)]
df1
#      ID          Time          NewTime
# 1: 4466 12/1/14 19:56 12/1/14 19:56:00
# 2: 4466 12/1/14 19:56 12/1/14 19:56:05
# 3: 4466 12/1/14 19:56 12/1/14 19:56:10
# 4: 4466 12/1/14 19:56 12/1/14 19:56:15
# 5: 4466 12/1/14 19:56 12/1/14 19:56:20
# 6: 4466 12/1/14 19:56 12/1/14 19:56:25
# 7: 4466 12/1/14 19:56 12/1/14 19:56:30
# 8: 4466 12/1/14 19:56 12/1/14 19:56:35
# 9: 4466 12/1/14 19:56 12/1/14 19:56:40
#10: 4466 12/1/14 19:56 12/1/14 19:56:45
#11: 4466 12/1/14 19:56 12/1/14 19:56:50
#12: 4466 12/1/14 19:56 12/1/14 19:56:55
#13: 1136 3/23/15 23:00 3/23/15 23:00:00
#14: 1136 3/23/15 23:00 3/23/15 23:00:05
#15: 1136 3/23/15 23:00 3/23/15 23:00:10
#16: 1136 3/23/15 23:01 3/23/15 23:01:00
#17: 1136 3/23/15 23:01 3/23/15 23:01:05
#18: 1136 3/23/15 23:01 3/23/15 23:01:10
#19: 1136 3/23/15 23:01 3/23/15 23:01:15
#20: 1136 3/23/15 23:01 3/23/15 23:01:20
#21: 1136 3/23/15 23:01 3/23/15 23:01:25
#22: 1136 3/23/15 23:01 3/23/15 23:01:30
#23: 1136 3/23/15 23:01 3/23/15 23:01:35
#24: 1136 3/23/15 23:01 3/23/15 23:01:40
#25: 1136 3/23/15 23:01 3/23/15 23:01:45
#26: 1136 3/23/15 23:01 3/23/15 23:01:50
#27: 1136 3/23/15 23:01 3/23/15 23:01:55

ma'lumotlar

df1 <- structure(list(ID = c(4466L, 4466L, 4466L, 4466L, 4466L, 4466L, 
4466L, 4466L, 4466L, 4466L, 4466L, 4466L, 1136L, 1136L, 1136L, 
1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 1136L, 
1136L, 1136L, 1136L), Time = c("12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", "12/1/14 19:56", 
"12/1/14 19:56", "12/1/14 19:56", "3/23/15 23:00", "3/23/15 23:00", 
"3/23/15 23:00", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", "3/23/15 23:01", 
"3/23/15 23:01")), .Names = c("ID", "Time"), row.names = c(NA, 
-27L), class = "ma'lumotlar.frame")
1
qo'shib qo'ydi

dplyr dan group_by() va row_number() dan foydalanishingiz mumkin:

library(dplyr)
df %>% 
  group_by(ID,Time ) %>%
  mutate(NTime=sprintf("%s.%02d",Time, row_number(Time)*5))
1
qo'shib qo'ydi

dplyr dan group_by() va row_number() dan foydalanishingiz mumkin:

library(dplyr)
df %>% 
  group_by(ID,Time ) %>%
  mutate(NTime=sprintf("%s.%02d",Time, row_number(Time)*5))
1
qo'shib qo'ydi

Yoki faqat:

df <- data.frame(ID = 4466, Time = "12/1/14 19:56") 
df2 <- cbind(df[rep(1 : nrow(df),each=12),], ntime = strptime (paste(df$Time, ":", seq(0, 55, 5),sep=""), "%m/%d/%y %H:%M:%s", tz= "UTC"))
0
qo'shib qo'ydi

Yoki faqat:

df <- data.frame(ID = 4466, Time = "12/1/14 19:56") 
df2 <- cbind(df[rep(1 : nrow(df),each=12),], ntime = strptime (paste(df$Time, ":", seq(0, 55, 5),sep=""), "%m/%d/%y %H:%M:%s", tz= "UTC"))
0
qo'shib qo'ydi