提取“ mm-dd-yyy”之后的日常和年格式不正确多变的
我正在尝试根据“日期”变量的字符串值生成一天,月和年变量,该变量的格式为“ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
或使用
base r
We can use
Or with
base R