PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 7, 2022

[FIXED] How to Create Conditional Dummy Variables (Panel Data) in R?

 October 07, 2022     dummy-variable, r, statistics     No comments   

Issue

I have Panel Data consisting of two waves: 18 and 21. I have a status of employment having 4 values.

I want to create a dummy taking value 1 if the person is employed in both waves and zero otherwise. However, I fail the code produces a dummy having only zero values:

df$dummy <- df %>%
  group_by(NEW_id) %>%
  arrange(New_id, WAVE_NO) %>%
  mutate(dummy = case_when(WAVE_NO==18 & WAVE_NO==21 & EMPLOYMENT_STATUS=="Employed" ~ 1, TRUE ~ 0))

enter image description here


Solution

df <- data.frame(
  stringsAsFactors = FALSE,
  id = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L),
  wave = c(18L, 21L, 18L, 21L, 18L, 21L, 18L, 10L, 18L, 21L),
  EMPLOYMENT_STATUS = c(
    "Employed",
    "Employed",
    "unemployed",
    "Employed",
    "unemployed",
    "Employed",
    "Employed",
    "Employed",
    "unemployed",
    "unemployed"
  )
)

library(tidyverse)
df %>%
  group_by(id) %>%
  mutate(dummy = +(all(wave %in% c(18, 21)) &
                     all(EMPLOYMENT_STATUS == "Employed"))) %>%
  ungroup()
#> # A tibble: 10 x 4
#>       id  wave EMPLOYMENT_STATUS dummy
#>    <int> <int> <chr>             <int>
#>  1     1    18 Employed              1
#>  2     1    21 Employed              1
#>  3     2    18 unemployed            0
#>  4     2    21 Employed              0
#>  5     3    18 unemployed            0
#>  6     3    21 Employed              0
#>  7     4    18 Employed              0
#>  8     4    10 Employed              0
#>  9     5    18 unemployed            0
#> 10     5    21 unemployed            0

Created on 2022-01-23 by the reprex package (v2.0.1)



Answered By - Yuriy Saraykin
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing