提取“ mm-dd-yyy”之后的日常和年格式不正确多变的

发布于 2025-01-20 10:22:40 字数 1179 浏览 0 评论 0原文

我正在尝试根据“日期”变量的字符串值生成一天,月和年变量,该变量的格式为“ 27-02-2012”或“ dd-mm-yyyy”。

#Loading packages
library(tidyverse)
library(readxl)
library(writexl)
library(stringr)
library(textclean)
library(lubridate)
#library(zoo)

我的变量存储如下:

sapply(data_corpus, class)
    post        date    username 
"character" "character" "character"

要提取并生成白天,月和年的单独变量,我运行了此操作:

#Converting date variable
#data_corpus$date <- as_date(data_corpus$date)

但是,这将我在“日期”变量中的所有值转变为NAS。因此,我还尝试运行此功能,它可以与一个月合作。

#Creating day, month, year variables 
data_corpus$day <- day(data_corpus$date)
data_corpus$month <- month(data_corpus$date)
data_corpus$year <- year(data_corpus$date)

但是,像“ 27-02-2012”这样的日期将 提取如下,这意味着该月的提取正确,但是从原始“日期”变量的日子值中提取“年”,我不确定如何产生“ Day”的值?

   "date"        day   month    year
"27-02-2012"      20    2        27

以下是在创建上述3个变量之后存储变量的方式:

sapply(data_corpus, class)
      post        date    username         day       month        year 
"character" "character" "character"   "integer"   "numeric"   "numeric" 

I am trying to generate day, month, and year variables based on the string values of a "date" variable, which is formatted as "27-02-2012" or "DD-MM-YYYY".

#Loading packages
library(tidyverse)
library(readxl)
library(writexl)
library(stringr)
library(textclean)
library(lubridate)
#library(zoo)

My variables are stored as follows:

sapply(data_corpus, class)
    post        date    username 
"character" "character" "character"

To extract and generate separate variables for day, month, and year, I ran this:

#Converting date variable
#data_corpus$date <- as_date(data_corpus$date)

But this turns all of my values in the "date" variable into NAs. So I also tried running this, which works well with month.

#Creating day, month, year variables 
data_corpus$day <- day(data_corpus$date)
data_corpus$month <- month(data_corpus$date)
data_corpus$year <- year(data_corpus$date)

However, a date like "27-02-2012" would
be extracted as follows, which means that month is extracted correctly, but "year" was extracted from the day values in the original "date" variable, and I am not sure how's the value for the "day" been generated?

   "date"        day   month    year
"27-02-2012"      20    2        27

Here is how the variables are stored after creating the 3 variables above:

sapply(data_corpus, class)
      post        date    username         day       month        year 
"character" "character" "character"   "integer"   "numeric"   "numeric" 

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

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

发布评论

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

评论(1

凡尘雨 2025-01-27 10:22:40

我们可以使用

library(lubridate)
data_corpus$date <- dmy(data_corpus$date)

或使用base r

data_corpus$date <- as.Date(data_corpus$date, "%d-%m-%Y")

We can use

library(lubridate)
data_corpus$date <- dmy(data_corpus$date)

Or with base R

data_corpus$date <- as.Date(data_corpus$date, "%d-%m-%Y")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文