Growstart and growend represent months during a growing season. I know that it can't be considered as a date object. The time column is the growing season time range.
How can I get the number of months (values) between growstart and growend?
Thank you.
structure(list(gid = c(100468L, 100468L, 100468L, 100468L, 100468L,
104078L, 104078L, 104078L, 104078L, 104078L, 104078L, 115615L,
115615L, 115615L, 115615L, 115615L, 115615L), country = c("Namibia",
"Namibia", "Namibia", "Namibia", "Namibia", "Namibia", "Namibia",
"Namibia", "Namibia", "Namibia", "Namibia", "Congo, Democratic Republic of (Zaire)",
"Congo, Democratic Republic of (Zaire)", "Congo, Democratic Republic of (Zaire)",
"Congo, Democratic Republic of (Zaire)", "Congo, Democratic Republic of (Zaire)",
"Congo, Democratic Republic of (Zaire)"), xcoord = c("13.75",
"13.75", "13.75", "13.75", "13.75", "18.75", "18.75", "18.75",
"18.75", "18.75", "18.75", "27.25", "27.25", "27.25", "27.25",
"27.25", "27.25"), ycoord = c("-20.25", "-20.25", "-20.25", "-20.25",
"-20.25", "-17.75", "-17.75", "-17.75", "-17.75", "-17.75", "-17.75",
"-9.75", "-9.75", "-9.75", "-9.75", "-9.75", "-9.75"), km2 = c("3282.64642089",
"3282.64642089", "3282.64642089", "3282.64642089", "3282.64642089",
"3233.1051487", "3233.1051487", "3233.1051487", "3233.1051487",
"3233.1051487", "3233.1051487", "3122.97751422", "3122.97751422",
"3122.97751422", "3122.97751422", "3122.97751422", "3122.97751422"
), maincrop = c(52L, 52L, 52L, 52L, 52L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 37L, 37L, 37L), crop = c("Others annual",
"Others annual", "Others annual", "Others annual", "Others annual",
"Cassava", "Cassava", "Cassava", "Cassava", "Cassava", "Cassava",
"Cassava", "Cassava", "Cassava", "Cassava", "Cassava", "Cassava"
), growstart = c(12L, 12L, 12L, 12L, 12L, 9L, 9L, 9L, 9L, 9L,
9L, 5L, 5L, 5L, 5L, 5L, 5L), growend = c(4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 12L, 12L), Time = structure(c(4352,
4383, 4717, 4414, 4442, 9862, 10135, 10166, 10196, 9893, 9921,
13057, 13088, 13118, 12904, 12935, 12965), class = "Date"), harvestYear = c(1982L,
1982L, 1983L, 1982L, 1982L, 1997L, 1998L, 1998L, 1998L, 1997L,
1997L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L)), row.names = c(1L,
2L, 3L, 4L, 5L, 40000L, 40001L, 40002L, 40003L, 40004L, 40005L,
200000L, 200001L, 200002L, 200003L, 200004L, 200005L), class = "data.frame")
PS: I have tried to create columns as date-growstart and date-growend. But I failed to find a convenient result. For example when growstart is 12 (December) and growend is 4 (April) it gives -8.
vhi.crop.gs$Datestart<-gsub(" ","",paste(vhi.crop.gs$harvestYear,"-",vhi.crop.gs$growstart,"-","01"))
vhi.crop.gs$Datestart<-as.Date(vhi.crop.gs$Datestart,format="%Y-%m-%d")
vhi.crop.gs$Dateend<-gsub(" ","",paste(vhi.crop.gs$harvestYear,"-",vhi.crop.gs$growend,"-","01"))
vhi.crop.gs$Dateend<-as.Date(vhi.crop.gs$Dateend,format="%Y-%m-%d")
vhi.crop.gs %>%
group_by(gid, country, xcoord, ycoord, crop, maincrop, harvestYear)
mutate(Months_difference = (interval(ymd(Datestart),
ymd(Dateend))) %/% months(1))
CodePudding user response:
library(dplyr)
df |>
mutate(months_diff = ifelse(
growend - growstart < 0,
growend - growstart 12,
growend - growstart
))