Hi,
I have this simple dummy df:
source <- data.frame(stringsAsFactors=FALSE,
þÿ.URN. = c("aaa", "bbb", "ccc", "ddd"),
Rec_2_43 = c(1, NA, 3, NA),
Staff_43 = c(NA, 1, NA, 1),
Rec_1_35 = c(2, NA, 1, NA),
Timeliness_2 = c(NA, 1, NA, 1),
Staff_2 = c(1, NA, 2, NA)
)
source
Now:
- I would like to create a code which creates new variables with names based on whatever is after the last "_".
- In each of these new variables a coding should create 1s if source variables contain values >=1
So in this small file variables "43", "35" and "2" should be created. Nevertheless, I would like to use a general code for all other variables (ending with _x) which may appear in my df.
Then:
if any value in variables ending with _43 is >=1, Variable 43 should be 1
if any value in variables ending with _35 is >=1, Variable 35 should be 1
if any value in variables ending with _2 is >=1, Variable 2 should be 1
if any value in variables ending with _x is >=1, Variable x should be 1
as a result I should get something like that:
# A tibble: 4 x 9
`þÿ"URN"` Rec_2_43 Staff_43 Rec_1_35 Timeliness_2 Staff_2 `43` `35` `2`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 aaa 1 NA 2 NA 1 1 1 1
2 bbb NA 1 NA 1 NA 1 NA 1
3 ccc 3 NA 1 NA 2 1 1 1
4 ddd NA 1 NA 1 NA 1 NA 1
>
Is it easy to do?