Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201919

Recorder Dataset in R based on Country

$
0
0
structure(list(Date = c("KW 52 / 2016", "KW 1 / 2017", "KW 2 / 2017", 
"KW 3 / 2017"), Sales_AT = c(150L, 169L, 143L, 170L), Sales_CH = c(150L, 
169L, 143L, 170L), Sales_GER = c(150L, 169L, 143L, 170L), Sales_HUN = c(134L, 
139L, NA, 125L), Sales_JP = c(134L, NA, 142L, 125L), Sales_POL = c(127L, 
175L, 150L, 141L), Sales_SWE = c(125L, NA, 159L, 131L), Sales_USA = c(169L, 
159L, NA, 132L), difference_AT = c(NA, 19L, -26L, 27L), difference_CH = c(NA, 
19L, -26L, 27L), difference_GER = c(NA, 19L, -26L, 27L), difference_HUN = c(NA, 
5L, NA, -14L), difference_JP = c(NA, NA, 8L, -17L), difference_POL = c(NA, 
48L, -25L, -9L), difference_SWE = c(NA, NA, 34L, -28L), difference_USA = c(NA, 
-10L, NA, -27L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-4L))

This is my dataset which looks like this:

A tibble: 4 x 17
  Date         Sales_AT Sales_CH Sales_GER Sales_HUN Sales_JP Sales_POL Sales_SWE Sales_USA difference_AT difference_CH difference_GER difference_HUN difference_JP difference_POL difference_SWE difference_USA
  <chr>           <int>    <int>     <int>     <int>    <int>     <int>     <int>     <int>         <int>         <int>          <int>          <int>         <int>          <int>          <int>          <int>
1 KW 52 / 2016      150      150       150       134      134       127       125       169            NA            NA             NA             NA            NA             NA             NA             NA
2 KW 1 / 2017       169      169       169       139       NA       175        NA       159            19            19             19              5            NA             48             NA            -10
3 KW 2 / 2017       143      143       143        NA      142       150       159        NA           -26           -26            -26             NA             8            -25             34             NA
4 KW 3 / 2017       170      170       170       125      125       141       131       132            27            27             27            -14           -17             -9            -28            -27

I want to reorder the dataset to have the sales and difference column of each country next to each other.

I´m look for a dplyr solution which works like this, but in a dynamic way:

wide_result %>%
    select(contains("AT"), contains("CH"), contains("HUN"), contains("JP"), contains("USA"))

Can anyone help me?


Viewing all articles
Browse latest Browse all 201919

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>