Advertisement
celestialgod

time format transform

Oct 13th, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.78 KB | None | 0 0
  1. library(dplyr)
  2. minDate <- as.POSIXct("1990-01-01")
  3.  
  4. N <- 1000
  5. p <- 100
  6. dat <- lapply(1:p, function(x) {
  7.   if (x > p*0.1){
  8.     rnorm(N)
  9.   } else {
  10.     as.integer(Sys.time()) - sample(2e8, N)
  11.   }
  12. }) %>% `names<-`(paste0("V", 1:p)) %>% as_data_frame
  13.  
  14. possibleTimeCols <- sapply(dat[1, ], function(x){
  15.   as.POSIXct(x, origin = "1970-01-01", tz = "Asia/Taipei") >= minDate
  16. }) %>% {names(.)[.]}
  17.  
  18. datOut <- dat %>% mutate_each_(funs(as.POSIXct(., origin = "1970-01-01", tz = "Asia/Taipei")),
  19.                      vars = possibleTimeCols)
  20. print(datOut)
  21. # Source: local data frame [1,000 x 100]
  22. #
  23. #                     V1                  V2                  V3
  24. #                 <time>              <time>              <time>
  25. # 1  2011-12-15 20:55:45 2012-04-18 23:16:08 2012-10-13 13:38:58
  26. # 2  2013-03-15 11:05:22 2013-07-28 18:11:49 2011-03-26 14:46:51
  27. # 3  2015-03-03 13:54:22 2011-06-18 05:31:24 2012-03-26 14:53:00
  28. # 4  2015-11-03 01:30:12 2011-11-02 01:21:49 2013-11-05 15:16:48
  29. # 5  2012-11-18 12:06:22 2012-04-05 16:28:43 2010-09-12 14:24:42
  30. # 6  2012-01-24 03:38:18 2014-11-09 11:54:44 2016-05-30 21:50:26
  31. # 7  2010-08-03 22:27:15 2016-01-04 00:05:41 2014-02-16 00:53:00
  32. # 8  2013-05-14 12:44:14 2011-12-24 22:13:11 2012-01-17 18:22:01
  33. # 9  2011-05-14 16:28:25 2011-04-23 07:36:51 2011-05-01 04:31:10
  34. # 10 2010-07-02 17:25:18 2010-08-26 05:15:27 2013-12-31 21:10:19
  35.  
  36. # data.table解法
  37. library(data.table)
  38. library(pipeR)
  39. minDate <- as.POSIXct("1990-01-01")
  40.  
  41.  
  42. N <- 1000
  43. p <- 100  
  44. dat <- lapply(1:p, function(x) {
  45.   if (x > p*0.1){
  46.     rnorm(N)
  47.   } else {
  48.     as.integer(Sys.time()) - sample(2e8, N)
  49.   }
  50. }) %>>% as.data.table
  51.  
  52. possibleTimeCols <- sapply(dat[1, ], function(x){
  53.     as.POSIXct(x, origin = "1970-01-01", tz = "Asia/Taipei") >= minDate
  54. }) %>>% (names(.)[.])
  55.  
  56. dateDT <- dat[ , lapply(.SD, function(x) as.POSIXct(x, origin = "1970-01-01", tz = "Asia/Taipei")) ,
  57.      .SDcols = possibleTimeCols]
  58. dat[ , `:=`(eval(possibleTimeCols), dateDT)] %>>% print
  59. #                        V1                  V2                  V3
  60. #    1: 2014-04-29 22:17:22 2013-07-12 11:37:04 2013-12-19 13:01:16
  61. #    2: 2010-11-15 19:23:53 2016-01-28 05:17:19 2013-06-16 15:04:38
  62. #    3: 2013-04-14 18:16:29 2012-10-25 07:32:09 2012-09-30 05:00:31
  63. #    4: 2010-10-19 02:10:24 2015-04-02 19:09:34 2012-09-11 20:30:54
  64. #    5: 2012-09-10 02:39:22 2014-05-18 23:02:04 2015-11-19 21:44:06
  65. #   ---                                                            
  66. #  996: 2016-09-20 12:48:47 2016-10-08 11:42:33 2013-08-07 23:56:46
  67. #  997: 2011-12-19 12:23:57 2011-01-23 02:55:16 2015-12-25 23:17:27
  68. #  998: 2013-08-29 22:00:48 2013-07-16 04:39:07 2014-03-17 08:53:01
  69. #  999: 2011-11-13 19:55:30 2010-08-08 16:43:12 2012-01-24 10:58:16
  70. # 1000: 2015-08-07 16:55:23 2015-11-22 21:17:57 2014-03-05 10:13:36
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement