r - 在r中将标识符变量从一个数据集中拉到另一个数据集中
问题描述
我希望有人可以帮助我解决以下问题:我正在使用 r 中的两个数据表:一个包含用户四年内的体重测量值,另一个包含这些相同用户在同一时间段内的身体活动测量值。我一直在寻找体重测量数据中的长时间中断,这些是我感兴趣的事件。我现在想在体重数据中提取事件时间范围内的身体活动数据。一些用户可能在四年内发生了几次称重中断事件,因此仅 userID 不足以提取数据。在重量数据集中,我创建了一个名为 events_stop 的变量,它是特定称重中断事件的标识符。身体活动数据表中尚不存在此变量。我'
到目前为止,我的代码如下所示:我首先根据体重数据创建了一个数据表,其中包含 user_ID 和 events_stop,以及我稍后想要引入的 -20、-4 和 +6 周时间点的日期身体活动数据表“steps.all3”
minus20andminus4 <- weight.all6[, lapply(.SD, min, na.rm=T),
by=.(user_ID, events_stop), .SDcols = c("minus20", "minus4", "plus6")]
为了能够将 minus20andminus4 中的日期数据与身体活动数据表相匹配,我需要添加 events_stop 变量。为此,我想匹配 user_ID 相同且日期在停止事件期间的行
steps.all3[, events_stop := ifelse(steps.all3$user_ID == minus20andminus4$user_ID
& steps.all3$date_local >= minus20andminus4$minus20
& steps.all3$date_local <= minus20andminus4$plus6,
minus20andminus4$events_stop, NA)]
此时我收到错误消息:
Error in Ops.factor(steps.all3$user_ID, minus20andminus4$user_ID) :
level sets of factors are different
In addition: Warning message:
In is.na(e1) | is.na(e2) :
longer object length is not a multiple of shorter object length
重量数据如下所示:
user_ID date_local weight_kg events_stop minus20 minus4 plus 6
134 2016-01-07 99.2 160 2016-01-07 2016-04-28 2016-07-07
134 2016-02-08 99.6 160 2016-01-07 2016-04-28 2016-07-07
134 2016-02-10 99.5 160 2016-01-07 2016-04-28 2016-07-07
134 2016-03-13 99.1 160 2016-01-07 2016-04-28 2016-07-07
222 2014-04-20 78.2 181 2014-04-20 2014-08-03 2014-11-20
222 2014-05-02 78.3 181 2014-04-20 2014-08-03 2014-11-20
222 2014-05-07 78.9 181 2014-04-20 2014-08-03 2014-11-20
222 2016-08-15 82.1 195 2016-08-13 2016-12-03 2017-02-11
222 2016-08-22 82.6 195 2016-08-13 2016-12-03 2017-02-11
所以每个重量测量有一行。
步骤数据的相同结构:
user_ID date_local steps
134 2016-01-09 10231
134 2016-02-10 8972
222 2014-04-28 10332
222 2014-05-01 7782
222 2016-09-04 8432
谁能帮我吗?提前非常感谢!
解决方案
是否有可能其中一个date_local
s fromsteps.all3
可能在超过一个期间内?如果是这种情况,则可能值得进行不等式连接。以下是否捕获了您正在寻找的内容?
minus20andminus4[steps.all3,
.(i.user_ID, i.date_local, i.steps, x.events_stop, x.minus20, x.minus4, x.plus6),
on = .(user_ID = user_ID, minus20 <= date_local, plus6 >= date_local),
allow.cartesian = TRUE]
i.user_ID i.date_local i.steps x.events_stop x.minus20 x.minus4 x.plus6
1: 134 2016-01-09 10231 160 2016-01-07 2016-04-28 2016-07-07
2: 134 2016-02-10 8972 160 2016-01-07 2016-04-28 2016-07-07
3: 222 2014-04-28 10332 181 2014-04-20 2014-08-03 2014-11-20
4: 222 2014-05-01 7782 181 2014-04-20 2014-08-03 2014-11-20
5: 222 2016-09-04 8432 195 2016-08-13 2016-12-03 2017-02-11
推荐阅读
- ajax - CFML 如何从 ajax 响应中获取状态码
- reactjs - React 路由器私有路由正在访问它不应该访问的路由
- java - 使用 Java 从无效的 SOAP 1.1 消息中获取值
- wordpress-theming - Why does my child theme break my Divi website's header menu?
- python - 获取每个集群中的节点成员资格
- tfs - Getting file version from TFS rest API Get Item
- google-bigquery - BigQuery data load strategy suggestions
- node.js - 如何在 Facebook 的 Graph API 中使用 App Token
- sql-server - Concatenate string values in one row from multiple rows with constraints SSIS / SQL Server
- c# - Swagger Api 授权密钥?