Convert column in data.frame to date

家住魔仙堡 提交于 2019-11-28 01:33:33
akrun

Do the transformations within mutate

df2 %>%
   group_by(a1) %>%
   mutate(b2=as.Date(b2, format = "%d.%m.%Y"))
#    a1         b2    c3    d3
#  (chr)     (date) (chr) (int)
#1     a 2015-01-01    1a     1
#2     a 2015-02-02    2b     2
#3     b 2012-02-14    3c     3
#4     b 2008-08-16    4d     4
#5     c 2003-06-17    5e     5
#6     d 2015-01-31    6f     6
#7     e 2022-01-07    7g     7
#8     e 2001-05-09    8h     8

If we need to do only the transformation, we don't need to group by 'a1'.

mutate(df2, b2= as.Date(b2, format= "%d.%m.%Y")

By using %<>% operator from magrittr, we can transform in place.

df2 %<>%
  mutate(b2= as.Date(b2, format= "%d.%m.%Y")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!