0

I have an dataframe that I've read from a CSV that I'm trying to clean up. This is what it looks like:

  A B     C
1 0 X;Y;Z true
2 2 Y;Z   false
3 5 X:Y   false

What I'm trying to break up is the B column into a binary input like this:

  A B     C     has.x has.y has.z
1 0 X;Y;Z true  1     1     1
2 2 Y     false 0     1     0
3 5 X:Y   false 1     1     0

I tried using an ifelse with an assignment but it applies the value to the whole column. How do I break it down to apply to reach row individually?

raw <- read.csv("data.csv")
raw$has.x <- ifelse("x" %in% raw[,"B"], 1, 0)
RedOster
  • 325
  • 3
  • 16

1 Answers1

0
x <- read.table(text = 'A B     C
1 0 X;Y;Z true
2 2 Y;Z   false
3 5 X;Y   false', stringsAsFactors  = F) 


## use dplyr::seperate

x1 <- x %>% separate(., B, sep = ';', 
                into = c( 'has.x' ,'has.y' ,'has.z'),   remove = FALSE) 

## put the values in the desired format 

x1[,c( 'has.x' ,'has.y' ,'has.z')] <-  sapply(x1[,c( 'has.x' ,'has.y' ,'has.z')], 
       function (x) as.numeric(!is.na(x)))


x1
  A     B has.x has.y has.z     C
1 0 X;Y;Z     1     1     1  true
2 2   Y;Z     1     1     0 false
3 5   X;Y     1     1     0 false
Mouad_Seridi
  • 2,666
  • 15
  • 27