Home > Enterprise >  How to extract the first letter of the past, actual and before monthly names in R?
How to extract the first letter of the past, actual and before monthly names in R?

Time:10-04

I need to input an column in R using dplyr::mutate that can return to me the first letters of before month actual month next month.

My dataframe is looks like this:

library(dplyr)

Months <- c(

"jan-50",
"feb-50",
"mar-50",
"apr-50",
"may-50",
"jun-50",
"jul-50",
"aug-50",
"sep-50",
"oct-50",
"nov-50",
"dec-50"

)

three_months <- c(

"DJF", # Is the same as D = December J = January and F = February
"JFM", # Is the same as J = January, F = February, M = March and so on...
"FMA",
"MAM",
"AMJ",
"MJJ",
"JJA",
"JAS",
"ASO",
"SON",
"OND",
"NDJ"

)

df <- data.frame(Months, three_months)
df
#Months three_months
#1  jan-50          DJF
#2  feb-50          JFM
#3  mar-50          FMA
#4  apr-50          MAM
#5  may-50          AMJ
#6  jun-50          MJJ
#7  jul-50          JJA
#8  aug-50          JAS
#9  sep-50          ASO
#10 oct-50          SON
#11 nov-50          OND
#12 dec-50          NDJ

df <- df %>%
mutate(three_months = 
       # Formula to initial letters of the past month   actual month   next month
)

How did I can I use this formula to access the past and the next values of each month relative initials letters ?

I only can make for the actual month by using something like:

df <- df%>%
mutate(
   
three_months = abbreviate(Data, 1, strict=TRUE),
three_months = gsub('[1 2]', '', `3 months`),
three_months = toupper(`3 months`) # Shows the time t month initial letter
  
)

Thanks!

CodePudding user response:

I think the easiest way is using a lookup vector:

lookup <- c("jan"= "DJF",
            "feb" = "JFM",
            "mar" = "FMA",
            "apr" = "MAM",
            "may" = "AMJ",
            "jun" = "MJJ",
            "jul" = "JJA",
            "aug" = "JAS",
            "sep" = "ASO",
            "oct" = "SON",
            "nov" = "OND",
            "dec" = "NDJ")

library(dplyr)
library(stringr)

df %>% 
  mutate(three_months = lookup[str_replace(Months, "(\\w )-.*", "\\1")])

This returns

   Months three_months
1  jan-50          DJF
2  feb-50          JFM
3  mar-50          FMA
4  apr-50          MAM
5  may-50          AMJ
6  jun-50          MJJ
7  jul-50          JJA
8  aug-50          JAS
9  sep-50          ASO
10 oct-50          SON
11 nov-50          OND
12 dec-50          NDJ

CodePudding user response:

By seen the @Martin Gal answer, I thought:

df <- df %>%  
mutate(`3 months` = 
       
       case_when( 
         month(Months) ==  1 ~ "DJF",
         month(Months) ==  2 ~ "JFM",    
         month(Months) ==  3 ~ "FMA",
         month(Months) ==  4 ~ "MAM",
         month(Months) ==  5 ~ "AMJ",
         month(Months) ==  6 ~ "MJJ",
         month(Months) ==  7 ~ "JJA",
         month(Months) ==  8 ~ "JAS",
         month(Months) ==  9 ~ "ASO",
         month(Months) ==  10 ~ "SON",
         month(Months) ==  11 ~ "OND",
         month(Months) ==  12 ~ "NDJ",
                               )
     )

As I´m expecting an cyclical monthly time series the case_when here works well too.

CodePudding user response:

We may do this with regex_left_join

library(fuzzyjoin)
regex_left_join(tibble(Months), 
    stack(setNames(three_months, tolower(month.abb))), 
         by = c("Months" = "ind")) %>% 
    select(-ind)
# A tibble: 12 × 2
   Months values
   <chr>  <chr> 
 1 jan-50 DJF   
 2 feb-50 JFM   
 3 mar-50 FMA   
 4 apr-50 MAM   
 5 may-50 AMJ   
 6 jun-50 MJJ   
 7 jul-50 JJA   
 8 aug-50 JAS   
 9 sep-50 ASO   
10 oct-50 SON   
11 nov-50 OND   
12 dec-50 NDJ   
  • Related