在R中从excel导入的处理日期和时间
我有硬拷贝文件中的数据,这些数据必须先手动输入excel文件,然后在R中处理。 数据包含不同受试者在不同日期(2014年8月18日、2014年8月19日)的不同时间点(如08:10、08:20等)的多个变量读数(通过ID)。每个阅读系列都有参考开始时间(如08:00)和参考开始日期(如2014年8月18日) 包含数据的excel文件如下所示在R中从excel导入的处理日期和时间,excel,date,time,rstudio,datetime-conversion,Excel,Date,Time,Rstudio,Datetime Conversion,我有硬拷贝文件中的数据,这些数据必须先手动输入excel文件,然后在R中处理。 数据包含不同受试者在不同日期(2014年8月18日、2014年8月19日)的不同时间点(如08:10、08:20等)的多个变量读数(通过ID)。每个阅读系列都有参考开始时间(如08:00)和参考开始日期(如2014年8月18日) 包含数据的excel文件如下所示 ID Reading Date Time Ref/Start Time Ref/Start Date 1 12.1 18.
ID Reading Date Time Ref/Start Time Ref/Start Date
1 12.1 18.08.2014 7:59 8:00 18.08.2014
1 26.34 18.08.2014 8:10 8:00 18.08.2014
1 35.2 18.08.2014 8:20 8:00 18.08.2014
1 30 18.08.2014 8:30 8:00 18.08.2014
1 12 19.08.2014 8:00 8:00 18.08.2014
1 13 19.08.2014 20:00 8:00 18.08.2014
1 12 20.08.2014 8:00 8:00 18.08.2014
这些数据以后必须在R中进行处理。我的目标是生成一个新列,其中包含每个阅读序列的开始时间点之后的每个阅读时间(以小时为单位)。也就是说,得到(y)和(x),其中(x)是从开始算起的时间,单位为小时。
我现在将这个excel文件导入到R中(以前保存为.csv),但我不知道现在应该如何在R中生成新列!我是否应该先以另一种方式在excel中插入数据
我希望我成功地澄清了我需要什么,并希望我能从某人那里得到帮助
非常感谢。有很多方法可以实现这一点。这里有一个 假设您在名为
time\u d.csv
的csv文件中有数据,您可以执行以下操作:
a1=read.csv("time_d.csv") #reads data into R data frame
a1$date_read=paste(a1$Date, a1$Time, sep=" ") #adds a new col to data frame
#by merging two existing cols
a1$date_ref=paste(a1$Ref_date, a1$Ref_time, sep=" ") #adds new col
a1=subset(a1,select=-c(Date,Time)) #removes the no longer needed cols
a1=subset(a1,select=-c(Ref_date,Ref_time)) #removes the no longer needed cols
a1$date_read=as.POSIXct(strptime(a1$date_read,"%d.%m.%Y %H:%M" )) #convert
#to date/time objects
a1$date_ref=as.POSIXct(strptime(a1$date_ref,"%d.%m.%Y %H:%M" ))
a1$Duration=difftime(a1$date_read,a1$date_ref, units="hours") #adds new col
#calculating the time difference in hours
time_d.csv如下所示:
ID Reading Date Time Ref_time Ref_date
1 12.1 18.08.2014 07:59 08:00 18.08.2014
1 26.34 18.08.2014 08:10 08:00 18.08.2014
1 35.2 18.08.2014 08:20 08:00 18.08.2014
1 30 18.08.2014 08:30 08:00 18.08.2014
1 12 19.08.2014 08:00 08:00 18.08.2014
1 13 19.08.2014 20:00 08:00 18.08.2014
1 12 20.08.2014 08:00 08:00 18.08.2014
ID Reading date_read date_ref Duration
1 1 12.10 2014-08-18 07:59:00 2014-08-18 08:00:00 -0.01666667 hours
2 1 26.34 2014-08-18 08:10:00 2014-08-18 08:00:00 0.16666667 hours
3 1 35.20 2014-08-18 08:20:00 2014-08-18 08:00:00 0.33333333 hours
4 1 30.00 2014-08-18 08:30:00 2014-08-18 08:00:00 0.50000000 hours
5 1 12.00 2014-08-19 08:00:00 2014-08-18 08:00:00 24.00000000 hours
6 1 13.00 2014-08-19 20:00:00 2014-08-18 08:00:00 36.00000000 hours
7 1 12.00 2014-08-20 08:00:00 2014-08-18 08:00:00 48.00000000 hours
您可以看到,我已稍微更改了列标题。然后,使用此格式的.csv,您可以执行以下操作:
a1=read.csv("time_d.csv") #reads data into R data frame
a1$date_read=paste(a1$Date, a1$Time, sep=" ") #adds a new col to data frame
#by merging two existing cols
a1$date_ref=paste(a1$Ref_date, a1$Ref_time, sep=" ") #adds new col
a1=subset(a1,select=-c(Date,Time)) #removes the no longer needed cols
a1=subset(a1,select=-c(Ref_date,Ref_time)) #removes the no longer needed cols
a1$date_read=as.POSIXct(strptime(a1$date_read,"%d.%m.%Y %H:%M" )) #convert
#to date/time objects
a1$date_ref=as.POSIXct(strptime(a1$date_ref,"%d.%m.%Y %H:%M" ))
a1$Duration=difftime(a1$date_read,a1$date_ref, units="hours") #adds new col
#calculating the time difference in hours
对于您的特定数据,日期的格式对于此行很重要:
as.POSIXct(strtime(a1$date_读取,“%d.%m.%Y%H:%m”)
如果您更改了日期的格式,那么您也应该更改R中的行代码
最终结果如下所示:
ID Reading Date Time Ref_time Ref_date
1 12.1 18.08.2014 07:59 08:00 18.08.2014
1 26.34 18.08.2014 08:10 08:00 18.08.2014
1 35.2 18.08.2014 08:20 08:00 18.08.2014
1 30 18.08.2014 08:30 08:00 18.08.2014
1 12 19.08.2014 08:00 08:00 18.08.2014
1 13 19.08.2014 20:00 08:00 18.08.2014
1 12 20.08.2014 08:00 08:00 18.08.2014
ID Reading date_read date_ref Duration
1 1 12.10 2014-08-18 07:59:00 2014-08-18 08:00:00 -0.01666667 hours
2 1 26.34 2014-08-18 08:10:00 2014-08-18 08:00:00 0.16666667 hours
3 1 35.20 2014-08-18 08:20:00 2014-08-18 08:00:00 0.33333333 hours
4 1 30.00 2014-08-18 08:30:00 2014-08-18 08:00:00 0.50000000 hours
5 1 12.00 2014-08-19 08:00:00 2014-08-18 08:00:00 24.00000000 hours
6 1 13.00 2014-08-19 20:00:00 2014-08-18 08:00:00 36.00000000 hours
7 1 12.00 2014-08-20 08:00:00 2014-08-18 08:00:00 48.00000000 hours
非常感谢。。它确实起作用了:)我真的非常感谢你的帮助,非常感谢你的回答。。。竖起大拇指