0

I have a data frame containing 11 variables and nearly 100 000 of obs.

'July 2010/11', 'Aug 2010/11', 'Sep 2010/11', 'Oct 2010/11', 'Dec 2010/11'
'Jan 2010/11','Feb 2010/11' , 'Mar 2010/11', 'Apr 2010/11','May'2010/11','June 2010/11

My financial Month starts from July and end in June of the Next year. Here my Financial Year starts from July 2010 and ends in June 2011. I want to convert these Month -Financial Year time into Month-Actual Year.

Like 'July 2010/11' into July 2010 'Jan 2010/11' into Jan 2011 I relevel my OM variable according to financial year

levels(mydata$OM)<-c('July','Aug','Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May','Jun')

enter image description here

What i want to do is that i want to merge FY and OM variable into a variable in such a way that if it is July 2010/11 it only takes July 2010 and if its Jan 2010/11 it will become Jan 2011. I tried below code but its not working properly as it is giving me incorrect result

for(i in 1:NROW(mydata))
{
if(SD[i,3]=='2010/11'| SD[i,3]=='2011/12' | SD[i,3]=='2012/13'
   |SD[i,3]=='2013/14' | SD[i,3]=='2014/15' | SD[i,3]=='2015/16' & SD[i,4] %in% c('July','Aug','Sep','Oct','Nov','Dec'))
 {
  SD[i,12] <- paste(sortedData[i,4],sortedData[i,3],sep = ' ')
  SD[i,12]<- str_sub(string =SD[i,12] , start = 1, end = -4)
}
else{
  SD[i,12] <-paste(sortedData[i,4],sortedData[i,3],sep = ' ')
  SD[i,12] <-str_replace(string = SD[i,12], pattern = '\\d{4}/',replacement = '20')   
}
}
James Z
  • 12,209
  • 10
  • 24
  • 44
learner
  • 828
  • 2
  • 19
  • 36

1 Answers1

0

IF you have data as shown in the table you can use ifelse statement and then substr() on the FY.

 mydata$year <- ifelse(as.numeric(mydata$OM) < 7,
     substr(mydata$FY, 1, 4),
     paste0("20", substr(mydata$FY, 6, 7)))

If your problem would be more reproducible, I could test it, but otherwise you need to adjust it by yourself.

zielinskipp
  • 120
  • 5