How transform (calculate) an ordinal variable to a dichotomous in R?

FKG

I want to transform an ordinal variabel (0-2) – where 0 is no rights, 1 is some rights, and 2 full rights – to a dichotomous variable.

The original ordinal variable is coded for each country and year (country-year unit).

I want to create a dichotomous variable, (let's call it Improvement), capturing all annual positive changes, for each country-year. So when it goes from 0 to 1 (or from 0 to 2, or from 1 to 0), I want it to be 1 for that year and country. And zero otherwise.

Below I give an example of how my data looks like. The "RIGHTS" is the original ordinal variable. The "MY DICHOTOMOUS" variable is what I want to calculate in R. How can I do it?

COUNTRY YEAR RIGHTS  MY DICHOTOMOUS
A        1990  0           0
A        1991  0           0
A        1992  0           0
A        1993  1           1 
A        1994  0           0
B        1990  1           1 
B        1991  1           0 
B        1992  1           0
B        1993  1           0
B        1994  1           0

Please, note that the original data can go the other away as well, i.e. it can go negative. I do not want to code for negative changes for this dichotomous variable.

akrun

We can use diff

 df1$dichotomous <- +c(FALSE,diff(df1$RIGHTS)==1)
 df1$dichotomous
 #[1] 0 0 0 1 0 1 0 0 0 0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Ordinal variable to become dichotomous

How to recode ordinal variable?

How to calculate ordinal number on columns?

How to transform ordinal values into categorical ones?

How transform grouping variable to columns in R?

How to Phi^-1 transform a variable in R?

r packages for fused lasso with ordinal response variable?

How to calculate average of a variable by hour in R

How to calculate disease prevalence by a variable in R

How to calculate KNN Variable Importance in R

How to create a variable that indicates agreement from two dichotomous variables

How to transform a variable and a number into a variable?

How does R handle ordinal predictors in lm()?

transform date into dummy variable in R

How to calculate a variable using a lagged value using R

How can i calculate sum of character variable in r

How to calculate difference in time between variable rows in R?

How to calculate the variance of specific variable across multiple datasets in R

How to filter a data set and calculate a new variable faster in R?

How to calculate a percentage of one variable based on the frequency of another in R

How to transform date in r?

How to transform a dataset in R?

How to correctly calculate Fisher Transform indicator

Calculate variable based on criteria in r

Calculate variable stored as character in R

Coverting a numeric variable into a ordinal variable

Transform variable length list into matrix in R

How to plot interactions of predictors when the response data is ordinal in r?

How to count ordinal values through multiple columns in R