I have a data frame given below;
NPA_Jan Risk_Jan NAP_Feb Risk_Feb
Yes High No Medium
No Low No Low
No High Yes High
Yes High Yes High
Yes Low No Low
I am expecting an output below:
NPA_Jan_New Risk_Jan_New NPA_Feb_New Risk_Feb_New
1 1 0 0
0 0 0 0
0 1 1 1
1 1 1 1
1 0 0 0
Wherever there is NPA is 'Yes' and Risk as 'High' there should tag 1.
The simplest way is as David said,
df.new <- as.data.frame((df == "Yes" | df == "High") + 0)
colnames(df.new) <- paste(colnames(df.new), '_new', sep='')
An option is using
out <- +(sapply(df, `%in%`, c("Yes", "High")))
colnames(out) <- paste0(colnames(df), "_new")
out
# NPA_Jan_new Risk_Jan_new NAP_Feb_new Risk_Feb_new
#[1,] 1 1 0 0
#[2,] 0 0 0 0
#[3,] 0 1 1 1
#[4,] 1 1 1 1
#[5,] 1 0 0 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.