I need to go through all years and define new days by the starting hour. My code right now looks like this:
starting_hour <- 5
hours_in_day <- 48
for (x in unique(data$Year)) {
a = 1
while (a <= nrow(filter(data, data$Year == x)) && (data$Year == x)) {
if ((data[a, "Hours"] == starting_hour) && (a < 48)) {
for (c in 1:floor((nrow(filter(data, data$Year == x))-starting_hour)/hours_in_day)) {
for (b in 0:(hours_in_day-1)) {
data[a, "Hours"] <- b
data[a, "Date"] <- c
a <- a+1
}
}
next
}
else {
data[a, "Date"] <- 0
a <- a+1
if (data[a, "Year"] != x) {
break
}
next
}
}
}`
Right now this only works with the first year. I don't understand why it doesn't go through all years. Here's first 50 rows of my data:
> dput(data)
structure(list(Date = structure(c(1419984000, 1388534400, 1388534400,
1388534400, 1388534400, 1388534400, 1388534400, 1388534400, 1388534400,
1388534400, 1388534400, 1388534400, 1388534400, 1388534400, 1388534400,
1388534400, 1388534400, 1388534400, 1388534400, 1388534400, 1388534400,
1388534400, 1388534400, 1388534400, 1388534400, 1388620800, 1388620800,
1388620800, 1388620800, 1388620800, 1388620800, 1388620800, 1388620800,
1388620800, 1388620800, 1388620800, 1388620800, 1388620800, 1388620800,
1388620800, 1388620800, 1388620800, 1388620800, 1388620800, 1388620800,
1388620800, 1388620800, 1388620800, 1388620800, 1388707200), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Year = c(2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014,
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014),
Hours = c(23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 0)), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))
Aucun commentaire:
Enregistrer un commentaire