Home > OS >  Condition in rows, modify all columns without a loop
Condition in rows, modify all columns without a loop

Time:10-19

what I want to do is to modify all selected columns of an R data table according to the rows conditions i.eenter image description here

for all 4 columns selected in cols variable, if the value is greater (or equal) than 1.5, i would like to put them to 1 else 0

I tried something like that : iris[(cols) > 1.5 , (cols) := 1, .SDcols = cols]

Thx

CodePudding user response:

One data.table approach:

iris <- as.data.table(iris)
cols <- names(iris)[1:4]
cols
# [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
iris[, (cols) := lapply(.SD, function(z) fifelse(z > 1.5, 1, z)), .SDcols = cols]
iris
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#             <num>       <num>        <num>       <num>    <fctr>
#   1:            1           1          1.4         0.2    setosa
#   2:            1           1          1.4         0.2    setosa
#   3:            1           1          1.3         0.2    setosa
#   4:            1           1          1.5         0.2    setosa
#   5:            1           1          1.4         0.2    setosa
#   6:            1           1          1.0         0.4    setosa
#   7:            1           1          1.4         0.3    setosa
#   8:            1           1          1.5         0.2    setosa
#   9:            1           1          1.4         0.2    setosa
#  10:            1           1          1.5         0.1    setosa
#  ---                                                            
# 141:            1           1          1.0         1.0 virginica
# 142:            1           1          1.0         1.0 virginica
# 143:            1           1          1.0         1.0 virginica
# 144:            1           1          1.0         1.0 virginica
# 145:            1           1          1.0         1.0 virginica
# 146:            1           1          1.0         1.0 virginica
# 147:            1           1          1.0         1.0 virginica
# 148:            1           1          1.0         1.0 virginica
# 149:            1           1          1.0         1.0 virginica
# 150:            1           1          1.0         1.0 virginica

An alternative using set:

for (nm in cols) set(iris, which(iris[[nm]] > 1.5), nm, 1)

CodePudding user response:

Another solution:

library(dplyr)
library(data.table)

iris[,1:4] %>% data.table() %>% mutate_all(~ ifelse(.x>=1.5,1,0))

CodePudding user response:

If you just need to check for numeric columns across can be a good fit, it also works with more specific choices like positions and names

library(tidyverse)

iris |>
  as_tibble() |> 
  mutate(across(.cols = where(is.numeric),.fns = ~ if_else(.x > 1.5,1,.x)))
#> # A tibble: 150 x 5
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>           <dbl>       <dbl>        <dbl>       <dbl> <fct>  
#>  1            1           1          1.4         0.2 setosa 
#>  2            1           1          1.4         0.2 setosa 
#>  3            1           1          1.3         0.2 setosa 
#>  4            1           1          1.5         0.2 setosa 
#>  5            1           1          1.4         0.2 setosa 
#>  6            1           1          1           0.4 setosa 
#>  7            1           1          1.4         0.3 setosa 
#>  8            1           1          1.5         0.2 setosa 
#>  9            1           1          1.4         0.2 setosa 
#> 10            1           1          1.5         0.1 setosa 
#> # ... with 140 more rows

Created on 2021-10-18 by the reprex package (v2.0.1)

CodePudding user response:

Base R option -

data <- iris
cols <- 1:4
data[cols] <-  (data[cols] > 1.5)

#    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#1              1           1            0           0     setosa
#2              1           1            0           0     setosa
#3              1           1            0           0     setosa
#4              1           1            0           0     setosa
#5              1           1            0           0     setosa
#6              1           1            1           0     setosa
#...
#...

The at the beginning is used to change the logical values (TRUE/FALSE) to integers (1/0).

CodePudding user response:

We may do

library(dplyr)
iris %>%
     mutate(across(where(is.numeric), ~  (. > 1.5)))
  • Related