I am trying to find out if any of my columns have a values overpassing certain value, e.g. 7. If yes, that classify it as more
, otherwise be less
. My problem is similar to question here but I can't figure out how to update it to my case. I need the values to be classified row-wise. Also, some of the rows can be filled in just by the NA/NaN values. I need to keep them in a dataframe, so I can't filter them before the calculation.
Example:
# Example: classify data if they fit the condition:
data <- data.frame(V1=c(1,5,7, NA),
V2=c(1,5,8, NA),
V3=c(3,9,10, NaN))
# Classify each column:
ifelse(data$V1 >=7, "more", "less")
# Classify based on all columns? NOT WORKING!
data$NewCol <- ( Reduce(`|`, lapply(data, >=, 7)))
Desired outcome:
V1 V2 V3 newCol
1 1 1 3 less # no value is >= 7
2 5 5 9 more
3 7 8 10 more
4 NA NA NaN NA
CodePudding user response:
You may use rowSums
-
data$newCol <- ifelse(rowSums(data >= 7) > 0, 'more', 'less')
Or using dplyr
with case_when
if we have multiple conditions to check.
library(dplyr)
data %>%
rowwise() %>%
mutate(newCol = case_when(all(is.na(c_across())) ~ NA_character_,
any(c_across() >= 7) ~ 'more',
TRUE ~ 'less'))
# V1 V2 V3 newCol
# <dbl> <dbl> <dbl> <chr>
#1 1 1 3 less
#2 5 5 9 more
#3 7 8 10 more
#4 NA NA NaN NA