Search code examples
rif-statementgroup-bydplyrcase-when

Making two new variables with case_when or else if statement (R coding)


I need to make some changes to the data frame to be able to create a visual that I want, but I'm stuck. Down below is how the data frame looks like. What I want to do is to make a new variable named 'article'. The values of this variable will be sum_pageviews of the 'Article Based' values from the 'Page' column. Likewise, another variable will be 'info', and its values will be sum_pageviews from 'Informational Based' in Page variable. Then the length of the data frame will be 8 in the end without the repetitiveness of department, and that's what I want to see.

enter image description here

Image down below is how new dataframe is supposed to be look like. I entered on Excel, but I'd like to know code in R.

enter image description here

--> I think I should start with

sum_views%>%group_by(department)%>%mutate

then ifelse or case_when statement, but no idea. Please let me know the codes. Thanks in advance


Solution

  • Does this work:

    library(dplyr)
    library(tidyr)
    df %>% mutate(article = case_when(str_detect(Page, 'Article Based') ~ sum_pageviews, TRUE ~ NA_real_), 
    Info = case_when(str_detect(Page, 'Information Based') ~ sum_pageviews , TRUE ~ NA_real_)) %>% 
    fill(article, .direction = 'down') %>% fill(Info, .direction = 'up') %>% select(-c(Page, sum_pageviews)) %>% 
    group_by(department) %>% filter(row_number() == 1)
    # A tibble: 5 x 3
    # Groups:   department [5]
      department article  Info
      <chr>        <dbl> <dbl>
    1 Hess          1261  8443
    2 Econ          2320 15682
    3 DA            2495  7262
    4 CS            5096  9870
    5 Theatre       2992  5764
    

    Data used:

    df
    # A tibble: 10 x 3
       Page              department sum_pageviews
       <chr>             <chr>              <dbl>
     1 Article Based     Hess                1261
     2 Information Based Hess                8443
     3 Article Based     Econ                2320
     4 Information Based Econ               15682
     5 Article Based     DA                  2495
     6 Information Based DA                  7262
     7 Article Based     CS                  5096
     8 Information Based CS                  9870
     9 Article Based     Theatre             2992
    10 Information Based Theatre             5764