按日期和主题嵌套的变量 - EMA数据集

时间:2018-03-15 14:52:45

标签: r datetime

我创建了RespNum&使用以下代码的RespDay变量(请参阅______________________开始)

现在我只需要执行以下任务:创建一个名为'Day'的变量,该变量由subject和date嵌套

数据样本:(点击here下载)

ParticipantId DateTime_local      RespNum RespDay
 <chr>         <dttm>                <int>   <int>
1 1001    2017-10-20 18:42:00       1       1
2 1001    2017-10-20 20:24:00       2       2
3 1001    2017-10-20 23:12:00       3       3
4 1001    2017-10-21 01:23:00       4       1
5 1001    2017-10-21 13:32:00       5       2
6 1001    2017-10-21 15:17:00       6       3
7 1001    2017-10-21 17:32:00       7       4
8 1001    2017-10-21 20:23:00       8       5
9 1001    2017-10-21 22:57:00       9       6
10 1001    2017-10-22 01:54:00      10       1

___________用于创建RespNum&amp;的代码RespDay ______________________

data = dataset

在正确的时区创建新变量

data <- data %>%
mutate(DateTime = mdy_hm(DateTime),
       DateTime_local = force_tz(DateTime, tzone = "America/New_York"))

创建RespNum

此变量是主题的响应数。

data <- data %>%
group_by(ParticipantId) %>%
mutate(RespNum = row_number(DateTime_local)) %>%
ungroup() %>%
arrange(ParticipantId, RespNum, DateTime_local) # arrange data

data %>% select(ParticipantId, DateTime_local, RespNum) #view data

分割日期&amp;时间分为两列

data$date <- sapply(strsplit(as.character(data$DateTime_local), " "), "[", 1)
data$time <- sapply(strsplit(as.character(data$DateTime_local), " "), "[", 2)

更改日期格式并保存为数字日期

(data$date <- ymd(data$date)) #change to date format
class(data$date) #check that it is stored as date
as.numeric(data$date) #save date as numeric
class(data$date) #check that it is still date

创建RespDay变量

ID =分组变量

data$ID <- data$ParticipantId

日期=日期(不是日期+时间)

创建包含主题ID和日期

的变量
data$ID_DAY<-paste(data$ID,as.numeric(data$date),sep="") 

data <- data %>% 
group_by(ID_DAY) %>%
mutate(RespDay = row_number(date)) %>%
ungroup() %>%
arrange(ParticipantId, RespNum, RespDay, DateTime_local) # arrange data

data %>% select(ParticipantId, DateTime_local, RespNum, RespDay) #view data

'Day'变量应该是参与者响应的第一天的1系列,参与者响应的第2天的2系列等等。

所以使用上面的数据子集: ParticipantId DateTime_local RespNum RespDay Day

 <chr>         <dttm>                <int>   <int>          <int>   
1 1001    2017-10-20 18:42:00       1       1       1
2 1001    2017-10-20 20:24:00       2       2       1
3 1001    2017-10-20 23:12:00       3       3       1
4 1001    2017-10-21 01:23:00       4       1       2
5 1001    2017-10-21 13:32:00       5       2       2
6 1001    2017-10-21 15:17:00       6       3       2
7 1001    2017-10-21 17:32:00       7       4       2   
8 1001    2017-10-21 20:23:00       8       5       2
9 1001    2017-10-21 22:57:00       9       6       2       
10 1001    2017-10-22 01:54:00      10       1  3

谢谢!

1 个答案:

答案 0 :(得分:0)

使用tidyverse和lubridate包,这有效!

library(tidyverse)
library(lubridate)

##data = data name
## ParticipantId = unique subject ID
## expday = new variable created

data <- data %>% 
  group_by(ParticipantId) %>%
  mutate(
    DateTime = mdy_hm(DateTime),
    Date = lubridate::date(DateTime),
    expday = dense_rank(Date))
    ungroup() %>%
arrange(ParticipantId, DateTime, expday) # arrange data
data %>% select(ParticipantId, DateTime, expday) #view data