去掉数字月份前面多余的0

发布于 2025-01-10 21:53:24 字数 555 浏览 1 评论 0原文

我有一个 df ,其中有一列以 character 格式存储日期,我想提取月份。为此,我使用以下内容:

mutate(
    
    Date = as.Date(
      
      str_remove(Timestamp, "_.*")
      
      ),
    
    Month = month(
      
      Date, 
      
      label = F)
    
  ) 

但是,十月、十一月和十二月在存储时在月份前面有一个额外的零。 lubridate 库无法识别它。如何调整上面的代码来解决这个问题?这是我的 Timestamp 列:

c("2021-010-01_00h39m", "2021-010-01_01h53m", "2021-010-01_02h36m", 
"2021-010-01_10h32m", "2021-010-01_10h34m", "2021-010-01_14h27m"
)

I have a df with a column which has dates stored in character format, for which I want to extract the months. For this I use the following:

mutate(
    
    Date = as.Date(
      
      str_remove(Timestamp, "_.*")
      
      ),
    
    Month = month(
      
      Date, 
      
      label = F)
    
  ) 

However, the October, November and December are stored with an extra zero in front of the month. The lubridate library doesn't recognise it. How can I adjust the code above to fix this? This is my Timestamp column:

c("2021-010-01_00h39m", "2021-010-01_01h53m", "2021-010-01_02h36m", 
"2021-010-01_10h32m", "2021-010-01_10h34m", "2021-010-01_14h27m"
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2025-01-17 21:53:24

首先将值转换为日期并使用 format 从中获取月份。

format(as.Date(x, '%Y-0%m-%d'), '%b')
#[1] "Oct" "Oct" "Oct" "Oct" "Oct" "Oct"

%b 给出缩写的月份名称,您也可以根据您的选择使用 %B%m

format(as.Date(x, '%Y-0%m-%d'), '%B')
#[1] "October" "October" "October" "October" "October" "October"

format(as.Date(x, '%Y-0%m-%d'), '%m')
#[1] "10" "10" "10" "10" "10" "10"

First convert the values to date and use format to get months from it.

format(as.Date(x, '%Y-0%m-%d'), '%b')
#[1] "Oct" "Oct" "Oct" "Oct" "Oct" "Oct"

%b gives abbreviated month name, you may also use %B or %m depending on your choice.

format(as.Date(x, '%Y-0%m-%d'), '%B')
#[1] "October" "October" "October" "October" "October" "October"

format(as.Date(x, '%Y-0%m-%d'), '%m')
#[1] "10" "10" "10" "10" "10" "10"
自由范儿 2025-01-17 21:53:24

一种方法是使用 strsplit 提取第二个元素:

month.abb[readr::parse_number(sapply(strsplit(x, split = '-'), "[[", 2))]

它将返回:

#"Oct" "Oct" "Oct" "Oct" "Oct" "Oct"

数据:

c("2021-010-01_00h39m", "2021-010-01_01h53m", "2021-010-01_02h36m", 
  "2021-010-01_10h32m", "2021-010-01_10h34m", "2021-010-01_14h27m"
) -> x

One way would be use strsplit to extract the second element:

month.abb[readr::parse_number(sapply(strsplit(x, split = '-'), "[[", 2))]

which will return:

#"Oct" "Oct" "Oct" "Oct" "Oct" "Oct"

data:

c("2021-010-01_00h39m", "2021-010-01_01h53m", "2021-010-01_02h36m", 
  "2021-010-01_10h32m", "2021-010-01_10h34m", "2021-010-01_14h27m"
) -> x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文