Add a new column if multiple columns have negative value

Kryo

I am trying to add a new column "result" in my dataframe df1, if the condition is

  1. select only specific columns (from count1:count3 columns) have negative value, then result = "negative"
  2. if any one of the columns (count1, count2, count3) have a positive value the result = "positive"

input

df1<- data.frame(ID= c("ID1","ID2","ID3","ID4"), count1 = c(1,-1,0,-1), count2 = c(1,-1,-1,1), count3 = c(1,-1,1,-1))

expected output

df2 <- data.frame(ID= c("ID1","ID2","ID3","ID4"),count1 = c(1,-1,1,-1), count2 = c(1,-1,-1,1), count3 = c(1,-1,1,-1), result = c("positive","negative","positive","positive"))

  ID count1 count2 count3   result
 ID1      1      1      1 positive
 ID2     -1     -1     -1 negative
 ID3      1     -1      1 positive
 ID4     -1      1     -1 positive
Karthik S

Does this work:

apply(df1,1,function(x) if(all(x > 0)) 'positive' else 'negative')
[1] "positive" "negative" "negative" "negative"

library(dplyr)
df1 %>% rowwise() %>% mutate(result = if_else(all(c_across(cols = everything()) > 0),'positive','negative'))
# A tibble: 4 x 4
# Rowwise: 
  count1 count2 count3 result  
   <dbl>  <dbl>  <dbl> <chr>   
1      1      1      1 positive
2     -1     -1     -1 negative
3      0     -1      1 negative
4     -1      1     -1 negative

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add a new column that specify multiple columns name that have value

Find nearest value from multiple columns and add to a new column in Python

Compare values in multiple columns and add a new value in another column in Python

Python: Set new column value if multiple columns in a dataframe have any value other than one specific string

I have a data frame with multiple columns. How do I group the columns by year, and add a new column that ranks the values of one of the columns?

Compair 2 columns and add a value in a new column

How to add multiple new columns with a fixed value?

Check content of multiple columns of one row and add new column with value depending on contents

SAS Add new multiple columns based on other column's value using loop

Add new columns and insert values in columns based on value in another column

Pandas dataframe add new column based on if other columns have data or not

Referencing multiple columns and rows to calculate new value in a new column

Pivot multiple columns with a column value repeats for each new column

how to divide pos and negative values from one column to value from another column and add the result in new column

Return list of columns in Dataframe that have a specific value as new column

R: Add new column based on condition with multiple columns

Add a new column to a dataframe based on multiple columns from another dataframe

add a new column based on conditions on multiple columns in R

Python pandas: add new columns based on the existed a column value, and set the value of new columns as 1 or 0

How to add new column, where the value is based on existing columns with awk

PySpark - Add a new nested column or change the value of existing nested columns

Add new column with times same value was found in 2 columns

Add new columns to DataFrame from the value of one column

To add new columns that identify the value position by each column with loop

pandas: add new column with value from either of two other columns

Add a new row with a certain value into the first column and then "-" to the rest of the columns

Python add new column with repeating value based on two other columns

Add new columns to data frame for each unique value in another column

how to check the value in some columns and add the header to the new column in pandas