Идентификация строк с одинаковой последовательностью при игнорировании пропущенных данных в R - PullRequest
0 голосов
/ 08 ноября 2018

Я работаю с данными панели, где одна и та же переменная записывается несколько раз, чтобы создать последовательность состояний.Я только хочу использовать наблюдения, которые не имеют однородных последовательностей, но я изо всех сил пытаюсь создать флаг, который бы идентифицировал их, но также не рассматривал 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").

Я попробовал несколько подходов:

Я попытался подсчитать последовательные состояния, но это не учитывает отдельные идентификаторы

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

IЯ также пытался вручную создать переменную flag, но она не учитывает NA и, учитывая количество повторных наблюдений в моем наборе данных, она слишком ручная / отнимает много времени

df$employed[df$S1=="Education" & df$S2=="Education" & df$S3=="Education" & df$S4=="Education" & df$S5=="Education"] <- 1
df$employed

Любая помощь будет принята с благодарностью.

Ответы [ 3 ]

0 голосов
/ 08 ноября 2018

У меня было похожее решение, но без table:

df[df == "NA"] <- NA
df$to.keep <- apply(df[, -1], 1, function(x) {
  !any(is.na(x)) & length(unique(x)) > 1
})

> which(df$to.keep)
[1] 4 6 7
0 голосов
/ 08 ноября 2018
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")
S6 <- c("Education", "Employment", "Education", "Unemployed", "Education", "Employment", "Employment", "EMP", "Education", "Education")
df <- data.frame(ID, S1, S2, S3, S4, S5,S6)

Добавлен S6 также из ваших комментариев, когда Андре не может правильно его пометить

library(dplyr)
df[df == "NA"] <- NA

df$Flag_NA = ifelse(apply(df %>% select(-ID),1,function(x) any(is.na(x))),'No','Yes')
df$Flag_Uniform = ifelse(apply(df %>% select(-ID,-Flag_NA), 1, function(x)length(unique(x))) == 1,'No','Yes')
df = df %>% mutate(Flag_keep = ifelse(Flag_NA == Flag_Uniform,"Yes","No"))

df
   ID         S1         S2         S3         S4         S5         S6 Flag_NA Flag_Uniform Flag_keep
1   1  Education  Education  Education  Education  Education  Education     Yes           No        No
2   2 Employment Employment Employment Employment Employment Employment     Yes           No        No
3   3  Education  Education       <NA>  Education  Education  Education      No          Yes        No
4   4  Education Unemployed Unemployed Unemployed Unemployed Unemployed     Yes          Yes       Yes
5   5  Education  Education  Education  Education  Education  Education     Yes           No        No
6   6  Education  Education Employment Employment Employment Employment     Yes          Yes       Yes
7   7  Education Employment Employment Employment Employment Employment     Yes          Yes       Yes
8   8  Education  Education       <NA>       <NA>       <NA>        EMP      No          Yes        No
9   9  Education  Education  Education  Education  Education  Education     Yes           No        No
10 10  Education  Education  Education  Education  Education  Education     Yes           No        No
0 голосов
/ 08 ноября 2018

Это супер просто:

df[df == "NA"] <- NA

df$keep <- lengths(apply(df[,-1],1, table)) > 1

#> which(df$keep)
#[1] 4 6 7
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...