[英]R - manipulating time series data
I have a time-series dataset with yearly values for 30 years for >200,000 study units that all start off as the same value of 'healthy==1' and can transition to 3 classes - 'exposed==2', 'infected==3' and 'recover==4';我有一个时间序列数据集,其中包含超过 200,000 个研究单位的 30 年年度值,所有这些研究单位都以“健康==1”的相同值开始,并且可以过渡到 3 个类别 - 'exposed==2','infected= =3' 和 '恢复==4'; some units also remain as 'healthy' throughout the time series.
一些单位在整个时间序列中也保持“健康”。 The dataset is in long format.
数据集为长格式。
I would like to manipulate the dataset that keeps all 30 years for each unit but collapsed to only 'heathy==1' and 'infected==3' ie I would classify 'exposed==2' as 'healthy==1' and the first time a 'healthy' unit gets 'infected==3', it remains as infected for the remaining of the time-series even though it might 'recover==4'/change state again (gets infected and recover again).我想操纵每个单元保留所有 30 年的数据集,但崩溃为仅“heathy==1”和“infected==3”,即我会将“exposed==2”分类为“healthy==1”和第一次“健康”单元被“感染==3”,它在剩余的时间序列中仍然被感染,即使它可能再次“恢复==4”/改变 state(被感染并再次恢复)。 Healthy units that never transition to another class will remain classified as healthy throughout the time series.
永远不会过渡到另一个 class 的健康单元将在整个时间序列中保持分类为健康。
I am kinda stumped on how to code this out in r;我对如何在 r 中编写代码感到困惑; any ideas would be greatly appreciated
任何想法将不胜感激
example of dataset for two units;两个单位的数据集示例; one remains health throughout the time series and another has multiple transitions.
一个在整个时间序列中保持健康,另一个有多个过渡。
UID annual_change_val year
1 control1 1 1990
4 control1 1 1991
5 control1 1 1992
7 control1 1 1993
9 control1 1 1994
12 control1 1 1995
13 control1 1 1996
16 control1 1 1997
18 control1 1 1998
20 control1 1 1999
22 control1 1 2000
24 control1 1 2001
26 control1 1 2002
28 control1 1 2003
30 control1 1 2004
31 control1 1 2005
33 control1 1 2006
35 control1 1 2007
38 control1 1 2008
40 control1 1 2009
42 control1 1 2010
44 control1 1 2011
46 control1 1 2012
48 control1 1 2013
50 control1 1 2014
52 control1 1 2015
53 control1 1 2016
55 control1 1 2017
57 control1 1 2018
59 control1 1 2019
61 control1 1 2020
2 control64167 1 1990
3 control64167 1 1991
6 control64167 1 1992
8 control64167 2 1993
10 control64167 2 1994
11 control64167 2 1995
14 control64167 2 1996
15 control64167 2 1997
17 control64167 3 1998
19 control64167 3 1999
21 control64167 4 2000
23 control64167 4 2001
25 control64167 4 2002
27 control64167 4 2003
29 control64167 3 2004
32 control64167 4 2005
34 control64167 4 2006
36 control64167 4 2007
37 control64167 4 2008
39 control64167 4 2009
41 control64167 4 2010
43 control64167 4 2011
45 control64167 4 2012
47 control64167 4 2013
49 control64167 4 2014
51 control64167 4 2015
54 control64167 4 2016
56 control64167 4 2017
58 control64167 4 2018
60 control64167 4 2019
62 control64167 4 2020
If for some reason you only want to use base R,如果出于某种原因你只想使用 base R,
df$annual_change_val[df$annual_change_val == 2] <- 1
df$annual_change_val[df$annual_change_val == 4] <- 3
The first line means: take the annual_change_val
column from ( $
) dataframe df
, subset it ( [
) so that you're only left with values equal to 2, and re-assign ( <-
) to those a value of 1 instead.第一行的意思是:从 (
$
) dataframe df
中取出annual_change_val
列,对其进行子集化 ( [
),这样您只剩下等于 2 的值,然后将 ( <-
) 重新分配给那些值为 1 的值。 Similarly for the second line.第二行也是如此。
Here, I replace the values as before, and then I create a temp variable called max_inf
which holds the maximum year that the UID was "infected" (status=3).在这里,我像以前一样替换值,然后我创建一个名为
max_inf
的临时变量,它保存 UID 被“感染”的最大年份 (status=3)。 I then replace the status to 3 for any year that is beyond that year (within UID).然后,我将超过该年(在 UID 内)的任何年份的状态替换为 3。
d %>%
mutate(status = if_else(annual_change_val %in% c(1,2),1,3)) %>%
group_by(UID) %>%
mutate(max_inf = max(year[which(status==3)],na.rm=T),
status = if_else(!is.na(max_inf) & year>max_inf & status==1,3,status)) %>%
select(!max_inf)
You can simply change the values from 2 to 1, and from 4 to 3, as Andrea mentioned in the comments.正如 Andrea 在评论中提到的,您可以简单地将值从 2 更改为 1,从 4 更改为 3。 If
d
is your data, then如果
d
是你的数据,那么
library(dplyr)
d %>% mutate(status = if_else(annual_change_val %in% c(1,2),1,3))
library(data.table)
setDT(d)[, status:=fifelse(annual_change_val %in% c(1,2),1,3)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.