在R中更改测量数据
我有一套观测健康调查数据。我想修改源标识符,这样我仍然可以识别多个源,但原始ID不是跟踪ID以保持机密性。我搞不清楚 下面是数据框的基本布局在R中更改测量数据,r,R,我有一套观测健康调查数据。我想修改源标识符,这样我仍然可以识别多个源,但原始ID不是跟踪ID以保持机密性。我搞不清楚 下面是数据框的基本布局 ID 30 30 30 30 24 24 24 我想创建一个新的ID,以便数据如下所示 NewID ID 1 30 1 30 1 30 1 30 2 24 2 24 2 24 cbindmatchID,uniqueID,IDcbindmatchID,
ID
30
30
30
30
24
24
24
我想创建一个新的ID,以便数据如下所示
NewID ID
1 30
1 30
1 30
1 30
2 24
2 24
2 24
cbindmatchID,uniqueID,IDcbindmatchID,uniqueID,ID如果您的数据帧是df,那么应该这样做
df$NewID <- as.numeric(factor(df$ID))
如果您的数据帧是df,那么这应该可以做到
df$NewID <- as.numeric(factor(df$ID))
这在这里很有用:
> ID <- rep(c(30,24), c(4,3)) # your data
> ind <- rle(ID)$lengths
> data.frame(ID, newID=rep(c(1,length(ind)), ind ))
ID newID
1 30 1
2 30 1
3 30 1
4 30 1
5 24 2
6 24 2
7 24 2
这在这里很有用:
> ID <- rep(c(30,24), c(4,3)) # your data
> ind <- rle(ID)$lengths
> data.frame(ID, newID=rep(c(1,length(ind)), ind ))
ID newID
1 30 1
2 30 1
3 30 1
4 30 1
5 24 2
6 24 2
7 24 2