【发布时间】:2018-11-08 19:59:47
【问题描述】:
我正在处理面板数据,其中多次记录相同的变量以创建一系列状态。我只想使用没有统一序列的观察,但我正在努力创建一个标志来识别这些,同时也不将 NA 视为不同的状态。
我创建了一个示例数据集以简化操作:
ID <- c(1,2,3,4,5,6,7,8,9,10)
S1 <- c("Education", "Employment", "Education", "Education", "Education", "Education", "Education", "Education", "Education", "Education")
S2 <- c("Education", "Employment", "Education", "Unemployed", "Education", "Education", "Employment", "Education", "Education", "Education")
S3 <- c("Education", "Employment", "NA", "Unemployed", "Education", "Employment", "Employment", "NA", "Education", "Education")
S4 <- c("Education", "Employment", "Education", "Unemployed", "Education", "Employment", "Employment", "NA", "Education", "Education")
S5 <- c("Education", "Employment", "Education", "Unemployed", "Education", "Employment", "Employment", "NA", "Education", "Education")
df <- data.frame(ID, S1, S2, S3, S4, S5)
df
ID S1 S2 S3 S4 S5
1 1 Education Education Education Education Education
2 2 Employment Employment Employment Employment Employment
3 3 Education Education NA Education Education
4 4 Education Unemployed Unemployed Unemployed Unemployed
5 5 Education Education Education Education Education
6 6 Education Education Employment Employment Employment
7 7 Education Employment Employment Employment Employment
8 8 Education Education NA NA NA
9 9 Education Education Education Education Education
10 10 Education Education Education Education Education
理想情况下,我能够标记或仅保留观察 ID=c("4", "6", "7")。
我尝试了几种方法:
我尝试计算连续状态,但这并没有考虑单独的 ID
library(data.table)
setDT(df_long)
df_long[, employed := (S=="Employment")
][, e.length := with(rle(employed), rep(lengths,lengths))
][employed == 0, e.length := 0]
df_long[, education := (S=="Education")
][, edu.length := with(rle(education), rep(lengths,lengths))
][education == 0, edu.length := 0]
df_long
我也尝试过手动创建一个标志变量,但这并没有考虑到 NA,而且我的数据集中重复观察的数量太手动/耗时
df$employed[df$S1=="Education" & df$S2=="Education" & df$S3=="Education" & df$S4=="Education" & df$S5=="Education"] <- 1
df$employed
任何帮助将不胜感激。
【问题讨论】:
-
还可以按如下方式矢量化
which(rowSums((df[, 2] == df[, -(1:2)]) + (df[, -(1:2)] == "NA")) < 4)(但前提是您在指定, stringsAsFactors = FALSE时创建数据)