简体   繁体   English

如何创建一个新变量,其值基于两个时间点之间的差异?

[英]How do I create a new variable with values that base on the difference between two time points?

I have two variables that represent the wake-up time and go to sleep time in minutes (example: If Person A went to bed at 00:40 it would be "40" for sleep_min).我有两个变量代表唤醒时间和 go 以分钟为单位的睡眠时间(例如:如果人 A 在 00:40 上床睡觉,那么 sleep_min 将是“40”)。

  id      day wake_min sleep_min         
1 ADD15     1      518        40
2 ADD15     2      540        45
3 ADD15     3      570        80
4 ADD15     4      487        50
5 ADD15     5      582        73
6 AHK1      1      405      1435
7 AHK1      2      611      1402   

Then I have a second data set that has a row for each day and minute for every person.然后我有第二个数据集,每个人的每一天和一分钟都有一行。 So Person ADD15 has 1440 rows for day 1, 1440 for day 2 etc.所以 Person ADD15 第 1 天有 1440 行,第 2 天有 1440 行,等等。

      id day minute
1: ADD15   1      1
2: ADD15   1      2
3: ADD15   1      3
4: ADD15   1      4
5: ADD15   1      5
6: ADD15   1      6
...

I want to create a new variable named "state" for the second data set.我想为第二个数据集创建一个名为“state”的新变量。 This one should represent if a person is whether sleeping or awake.这个应该代表一个人是睡着还是醒着。 The variable should be coded 1 for sleeping and 0 for being awake.该变量应编码为 1 表示睡眠,0 表示清醒。 In the case of day 1 of person ADD15, all rows for the minutes between 40 and 518 should have the value "1" and the other rows for day 1 "0".对于人 ADD15 的第 1 天,40 到 518 分钟之间的所有行的值应为“1”,第 1 天的其他行为“0”。

I tried some things with teh ifelse function but nothing worked so far.我用 teh ifelse function 尝试了一些东西,但到目前为止没有任何效果。

I would really appreciate some help and advices for my problem!对于我的问题,我真的很感激一些帮助和建议!

Thanks in advance!!提前致谢!!

You could merge the datasets, then check if minute is in the sleep seq uence.您可以merge数据集,然后检查分钟是否在睡眠seq中。

dat3 <- merge(dat1, dat2)
dat3$asleep <-
  +mapply(`%in%`,
          dat3$minute,
          apply(dat3[3:4], 1, function(x) do.call(seq, as.list(unname(x))))
  )
head(dat3, 20)
#       id day wake_min sleep_min minute asleep
# 1  ADD15   1      518        40    264      1
# 2  ADD15   1      518        40    265      1
# 3  ADD15   1      518        40    262      1
# 4  ADD15   1      518        40    263      1
# 5  ADD15   1      518        40    272      1
# 6  ADD15   1      518        40    273      1
# 7  ADD15   1      518        40    274      1
# 8  ADD15   1      518        40    275      1
# 9  ADD15   1      518        40    276      1
# 10 ADD15   1      518        40    277      1
# 11 ADD15   1      518        40    278      1
# 12 ADD15   1      518        40    266      1
# 13 ADD15   1      518        40    267      1
# 14 ADD15   1      518        40    268      1
# 15 ADD15   1      518        40    269      1
# 16 ADD15   1      518        40    270      1
# 17 ADD15   1      518        40    271      1
# 18 ADD15   1      518        40      1      0
# 19 ADD15   1      518        40      2      0
# 20 ADD15   1      518        40      3      0

Data:数据:

dat1 <- structure(list(id = c("ADD15", "ADD15", "ADD15", "ADD15", "ADD15", 
"AHK1", "AHK1"), day = c(1L, 2L, 3L, 4L, 5L, 1L, 2L), wake_min = c(518L, 
540L, 570L, 487L, 582L, 405L, 611L), sleep_min = c(40L, 45L, 
80L, 50L, 73L, 1435L, 1402L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))

dat2 <- expand.grid(id=unique(dat1$id), day=1:3, minute=1:1440)

Thank your very much for your quick answer!非常感谢您的快速答复!

This worked so far, but theres still one problem.到目前为止,这有效,但仍然存在一个问题。 If the person went to sleep after midnight, the function works perfectly.如果该人在午夜后入睡,则 function 可以完美运行。 But if the person went to bed before midnight, it doesn't.但如果这个人在午夜之前上床睡觉,它就不会。

    id day wake_min sleep_min minute sleep
1  AHK1   1      405      1435    395 FALSE
2  AHK1   1      405      1435    396 FALSE
3  AHK1   1      405      1435    397 FALSE
4  AHK1   1      405      1435    398 FALSE
5  AHK1   1      405      1435    399 FALSE
6  AHK1   1      405      1435    400 FALSE
7  AHK1   1      405      1435    401 FALSE
8  AHK1   1      405      1435    402 FALSE
9  AHK1   1      405      1435    403 FALSE
10 AHK1   1      405      1435    404 FALSE
11 AHK1   1      405      1435    405  TRUE
12 AHK1   1      405      1435    406  TRUE

This person went to sleep at minute 1435 the day before.这个人在前一天的 1435 分钟睡觉。 So for day 1 minute 0 to 405 should be TRUE.因此,对于第 1 分钟,0 到 405 应该是 TRUE。 I don't know how to handle this.我不知道如何处理这个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中两个时间点之间创建随机日期向量? - How do I create a vector of random dates between two time points in R? 如何从一个变量中创建两个新变量,并在 R 中为其附加虚拟值? - How do I create two new variables out of one variable, and attach dummy values to it in R? 如何根据 R 中的两个分类值创建新变量? - How do I create a new variable based on two categorical values in R? 两个值之间的时间差 - Time difference between two values 试图获取 R 中两个时间点之间的时间差。 我在数据集中有 NA,但我找不到正确的方法 - Trying to get the time difference between two time points in R. I have NAs in the data set and i cant find a way to do it correctly 如何创建一个新的 dataframe 包含比较其他两个数据帧的差异值? - How can I create a new dataframe that contains the difference values from comparing two other dataframes? 如何找到序列矩阵中两点之间的距离? - How do I find the distance between two points in a matrix of sequences? 如何在多个行和列的不同数据框中找到两个值之间的差异? - How do I find the difference between two values in different dataframes across multiple rows and columns? 如何使用自行车站数据对两个数据点之间的时间差进行子集和查找 - How to subset and find time difference between two data points using bike station data 循环测量两个值之间的时间差 - Loop to measure difference in time between two values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM