Home > Net >  How do I generate a new column using a conditional which evaluates values against a vector?
How do I generate a new column using a conditional which evaluates values against a vector?

Time:03-05

Let's say I have this column.

dataframe$column<-c(1, 2, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 8, 9, NA, NA, 0)

and I got this vector

vector<- c(1, 5, 9)

I need to create a new column in my data frame that would return 0 when the value in any given row is equal to any value of my vector. If it is not equal I would like to return a 0, and if the column originally had a NA, I would like to return a NA as well.

So in my example the new colum should look like this:

[1] 1 0 0 0 0 0 0 1 1 1 0 0 0 1 NA NA 0

I'm pretty sure I could do this with apply or sapply, but I'm not very good with loops.

Thanks in advance.

CodePudding user response:

You can do:

library(tidyverse)

dataframe <- data.frame(column = c(1, 2, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 8, 9, NA, NA, 0))

vector<- c(1, 5, 9)

dataframe %>%
  mutate(new_column = case_when(is.na(column) ~ NA_real_,
                                column %in% vector ~ 1,
                                TRUE ~ 0))

   column new_column
1       1          1
2       2          0
3       2          0
4       2          0
5       3          0
6       4          0
7       4          0
8       5          1
9       5          1
10      5          1
11      6          0
12      7          0
13      8          0
14      9          1
15     NA         NA
16     NA         NA
17      0          0

CodePudding user response:

Try %in%

> replace( (x %in% v), is.na(x), NA)
 [1]  1  0  0  0  0  0  0  1  1  1  0  0  0  1 NA NA  0

or outer rowSums

>  (rowSums(outer(x, v, `==`)) > 0)
 [1]  1  0  0  0  0  0  0  1  1  1  0  0  0  1 NA NA  0

Data

x <- c(1, 2, 2, 2, 3, 4, 4, 5, 5, 5, 6, 7, 8, 9, NA, NA, 0)
v <- c(1, 5, 9)
  • Related