Check if the value exists in any other columns with Tidyverse

Tom Hoel

Sample data

df <- diag(1:8) %>% 
  as_tibble() %>%  
  mutate(A = c(10, 5, 11, 4, 6, 9, 65, 8))

# A tibble: 8 x 9
     V1    V2    V3    V4    V5    V6    V7    V8     A
  <int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1     1     0     0     0     0     0     0     0    10
2     0     2     0     0     0     0     0     0     5
3     0     0     3     0     0     0     0     0    11
4     0     0     0     4     0     0     0     0     4
5     0     0     0     0     5     0     0     0     6
6     0     0     0     0     0     6     0     0     9
7     0     0     0     0     0     0     7     0    65
8     0     0     0     0     0     0     0     8     8

The desired output would be

# A tibble: 8 x 10
     V1    V2    V3    V4    V5    V6    V7    V8     A result
  <int> <int> <int> <int> <int> <int> <int> <int> <dbl> <lgl> 
1     1     0     0     0     0     0     0     0    10 FALSE 
2     0     2     0     0     0     0     0     0     5 TRUE  
3     0     0     3     0     0     0     0     0    11 FALSE 
4     0     0     0     4     0     0     0     0     4 TRUE  
5     0     0     0     0     5     0     0     0     6 TRUE  
6     0     0     0     0     0     6     0     0     9 FALSE 
7     0     0     0     0     0     0     7     0    65 FALSE 
8     0     0     0     0     0     0     0     8     8 TRUE

AND/OR ... A takes the value that is non-zero value in any other columns, such that the output would be:

# A tibble: 8 x 9
     V1    V2    V3    V4    V5    V6    V7    V8     A
  <int> <int> <int> <int> <int> <int> <int> <int> <int>
1     1     0     0     0     0     0     0     0     1
2     0     2     0     0     0     0     0     0     2
3     0     0     3     0     0     0     0     0     3
4     0     0     0     4     0     0     0     0     4
5     0     0     0     0     5     0     0     0     5
6     0     0     0     0     0     6     0     0     6
7     0     0     0     0     0     0     7     0     7
8     0     0     0     0     0     0     0     8     8

How can this be achieved with tidyverse?

Park

You may try

library(dplyr)
df %>%
  mutate(result = rowSums(across(everything() & - "A", ~A %in% .x)) > 0)

     V1    V2    V3    V4    V5    V6    V7    V8     A result
  <int> <int> <int> <int> <int> <int> <int> <int> <dbl> <lgl> 
1     1     0     0     0     0     0     0     0    10 FALSE 
2     0     2     0     0     0     0     0     0     5 TRUE  
3     0     0     3     0     0     0     0     0    11 FALSE 
4     0     0     0     4     0     0     0     0     4 TRUE  
5     0     0     0     0     5     0     0     0     6 TRUE  
6     0     0     0     0     0     6     0     0     9 FALSE 
7     0     0     0     0     0     0     7     0    65 FALSE 
8     0     0     0     0     0     0     0     8     8 TRUE  

If starts with

     V1    V2    V3    V4    V5    V6    V7    V8     A
  <int> <int> <int> <int> <int> <int> <int> <int> <dbl>
1     1     0     0     0     0     0     0     0    10
2     0     2     0     0     0     0     0     0     5
3     0     0     3     0     0     0     0     0    11
4     0     0     0     4     0     0     0     0     4
5     0     0     0     0     5     0     0     0     6
6     0     0     0     0     0     6     0     0     9
7     0     0     0     0     0     0     7     0    65
8     0     0     0     0     0     0     0     8     8

then

df %>%
  mutate(A = rowSums(.) - A)

If starts with

     V1    V2    V3    V4    V5    V6    V7    V8
  <int> <int> <int> <int> <int> <int> <int> <int>
1     1     0     0     0     0     0     0     0
2     0     2     0     0     0     0     0     0
3     0     0     3     0     0     0     0     0
4     0     0     0     4     0     0     0     0
5     0     0     0     0     5     0     0     0
6     0     0     0     0     0     6     0     0
7     0     0     0     0     0     0     7     0
8     0     0     0     0     0     0     0     8

then

df %>%
  mutate(A = rowSums(.))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if a value exists in any of the columns in a table in sql

Check if certain value exists is any 4 out of 10 columns

For each row check if value in one column exists in two other columns

Check if any value ( multiple columns) of one data frame exists in any values (multiple columns) of another data frame

How to loop through columns, check if a particular value exists in any of the columns, mutate a new column and enter 1 if it exists, 0 if not?

Check if any value in a list exists in a group of dataframe columns and create new boolean column

Checking if value exists in any of two columns with pandas

React calculator: check if any value exists in string

LINQ check if exists, but without equal to any value

Query to check if a value exists in a set of columns

How to check if a value exists within a set of columns?

SQL: How to check a value in column is equal to the sum of any combination of the other columns in a row

Check if flag contains any value of other flag

Check if any solution exists

R: Create a dummy variable if a particular value exists in any of the previous columns

Laravel - Is there any blade directive to check if a variable exists and is set to a specific value?

Pandas: Check if value in one df exists in any column of another DF

Pandas: Check if value in one df exists in any column of another DF

How to check if a value of array exists in other group of arrays

How to check if value exists in one of the columns and in which one in MySQL?

Pandas DataFrame check if column value exists in a group of columns

Pandas - check if a value exists in multiple columns for each row

Pandas dataframe check if a value exists in multiple columns for one row

R: How to check if a value exists within a set of columns

Check if any other select has same value set

MySQL how to check if any value in a list of columns is NULL?

Check if any combination of columns are equal some specific value in matrix

Check if value exists in enum

Check if value exists in file

TOP Ranking

HotTag

Archive