R conditional variable for longitudinal data

user3641630

I have data for each month for a year on insured people. All variables are dummy variables and I need to create a new variable that shows when a person became uninsured. I am calling the variable duration. My dataset (df) looks something like this:

ID Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec

101 1 1 1 1 0 0 1 1 1 1 1 1

102 1 1 1 1 0 0 0 0 0 0 0 0

103 1 1 1 1 1 1 1 1 1 1 1 1

104 1 1 1 1 0 1 1 0 1 1 1 1

In the dataset, 1 is insured and 0 is uninsured.My new variable would have the have the col position for when the person changed from 1 to 0. For instance in the first row, my variable duration would have the value 5 for may. I am only insterested in the first instance of 0. For example, in row 4, i only need 5 for may and can ignore august. Also, if the person does not become uninsured like in the case of 103, the new variable would just have the value "0".

I began by using ifelse statement below but it would take me a lot of time to keep repeating it. if you have an easier solution for this, please share. Thanks!

df$duration=ifelse(df$feb==1,0,2)

Roland

There are more efficient alternatives, but maybe this is sufficient:

apply(DF[,-1], 1, function(x) which(x==0)[1])
#[1]  5  5 NA  5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Operation conditional on time index for longitudinal data in r

From Dev

R: how to rank longitudinal data

From Dev

How to plot longitudinal data in R

From Dev

cumulative variable construction in longitudinal data set

From Dev

cumulative variable construction in longitudinal data set

From Dev

Creating a variable that incorporates for lagged values in longitudinal panel data

From Dev

R: Insert missing dates in longitudinal data without losing information

From Dev

Create longitudinal data from a list of igraph objects in R

From Dev

Rearranging longitudinal data

From Dev

Rearranging longitudinal data

From Dev

Summarising longitudinal data with dplyr

From Dev

Create new variable in R data frame by conditional lookup

From Dev

Plot and model longitudinal data with covariates

From Dev

R: how to visualize longitudinal trajectory

From Dev

Visualizing longitudinal data with a trajectory/best-fitting mean growth curve and a spaghetti plot using R

From Dev

Addition of a new column to longitudinal data in R, based on matching, time, response and grouping information.

From Dev

Conditional variable using R code

From Dev

Drop variable in panel data in R conditional based on a defined number of consecutive observations

From Dev

Drop variable in panel data in R conditional based on a defined number of consecutive observations

From Dev

Conditional converting data in dataframe in r

From Dev

Conditional Formatting of grouped Data in R

From Dev

Cubic spline method for longitudinal series data?

From Dev

Handling of longitudinal (repeated measurements) data in dplyr?

From Dev

How to create row/subject index in longitudinal data

From Dev

SAS adding new observations for longitudinal data

From Dev

Remove discontinuous time points in longitudinal data

From Dev

spaghetti plots for binned longitudinal data with timepoints

From Dev

Handling of longitudinal (repeated measurements) data in dplyr?

From Dev

Store Query String Data Into Variable For Use In Conditional

Related Related

  1. 1

    Operation conditional on time index for longitudinal data in r

  2. 2

    R: how to rank longitudinal data

  3. 3

    How to plot longitudinal data in R

  4. 4

    cumulative variable construction in longitudinal data set

  5. 5

    cumulative variable construction in longitudinal data set

  6. 6

    Creating a variable that incorporates for lagged values in longitudinal panel data

  7. 7

    R: Insert missing dates in longitudinal data without losing information

  8. 8

    Create longitudinal data from a list of igraph objects in R

  9. 9

    Rearranging longitudinal data

  10. 10

    Rearranging longitudinal data

  11. 11

    Summarising longitudinal data with dplyr

  12. 12

    Create new variable in R data frame by conditional lookup

  13. 13

    Plot and model longitudinal data with covariates

  14. 14

    R: how to visualize longitudinal trajectory

  15. 15

    Visualizing longitudinal data with a trajectory/best-fitting mean growth curve and a spaghetti plot using R

  16. 16

    Addition of a new column to longitudinal data in R, based on matching, time, response and grouping information.

  17. 17

    Conditional variable using R code

  18. 18

    Drop variable in panel data in R conditional based on a defined number of consecutive observations

  19. 19

    Drop variable in panel data in R conditional based on a defined number of consecutive observations

  20. 20

    Conditional converting data in dataframe in r

  21. 21

    Conditional Formatting of grouped Data in R

  22. 22

    Cubic spline method for longitudinal series data?

  23. 23

    Handling of longitudinal (repeated measurements) data in dplyr?

  24. 24

    How to create row/subject index in longitudinal data

  25. 25

    SAS adding new observations for longitudinal data

  26. 26

    Remove discontinuous time points in longitudinal data

  27. 27

    spaghetti plots for binned longitudinal data with timepoints

  28. 28

    Handling of longitudinal (repeated measurements) data in dplyr?

  29. 29

    Store Query String Data Into Variable For Use In Conditional

HotTag

Archive