简体   繁体   English

在 Rstudio 中分隔字符串的年月日

[英]Separate the day, month and year of a string in Rstudio

How can i extract of the column "location" the date.我如何从“位置”列中提取日期。 In the first observation the date is: 2020-03-01第一次观察日期为:2020-03-01

    location      Basel    Basel.1    Basel.2
10  20200301T0000  13.240529  5.3905287   8.775112
11  20200302T0000  11.390529  3.0305285   6.665945
12  20200303T0000   8.100529 0.93052864  4.7905283
13  20200304T0000  10.440529  2.3605285  6.6859455
14  20200305T0000  11.570529  5.5705285  7.9667783
dat <- tibble::tribble(
~location,      ~Basel,    ~Basel.1,    ~Basel.2,
"20200301T0000",  13.240529,  5.3905287,   8.775112,
"20200302T0000",  11.390529,  3.0305285,   6.665945,
"20200303T0000",   8.100529, 0.93052864,  4.7905283,
"20200304T0000",  10.440529,  2.3605285,  6.6859455,
"20200305T0000",  11.570529,  5.5705285,  7.9667783
)

library(dplyr)
library(stringr)
library(lubridate)
   
mutate(dat, location = location %>% 
  str_remove("T0000") %>% 
  ymd())

# A tibble: 5 x 4
  location   Basel Basel.1 Basel.2
  <date>     <dbl>   <dbl>   <dbl>
1 2020-03-01 13.2    5.39     8.78
2 2020-03-02 11.4    3.03     6.67
3 2020-03-03  8.10   0.931    4.79
4 2020-03-04 10.4    2.36     6.69
5 2020-03-05 11.6    5.57     7.97

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM