将季度数据转换为每月数据

时间:2020-10-22 15:18:43

标签: r date

我需要通过将一些变量除以3将每季度的数据转换为每月的数据。

示例数据集:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private LdapProperties ldapProperties;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider authenticationProvider =
                new ActiveDirectoryLdapAuthenticationProvider(ldapProperties.getDomain(), ldapProperties.getProviderUrl());

        authenticationProvider.setConvertSubErrorCodesToExceptions(true);
        authenticationProvider.setUseAuthenticationRequestCredentials(true);
        //if you're not happy on the default searchFilter, you can set your own. See https://docs.spring.io/spring-security/site/docs/4.2.18.RELEASE/apidocs/org/springframework/security/ldap/authentication/ad/ActiveDirectoryLdapAuthenticationProvider.html#setSearchFilter-java.lang.String-
        authenticationProvider.setSearchFilter("(&(objectClass=user)(cn={1}))");
        return authenticationProvider;
    }
...
}

我需要的是每个季度获得几个月的时间,即最终数据集看起来像这样:

df <- data.frame(Year = c(2018,2019,2020), qtr = c(1,3,2),
 amount = c(3,6,12), variable = c(5,6,7))
df

还有一个额外的问题,如何在这种环境下打印数据框

2 个答案:

答案 0 :(得分:2)

这项工作:

df %>% 
  mutate(qtr_start_mth = case_when(qtr == 1 ~ 1,
                                   qtr == 2 ~ 4,
                                   qtr == 3 ~ 7,
                                   qtr == 4 ~ 10),
         qtr_end_mth = case_when(qtr == 1 ~ 3,
                                 qtr == 2 ~ 6, 
                                 qtr == 3 ~ 9, 
                                 qtr == 4 ~ 12)) %>% 
  mutate(month = map2(qtr_start_mth, qtr_end_mth, `:`)) %>% 
  separate_rows() %>% 
  unnest(month) %>% 
  mutate(amount = amount /3) %>% 
  select(1,2,3,4,7)

# A tibble: 9 x 5
   Year   qtr amount variable month
  <dbl> <dbl>  <dbl>    <dbl> <int>
1  2018     1      1        5     1
2  2018     1      1        5     2
3  2018     1      1        5     3
4  2019     3      2        6     7
5  2019     3      2        6     8
6  2019     3      2        6     9
7  2020     2      4        7     4
8  2020     2      4        7     5
9  2020     2      4        7     6

使用的数据:

> dput(df)
structure(list(Year = c(2018, 2019, 2020), qtr = c(1, 3, 2), 
    amount = c(3, 6, 12), variable = c(5, 6, 7)), class = "data.frame", row.names = c(NA, 
-3L))
> 

答案 1 :(得分:1)

使用 base

do.call(rbind, 
        c(make.row.names = FALSE,
          lapply(split(df, df$Year), function(i){
            cbind(i, month = 1:3 + (i$qtr - 1) * 3, row.names = NULL)
          })))

#   Year qtr amount variable month
# 1 2018   1      3        5     1
# 2 2018   1      3        5     2
# 3 2018   1      3        5     3
# 4 2019   3      6        6     7
# 5 2019   3      6        6     8
# 6 2019   3      6        6     9
# 7 2020   2     12        7     4
# 8 2020   2     12        7     5
# 9 2020   2     12        7     6
相关问题