RL_Pug
RL_Pug

Reputation: 857

How to use lapply() or ifelse() across multiple columnns to make Yes == 1, and else == 0?

A       B      C
Yes    Yes     No
No     Yes     Yes 
NA     No      No
Yes   Yes      No

my desired output

A       B      C
1      1       0
0      1       1 
0      0       0
1      1       0

I'm currently doing

data$A <- ifelse(data$A  == "Yes", 1, 0)

But how can I do this for all my columns? Or what if I wanted to do this for columns B & C

I tried this

data[2:3] <- data.frame(lapply(data[2:3], function(x) {ifelse(colnames(.)   == "Yes", 1, 0)} as.numeric(as.character(x))))

but it didn't work.

Any advice? Im tired of data wrangling already lol.

Upvotes: 0

Views: 102

Answers (4)

Chris Ruehlemann
Chris Ruehlemann

Reputation: 21400

Any of these work--take your pick:

sapply(data, function(x) ifelse(x == "No" | is.na(x), 0, 1))
     A B C
[1,] 1 1 0
[2,] 0 1 1
[3,] 0 0 0
[4,] 1 1 0

apply(data, 2, function(x) ifelse(x == "No" | is.na(x), 0, 1))
     A B C
[1,] 1 1 0
[2,] 0 1 1
[3,] 0 0 0
[4,] 1 1 0

Upvotes: 0

akrun
akrun

Reputation: 887048

Another option is to coerce logical to binary

 +(data != 'No' & !is.na(data))
#    A B C
#[1,] 1 1 0
#[2,] 0 1 1
#[3,] 0 0 0
#[4,] 1 1 0

data

data <- data.frame(A=c("Yes", "No", NA, "Yes"),
                   B=c("Yes", "Yes", "No", "Yes"),
                   C=c("No", "Yes", "No", "No"), stringsAsFactors=FALSE)

Upvotes: 1

Daniel O
Daniel O

Reputation: 4358

You could use this single line of garbled code:

df<-as.data.frame(lapply(c("A","B","C"), function(X) ifelse(eval(parse(text=paste0("df$",X))) == "Yes", 1, 0)),col.names = c("A","B","C"))

Output:

> df
   A B C
1  1 1 0
2  0 1 1
3 NA 0 0
4  1 1 0

Data

df <-data.frame(A=c('Yes','No',NA,'Yes'),B=c("Yes","Yes","No","Yes"),C=c("No","Yes","No","No"))

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521073

You could try using ifelse against the entire data frame:

ifelse(data == "No" | is.na(data), 0, 1)

     A B C
[1,] 1 1 0
[2,] 0 1 1
[3,] 0 0 0
[4,] 1 1 0

Data:

data <- data.frame(A=c("Yes", "No", NA, "Yes"),
                   B=c("Yes", "Yes", "No", "Yes"),
                   C=c("No", "Yes", "No", "No"), stringsAsFactors=FALSE)

Note that this actually generates a matrix result, but given that all its values are zero and one, maybe you find this acceptable.

Upvotes: 2

Related Questions