提问人:Venkatram Bachoti 提问时间:11/14/2022 最后编辑:R18Venkatram Bachoti 更新时间:11/16/2022 访问量:245
如何在 R 中从日期时间(列)创建日期(列)
How to create a date (column) from a date-time (column) in R
问:
我导入了一个CSV,其中包含“Activity_Date_Minute”列中的日期。例如,日期值为“04/12/2016 01:12:00”。现在,当我将 .csv 读入数据帧并仅提取日期时,这会在列中为我提供日期为 4-12-20。有人可以帮忙如何在单独的列中获取 mm-dd-yyyy 中的日期吗?
尝试了以下代码。期待看到一个日期为04/12/2016(月/日/年)的列。
#Installing packages
install.packages("tidyverse")
library(tidyverse)
install.packages('ggplot2')
library(ggplot2)
install.packages("dplyr")
library(dplyr)
install.packages("lubridate")
library(lubridate)
##Installing packages
install.packages("tidyverse")
library(tidyverse)
install.packages('ggplot2')
library(ggplot2)
install.packages("dplyr")
library(dplyr)
install.packages("lubridate")
library(lubridate)
##Reading minute-wise METs into "minutewiseMET_Records" and summarizing MET per day for all the IDs
minutewiseMET_Records <- read.csv("minuteMETsNarrow_merged.csv")
str(minutewiseMET_Records)
## converting column ID to character,Activity_Date_Minute to date
minutewiseMET_Records$Id <- as.character(minutewiseMET_Records$Id)
minutewiseMET_Records$Date <- as.Date(minutewiseMET_Records$Activity_Date_Minute)
str(minutewiseMET_Records)
控制台如下:
> minutewiseMET_Records <- read.csv("minuteMETsNarrow_merged.csv")
> str(minutewiseMET_Records)
'data.frame': 1048575 obs. of 3 variables:
$ Id : num 1.5e+09 1.5e+09 1.5e+09 1.5e+09 1.5e+09 ...
$ Activity_Date_Minute: chr "04/12/2016 00:00" "04/12/2016 00:01" "04/12/2016 00:02" "04/12/2016 00:03" ...
$ METs : int 10 10 10 10 10 12 12 12 12 12 ...
> ## converting column ID to character,Activity_Date_Minute to date
> minutewiseMET_Records$Id <- as.character(minutewiseMET_Records$Id)
> minutewiseMET_Records$Date <- as.Date(minutewiseMET_Records$Activity_Date_Minute)
> ## converting column ID to character,Activity_Date_Minute to date
> minutewiseMET_Records$Id <- as.character(minutewiseMET_Records$Id)
> minutewiseMET_Records$Date <- as.Date(minutewiseMET_Records$Activity_Date_Minute)
> str(minutewiseMET_Records)
'data.frame': 1048575 obs. of 4 variables:
$ Id : chr "1503960366" "1503960366" "1503960366" "1503960366" ...
$ Activity_Date_Minute: chr "04/12/2016 00:00" "04/12/2016 00:01" "04/12/2016 00:02" "04/12/2016 00:03" ...
$ METs : int 10 10 10 10 10 12 12 12 12 12 ...
$ Date : Date, format: "4-12-20" "4-12-20" ...
>
答:
0赞
zephryl
11/14/2022
#1
Activity_Date_Minute
不是初始数据中的日期时间,而是字符。因此,您必须首先将其转换为日期时间(例如,使用 ),然后使用 .lubridate::mdy_hm()
as.Date()
library(dplyr)
library(lubridate)
minutewiseMET_Records %>%
mutate(
Activity_Date_Minute = mdy_hm(Activity_Date_Minute),
Activity_Date = as.Date(Activity_Date_Minute)
)
# A tibble: 4 × 2
Activity_Date_Minute Activity_Date
<dttm> <date>
1 2016-04-12 00:00:00 2016-04-12
2 2016-04-12 00:01:00 2016-04-12
3 2016-04-12 00:02:00 2016-04-12
4 2016-04-12 00:03:00 2016-04-12
1赞
R18
11/14/2022
#2
我认为这对你有用
minutewiseMET_Records$Date <- format(as.Date(minutewiseMET_Records$Activity_Date_Minute, format = "%d/%m/%Y"),"%m/%d/%Y")
首先,你要说的是初始数据的格式。然后,你问它哪种格式是你想要的输出格式。
评论
0赞
Venkatram Bachoti
11/18/2022
我在数据帧上遵循上述语法进行相同的操作。dailyactivity_Records <- read.csv(“dailyActivity_calories_intensities_steps.csv”) view(dailyactivity_Records)
评论
install.packages()
install.packages("tidyverse")
library(tidyverse)
library()