Home > OS >  Extracting year and month from a POSIXct variable using pipes (Question)
Extracting year and month from a POSIXct variable using pipes (Question)

Time:02-15

I'm trying to extract the year and month from a POSIXct variable using data wrangling skills I recently learned. However it doesn't seem to work. I can do it the old way (just using format) but my pipes seem to be not working. Any help would be greatly appreciated. The output from my pipes looks kinda weird.

I posted information about the variable below.

-- Variable type: POSIXct ----------------------
# A tibble: 1 x 7
  skim_variable n_missing complete_rate
* <chr>             <int>         <dbl>
1 date                  0             1
  min                 max                
* <dttm>              <dttm>             
1 2014-05-02 00:00:00 2015-05-27 00:00:00
  median              n_unique
* <dttm>                 <int>
1 2014-10-16 00:00:00      372

My code which doesn't work.

dataset %>%
  select(date) %>%
  format(format = '%Y%m')


output:

 [1] "\033[38;5;246m# A tibble: 21,613 x 1\033[39m"                      
 [2] "   date               "                                            
 [3] "   \033[3m\033[38;5;246m<dttm>\033[39m\033[23m             "       
 [4] "\033[38;5;250m 1\033[39m 2014-10-13 \033[38;5;246m00:00:00\033[39m"
 [5] "\033[38;5;250m 2\033[39m 2014-12-09 \033[38;5;246m00:00:00\033[39m"
 [6] "\033[38;5;250m 3\033[39m 2015-02-25 \033[38;5;246m00:00:00\033[39m"
 [7] "\033[38;5;250m 4\033[39m 2014-12-09 \033[38;5;246m00:00:00\033[39m"
 [8] "\033[38;5;250m 5\033[39m 2015-02-18 \033[38;5;246m00:00:00\033[39m"
 [9] "\033[38;5;250m 6\033[39m 2014-05-12 \033[38;5;246m00:00:00\033[39m"
[10] "\033[38;5;250m 7\033[39m 2014-06-27 \033[38;5;246m00:00:00\033[39m"
[11] "\033[38;5;250m 8\033[39m 2015-01-15 \033[38;5;246m00:00:00\033[39m"
[12] "\033[38;5;250m 9\033[39m 2015-04-15 \033[38;5;246m00:00:00\033[39m"
[13] "\033[38;5;250m10\033[39m 2015-03-12 \033[38;5;246m00:00:00\033[39m"
[14] "\033[38;5;246m# ... with 21,603 more rows\033[39m"                 

code that works:

format(dataset$date, format = '%Y%m')

