Guest User

Untitled

a guest
Jan 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. countMonths <- function(day, month, cycle.days) {
  2. month.days <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  3. if (month < 1 | month > 12 | floor(month) != month) {
  4. cat("Invalid month value, must be an integer from 1 to 12")
  5. } else if (day < 1 | day > month.days[month]) {
  6. cat("Invalid day value, must be between 1 and month.days[month]")
  7. } else if (cycle.days < 0) {
  8. cat("Invalid cycle.days value, must be >= 0")
  9. } else {
  10. nmonths <- 1
  11. day.ct <- cycle.days - day
  12. while (day.ct > 0) {
  13. nmonths <- nmonths + 1
  14. month <- ifelse(month == 1, 12, month - 1) # sets to previous month
  15. day.ct <- day.ct - month.days[month] # subtracts days of previous month
  16. }
  17. nmonths
  18. }
  19. }
  20.  
  21. > head(cons2[-1],10)
  22. kwh cycle.days read.date row.index year month day kwh.per.day
  23. 1 381 29 2010-09-02 1 2010 9 2 13.137931
  24. 2 280 32 2010-10-04 2 2010 10 4 8.750000
  25. 3 282 29 2010-11-02 3 2010 11 2 9.724138
  26. 4 330 34 2010-12-06 4 2010 12 6 9.705882
  27. 5 371 30 2011-01-05 5 2011 1 5 12.366667
  28. 6 405 30 2011-02-04 6 2011 2 4 13.500000
  29. 7 441 32 2011-03-08 7 2011 3 8 13.781250
  30. 8 290 29 2011-04-06 8 2011 4 6 10.000000
  31. 9 296 29 2011-05-05 9 2011 5 5 10.206897
  32. 10 378 32 2011-06-06 10 2011 6 6 11.812500
  33.  
  34. > dput(head(cons2[-1],10))
  35. structure(list(kwh = c(381L, 280L, 282L, 330L, 371L, 405L, 441L,
  36. 290L, 296L, 378L), cycle.days = c(29L, 32L, 29L, 34L, 30L, 30L,
  37. 32L, 29L, 29L, 32L), read.date = structure(c(1283385600, 1286150400,
  38. 1288656000, 1291593600, 1294185600, 1296777600, 1299542400, 1302048000,
  39. 1304553600, 1307318400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
  40. row.index = 1:10, year = c(2010, 2010, 2010, 2010, 2011,
  41. 2011, 2011, 2011, 2011, 2011), month = c(9, 10, 11, 12, 1,
  42. 2, 3, 4, 5, 6), day = c(2L, 4L, 2L, 6L, 5L, 4L, 8L, 6L, 5L,
  43. 6L), kwh.per.day = c(13.1379310344828, 8.75, 9.72413793103448,
  44. 9.70588235294118, 12.3666666666667, 13.5, 13.78125, 10, 10.2068965517241,
  45. 11.8125)), .Names = c("kwh", "cycle.days", "read.date", "row.index",
  46. "year", "month", "day", "kwh.per.day"), row.names = c(NA, 10L
  47. ), class = "data.frame")
  48.  
  49. > cons2$tot.months <- countMonths(cons2$day, cons2$month, cons2$cycle.days)
  50. Warning messages:
  51. 1: In if (month < 1 | month > 12 | floor(month) != month) { :
  52. the condition has length > 1 and only the first element will be used
  53. 2: In if (day < 1 | day > month.days[month]) { :
  54. the condition has length > 1 and only the first element will be used
  55. 3: In if (cycle.days < 0) { :
  56. the condition has length > 1 and only the first element will be used
  57. 4: In while (day.ct > 0) { :
  58. the condition has length > 1 and only the first element will be used
  59. 5: In while (day.ct > 0) { :
  60. the condition has length > 1 and only the first element will be used
  61.  
  62. cons2 <- ddply(cons2, .(account, year, month, day), transform,
  63. tot.months = countMonths(day, month, cycle.days)
  64. )
  65.  
  66. mapply(countMonths,cons2$day,cons2$month,cons2$cycle.days)
  67.  
  68. cons2$read.date=as.Date(cons2$read.date)
  69. monnb <- function(d){ lt <- as.POSIXlt(as.Date(d, origin="1900-01-01")); lt$year*12 + lt$mon }
  70. mondf <- function(d1, d2) monnb(d2) - monnb(d1)
  71. mondf(cons2$read.date-cons2$cycle.days,cons2$read.date) + 1
  72.  
  73. countMonths <- function(day, month, cycle.days) {
  74. month.days <- c(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
  75. stopifnot(month >=1 & month <= 12 & floor(month)==month & cycle.days >=0 & day >= 1 & day <= month.days[month])
  76. nmonths <- 1
  77. day.ct <- cycle.days - day
  78. while (day.ct > 0) {
  79. nmonths <- nmonths + 1
  80. month <- ifelse(month == 1, 12, month - 1) # sets to previous month
  81. day.ct <- day.ct - month.days[month] # subtracts days of previous month
  82. }
  83. nmonths
  84. }
Add Comment
Please, Sign In to add comment