I am really new to R.
I have a table named RWA2010LONG
containing 65 variables. I want to create a new variable named NEWVAR
from the 30:49th variable of the table RWA2010LONG
and from another variable of the same table named (BIRTH
) based on the following condition: for each row, if one of the values of the variables 30:49 of the table RWA2010LONG
is equal to the value of the variable BIRTH
, NEWVAR
takes the value 1
. Otherwise, NEWVAR
takes the value 0
This is what I tried to do:
RWA2010LONG$ NEWVAR <- for (i in colnames(RWA2010LONG[30:49])){ifelse(i==RWA2010LONG$BIRTH, 1,0)}
CodePudding user response:
Here is an approach. Since you didn't provide data, I am making up some sample data and checking if any values in columns 5 through 10 are the same as BIRTH
:
#Example data
df <- data.frame(matrix(rnbinom(100, mu = 5, size = 0.5), ncol = 10,
dimnames = list(c(sprintf("obs_%s", 1:10)),
c("BIRTH",sprintf("col_%s", 2:10)))))
df$newvar <- apply(df[,5:10] == df$BIRTH, 1, any)*1
The apply
statement checks for the condition df[,5:10] == df$BIRTH
by row (the 1
indicates apply the condition by row, if for future reference, if you put a 2
it will check the condition by column) and returns TRUE
or FALSE
if the condition is met. The *1
converts those logicals to a numerical value.
Output:
# BIRTH col_2 col_3 col_4 col_5 col_6 col_7 col_8 col_9 col_10 newvar
# obs_1 0 3 4 0 6 18 0 10 5 7 1
# obs_2 5 1 0 7 5 0 2 2 2 3 1
# obs_3 1 2 4 2 1 13 14 1 2 8 1
# obs_4 1 0 0 0 11 0 0 0 15 0 0
# obs_5 1 9 1 0 4 27 2 1 0 0 1
# obs_6 5 1 0 2 0 7 2 4 0 0 0
# obs_7 1 0 0 6 0 0 1 5 0 0 1
# obs_8 44 0 15 1 1 0 1 1 0 6 0
# obs_9 6 6 0 0 0 4 17 0 6 1 1
# obs_10 0 2 0 0 2 11 2 1 9 2 0
CodePudding user response:
An option with if_any
library(dplyr)
df <- df %>%
mutate(newvar = (if_any(5:10, ~ .x == BIRTH)))