提問生成新的觀測
我想在我的數據集的變量()系統地更改爲「混合」其電流值之間的中介的行。通過混合現有的觀察{R}
數據
我目前有5年遞增的州縣和人口數據。數據在數據框中。
data:
year state county population
1 1990 Vermont Chittenden 4050
2 1995 Vermont Chittenden 4180
3 2000 Vermont Chittenden 4205
4 2005 Vermont Chittenden 4350
5 2010 Vermont Chittenden 4358
6 2015 Vermont Chittenden 4401
使用的技術在這裏找到:Change variable value based on row index {R},我申請了以下內容:
new.data <- data[rep(1:nrow(data),each=5),]
new.data$year <- new.data$year + sequence(rep(5,nrow(data))) -1
其結果是,我得到這個:
new.data:
year state county population
1 1990 Vermont Chittenden 4050
1.1 1991 Vermont Chittenden 4050
1.2 1992 Vermont Chittenden 4050
1.3 1993 Vermont Chittenden 4050
1.4 1994 Vermont Chittenden 4050
2 1995 Vermont Chittenden 4180
2.1 1996 Vermont Chittenden 4180
2.2 1997 Vermont Chittenden 4180
2.3 1998 Vermont Chittenden 4180
2.4 1999 Vermont Chittenden 4180
3 2000 Vermont Chittenden 4205
...
5 2010 Vermont Chittenden 4358
5.1 2010 Vermont Chittenden 4358
5.2 2011 Vermont Chittenden 4358
5.3 2012 Vermont Chittenden 4358
5.4 2013 Vermont Chittenden 4358
6 2015 Vermont Chittenden 4401
但是,請注意沒有變化一次五年。我想找出一種方法來在增量值之間「混合」中間值。它看起來是這樣的:
new.data:
year state county population
1 1990 Vermont Chittenden 4050
1.1 1991 Vermont Chittenden 4076
1.2 1992 Vermont Chittenden 4102
1.3 1993 Vermont Chittenden 4128
1.4 1994 Vermont Chittenden 4154
2 1995 Vermont Chittenden 4180
2.1 1996 Vermont Chittenden 4185
2.2 1997 Vermont Chittenden 4190
2.3 1998 Vermont Chittenden 4195
2.4 1999 Vermont Chittenden 4200
3 2000 Vermont Chittenden 4205
...
5 2010 Vermont Chittenden 4358
5.1 2011 Vermont Chittenden 4367
5.2 2012 Vermont Chittenden 4376
5.3 2013 Vermont Chittenden 4385
5.4 2014 Vermont Chittenden 4394
6 2015 Vermont Chittenden 4401
我該如何做到這一點?
如果需要,我很樂意發佈更多信息。謝謝!
看看'seq'函數。 –
Hi @TimothéePoisot,你想介紹一下嗎?我對R還是比較陌生的。我明白'seq'允許規定'from'和'to';然而,我不知道如何每五年重複一次。謝謝! – Yasha