Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition in rows, modify all columns without a loop

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

like image 864
nimliug Avatar asked Dec 31 '25 16:12

nimliug


2 Answers

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)
like image 170
r2evans Avatar answered Jan 02 '26 10:01

r2evans


Another solution:

library(dplyr)
library(data.table)

iris[,1:4] %>% data.table() %>% mutate_all(~ ifelse(.x>=1.5,1,0))
like image 34
PaulS Avatar answered Jan 02 '26 08:01

PaulS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!