R:不平衡的面板,为独特的观察创建虚拟

时间:2018-06-14 15:37:55

标签: r panel-data

我有一个不平衡的面板数据(意味着在所有时间段都没有观察到某些人)。我想创建一个虚拟变量,如果在两个或更多个周期中观察到个体,则取值为1,否则为0。 有人能够做到这一点,并能解释给我吗? 对不起,如果问题看起来有点琐碎"。

我试过这个,但是它创造了多个假人,我只需要一个。

for(level in unique(df$id)){
share[paste("dummy", level, sep = "_")] <- ifelse(df$id == level, 1, 0)
}

一个小例子可能是:

set.seed(123)
df <- data.frame(id = sample(1:10, 20, replace = TRUE),
                 happy = sample(c("yes", "no"), 20, replace = TRUE))

预期产量:

id   happy   dummy
 3     no      1
 8     no      0
 5     no      1
 9     no      1
10     no      1
 1     no      1
 6     no      1
 9     no      1
 6    yes      1
 5    yes      1
10     no      1
 5     no      1
 7     no      0
 6     no      1
 2    yes      0
 9    yes      1
 3     no      1
 1    yes      1
 4    yes      0
10    yes      1

1 个答案:

答案 0 :(得分:0)

使用return mysqldb.addUser("{}".format(session['client_fname']),"{}".format(session['client_lname'])),您可以避免循环并尝试此操作:

dplyr

基本上,此方法将set.seed(123) df <- data.frame(id = sample(1:10, 20, replace = TRUE), happy = sample(c("yes", "no"), 20, replace = TRUE)) library(dplyr) df <- df %>% group_by(id) %>% mutate(dummy = ifelse(length(id)>=2, 1, 0)) > df # A tibble: 20 x 3 # Groups: id [10] id happy dummy <int> <fct> <dbl> 1 3 no 1 2 8 no 0 3 5 no 1 4 9 no 1 5 10 no 1 6 1 no 1 7 6 no 1 8 9 no 1 9 6 yes 1 10 5 yes 1 11 10 no 1 12 5 no 1 13 7 no 0 14 6 no 1 15 2 yes 0 16 9 yes 1 17 3 no 1 18 1 yes 1 19 4 yes 0 20 10 yes 1 除以df的唯一值,然后创建一个列id,如果该ID的出现次数超过两次,则值为{1}如果没有。