output:

   [1] "201410" "201412" "201502" "201412"
   [5] "201502" "201405" "201406" "201501"
   [9] "201504" "201503" "201504" "201405"
  [13] "201405" "201410" "201503" "201501"
  [17] "201407" "201405" "201412" "201504"
  [21] "201405" "201408" "201407" "201405"
  [25] "201411" "201411" "201406" "201412"
  [29] "201406" "201503" "201411" "201412"
  [33] "201406" "201411" "201412" "201406"
  [37] "201405" "201412" "201502" "201406"
  [41] "201407" "201408" "201407" "201410"
  [45] "201407" "201407" "201503" "201407"
  [49] "201504" "201503" "201409" "201502"
  [53] "201412" "201502" "201503" "201405"
  [57] "201408" "201504" "201408" "201502"
  [61] "201412" "201408" "201410" "201412"
  [65] "201406" "201411" "201409" "201410"
  [69] "201408" "201406" "201409" "201501"
  [73] "201406" "201407" "201503" "201411"
  [77] "201410" "201504" "201406" "201503"
  [81] "201412" "201412" "201410" "201501"
  [85] "201406" "201411" "201411" "201406"
  [89] "201405" "201409" "201405" "201503"
  [93] "201502" "201407" "201412" "201409"
  [97] "201503" "201409" "201407" "201405"
 [101] "201406" "201410" "201412" "201410"
 [105] "201409" "201504" "201405" "201407"
 [109] "201503" "201408" "201407" "201503"
 [113] "201409" "201411" "201410" "201411"
 [117] "201406" "201406" "201501" "201505"
 [121] "201501" "201411" "201504" "201411"
 [125] "201406" "201503" "201407" "201407"
 [129] "201406" "201504" "201501" "201504"
 [133] "201406" "201405" "201501" "201408"
 [137] "201408" "201405" "201407" "201405"
 [141] "201406" "201407" "201505" "201504"
 [145] "201502" "201412" "201406" "201408"
 [149] "201410" "201411" "201408" "201504"
 [153] "201503" "201504" "201405" "201407"
 [157] "201411" "201408" "201411" "201410"
 [161] "201405" "201503" "201503" "201406"
 [165] "201408" "201412" "201502" "201503"
 [169] "201406" "201406" "201503" "201501"
 [173] "201405" "201501" "201409" "201502"
 [177] "201407" "201405" "201411" "201501"
 [181] "201407" "201504" "201406" "201407"
 [185] "201409" "201411" "201504" "201405"
 [189] "201504" "201412" "201408" "201411"
 [193] "201409" "201406" "201412" "201405"
 [197] "201411" "201407" "201501" "201410"
 [201] "201503" "201411" "201407" "201408"
 [205] "201503" "201408" "201409" "201502"
 [209] "201411" "201407" "201504" "201407"
 [213] "201405" "201501" "201405" "201501"
 [217] "201502" "201407" "201408" "201410"
 [221] "201408" "201406" "201409" "201409"
 [225] "201409" "201406" "201503" "201405"
 [229] "201501" "201503" "201504" "201406"
 [233] "201411" "201411" "201409" "201406"
 [237] "201504" "201503" "201407" "201405"
 [241] "201412" "201410" "201501" "201504"
 [245] "201408" "201502" "201409" "201502"
 [249] "201503" "201504" "201409" "201405"
 [253] "201504" "201504" "201409" "201405"
 [257] "201410" "201502" "201411" "201408"
 [261] "201412" "201410" "201412" "201504"
 [265] "201410" "201405" "201409" "201408"
 [269] "201504" "201505" "201410" "201505"
 [273] "201410" "201409" "201411" "201503"
 [277] "201501" "201411" "201505" "201505"
 [281] "201407" "201502" "201505" "201406"
 [285] "201504" "201409" "201410" "201502"
 [289] "201407" "201408" "201407" "201502"
 [293] "201410" "201410" "201407" "201407"
 [297] "201408" "201503" "201406" "201405"
 [301] "201406" "201408" "201406" "201504"
 [305] "201501" "201502" "201504" "201406"
 [309] "201502" "201501" "201502" "201410"
 [313] "201405" "201406" "201412" "201411"
 [317] "201504" "201505" "201407" "201411"
 [321] "201405" "201410" "201407" "201502"
 [325] "201409" "201503" "201410" "201409"
 [329] "201504" "201407" "201409" "201504"
 [333] "201407" "201504" "201407" "201410"
 [337] "201406" "201407" "201408" "201505"
 [341] "201504" "201411" "201409" "201410"
 [345] "201407" "201407" "201412" "201412"
 [349] "201410" "201504" "201406" "201409"
 [353] "201501" "201412" "201503" "201406"
 [357] "201408" "201410" "201405" "201408"
 [361] "201406" "201408" "201412" "201408"
 [365] "201409" "201501" "201503" "201410"
 [369] "201504" "201411" "201412" "201410"
 [373] "201503" "201412" "201408" "201405"
 [377] "201406" "201410" "201504" "201504"
 [381] "201409" "201408" "201503" "201501"
 [385] "201407" "201411" "201406" "201405"
 [389] "201409" "201408" "201412" "201409"
 [393] "201406" "201405" "201407" "201504"
 [397] "201503" "201406" "201406" "201406"
 [401] "201503" "201408" "201504" "201502"
 [405] "201406" "201409" "201408" "201504"
 [409] "201504" "201409" "201503" "201407"
 [413] "201411" "201407" "201502" "201409"
 [417] "201406" "201501" "201412" "201407"
 [421] "201412" "201407" "201412" "201501"
 [425] "201407" "201504" "201411" "201502"
 [429] "201405" "201505" "201410" "201409"
 [433] "201407" "201409" "201409" "201409"
 [437] "201408" "201503" "201407" "201409"
 [441] "201409" "201407" "201407" "201504"
 [445] "201408" "201412" "201411" "201503"
 [449] "201410" "201411" "201505" "201412"
 [453] "201502" "201504" "201410" "201405"
 [457] "201501" "201411" "201406" "201501"
 [461] "201406" "201503" "201411" "201412"
 [465] "201411" "201405" "201412" "201410"
 [469] "201501" "201501" "201406" "201502"
 [473] "201411" "201410" "201408" "201408"
 [477] "201407" "201409" "201504" "201407"
 [481] "201411" "201406" "201503" "201410"
 [485] "201504" "201504" "201407" "201407"
 [489] "201412" "201502" "201411" "201502"
 [493] "201501" "201410" "201406" "201405"
 [497] "201501" "201409" "201408" "201411"
 [501] "201505" "201405" "201405" "201410"
 [505] "201502" "201405" "201405" "201502"
 [509] "201405" "201406" "201505" "201503"
 [513] "201407" "201411" "201408" "201503"
 [517] "201410" "201505" "201411" "201408"
 [521] "201503" "201409" "201503" "201411"
 [525] "201408" "201411" "201406" "201406"
 [529] "201411" "201410" "201504" "201505"
 [533] "201502" "201406" "201502" "201502"
 [537] "201505" "201412" "201504" "201503"
 [541] "201502" "201410" "201410" "201409"
 [545] "201411" "201504" "201405" "201503"
 [549] "201409" "201405" "201407" "201410"
 [553] "201505" "201409" "201411" "201407"
 [557] "201406" "201407" "201504" "201410"
 [561] "201410" "201407" "201412" "201406"
 [565] "201412" "201407" "201412" "201408"
 [569] "201410" "201412" "201407" "201501"
 [573] "201504" "201406" "201501" "201412"
 [577] "201412" "201412" "201501" "201412"
 [581] "201405" "201408" "201407" "201410"
 [585] "201411" "201407" "201408" "201407"
 [589] "201409" "201503" "201409" "201410"
 [593] "201503" "201409" "201502" "201406"
 [597] "201407" "201410" "201410" "201408"
 [601] "201410" "201502" "201408" "201408"
 [605] "201502" "201504" "201405" "201502"
 [609] "201411" "201504" "201412" "201411"
 [613] "201409" "201406" "201406" "201501"
 [617] "201505" "201409" "201501" "201406"
 [621] "201504" "201406" "201410" "201502"
 [625] "201405" "201408" "201408" "201504"
 [629] "201410" "201405" "201405" "201406"
 [633] "201412" "201409" "201409" "201406"
 [637] "201408" "201501" "201407" "201504"
 [641] "201503" "201407" "201503" "201505"
 [645] "201409" "201504" "201503" "201407"
 [649] "201411" "201504" "201411" "201411"
 [653] "201405" "201504" "201407" "201504"
 [657] "201411" "201405" "201501" "201409"
 [661] "201503" "201411" "201501" "201405"
 [665] "201411" "201411" "201405" "201409"
 [669] "201407" "201408" "201406" "201409"
 [673] "201503" "201407" "201408" "201407"
 [677] "201406" "201501" "201409" "201407"
 [681] "201408" "201405" "201406" "201410"
 [685] "201410" "201501" "201405" "201503"
 [689] "201409" "201502" "201408" "201409"
 [693] "201407" "201503" "201407" "201406"
 [697] "201412" "201409" "201406" "201410"
 [701] "201504" "201505" "201411" "201412"
 [705] "201408" "201408" "201504" "201410"
 [709] "201408" "201407" "201406" "201409"
 [713] "201407" "201409" "201503" "201412"
 [717] "201407" "201407" "201501" "201504"
 [721] "201408" "201407" "201408" "201504"
 [725] "201409" "201406" "201406" "201409"
 [729] "201411" "201405" "201406" "201409"
 [733] "201405" "201407" "201501" "201410"
 [737] "201502" "201408" "201411" "201411"
 [741] "201407" "201412" "201411" "201409"
 [745] "201406" "201501" "201407" "201406"
 [749] "201502" "201409" "201504" "201412"
 [753] "201407" "201408" "201406" "201409"
 [757] "201407" "201501" "201502" "201410"
 [761] "201405" "201409" "201503" "201502"
 [765] "201412" "201411" "201503" "201407"
 [769] "201501" "201406" "201406" "201411"
 [773] "201406" "201405" "201502" "201405"
 [777] "201406" "201410" "201405" "201406"
 [781] "201502" "201406" "201411" "201501"
 [785] "201412" "201503" "201504" "201411"
 [789] "201407" "201405" "201406" "201406"
 [793] "201407" "201410" "201503" "201407"
 [797] "201410" "201409" "201410" "201502"
 [801] "201408" "201405" "201501" "201501"
 [805] "201406" "201504" "201411" "201406"
 [809] "201409" "201408" "201501" "201410"
 [813] "201505" "201410" "201503" "201412"
 [817] "201412" "201505" "201408" "201407"
 [821] "201410" "201408" "201409" "201410"
 [825] "201502" "201408" "201505" "201409"
 [829] "201405" "201503" "201502" "201406"
 [833] "201406" "201409" "201411" "201503"
 [837] "201407" "201505" "201407" "201501"
 [841] "201412" "201411" "201405" "201501"
 [845] "201412" "201405" "201411" "201407"
 [849] "201503" "201412" "201405" "201410"
 [853] "201407" "201407" "201406" "201408"
 [857] "201410" "201503" "201406" "201411"
 [861] "201406" "201505" "201408" "201503"
 [865] "201411" "201503" "201504" "201407"
 [869] "201412" "201504" "201504" "201409"
 [873] "201503" "201502" "201405" "201406"
 [877] "201502" "201411" "201412" "201405"
 [881] "201408" "201502" "201410" "201409"
 [885] "201406" "201406" "201411" "201407"
 [889] "201408" "201407" "201406" "201411"
 [893] "201405" "201407" "201408" "201411"
 [897] "201405" "201408" "201503" "201504"
 [901] "201406" "201408" "201405" "201407"
 [905] "201502" "201406" "201503" "201410"
 [909] "201410" "201408" "201411" "201410"
 [913] "201409" "201409" "201405" "201407"
 [917] "201410" "201412" "201408" "201405"
 [921] "201406" "201501" "201503" "201406"
 [925] "201405" "201412" "201405" "201503"
 [929] "201505" "201408" "201409" "201503"
 [933] "201504" "201502" "201407" "201503"
 [937] "201502" "201406" "201410" "201501"
 [941] "201503" "201406" "201410" "201408"
 [945] "201408" "201501" "201502" "201504"
 [949] "201503" "201410" "201505" "201503"
 [953] "201407" "201405" "201504" "201409"
 [957] "201503" "201409" "201409" "201412"
 [961] "201411" "201504" "201409" "201412"
 [965] "201405" "201407" "201406" "201405"
 [969] "201406" "201408" "201410" "201505"
 [973] "201502" "201407" "201405" "201408"
 [977] "201408" "201503" "201501" "201503"
 [981] "201503" "201504" "201503" "201409"
 [985] "201405" "201405" "201410" "201408"
 [989] "201504" "201502" "201410" "201503"
 [993] "201501" "201505" "201407" "201408"
 [997] "201409" "201408" "201504" "201409"
 [ reached getOption("max.print") -- omitted 20613 entries ]

CodePudding user response:

select returns a dataframe whereas format requires a vector. The correct function for you would be pull if you want to use pipes -

library(dplyr)

dataset %>%
  pull(date) %>%
  format(format = '%Y%m')
  •  Tags:  
  • r
  • Related