Home > OS >  Identify column with positive values and mixed values
Identify column with positive values and mixed values

Time:08-25

In the data.table below, there are 5 columns. Two columns have only positive values and three columns have a mix of positive and negative values.

temp_dt = structure(list(A = c(-1.4151, -1.2535, -0.7097, -0.4184, 0.0579, 
0.2964, 0.7352, 0.8097, 0.7089, 0.4088), B = c(56.2776, 48.3075, 
56.9811, 53.5512, 58.1714, 54.9303, 59.553, 53.5128, 50.3509, 
46.1777), C = c(78.3689, 47.99, 88.1506, 71.2523, 94.6338, 81.2598, 
90.4598, 67.8621, 54.8966, 35.9972), D = c(-11.3246, -9.9593, 
-5.6762, -3.3858, 0.1303, 1.8899, 4.9471, 5.4475, 4.7887, 2.8341
), E = c(-0.0974, -0.1852, -0.0652, -0.0917, -0.0695, -0.0548, 
-0.0286, -0.0419, -0.0796, -0.0875)), row.names = c(NA, -10L), class = c("data.table", 
"data.frame"))

> temp_dt 
         A       B       C        D       E
 1: -1.4151 56.2776 78.3689 -11.3246 -0.0974
 2: -1.2535 48.3075 47.9900  -9.9593 -0.1852
 3: -0.7097 56.9811 88.1506  -5.6762 -0.0652
 4: -0.4184 53.5512 71.2523  -3.3858 -0.0917
 5:  0.0579 58.1714 94.6338   0.1303 -0.0695
 6:  0.2964 54.9303 81.2598   1.8899 -0.0548
 7:  0.7352 59.5530 90.4598   4.9471 -0.0286
 8:  0.8097 53.5128 67.8621   5.4475 -0.0419
 9:  0.7089 50.3509 54.8966   4.7887 -0.0796
10:  0.4088 46.1777 35.9972   2.8341 -0.0875

I need a base R or data.table solution to identify columns with only positive values and also the columns with mixed values.

CodePudding user response:

In base R you could do.

For mixed valued columns:

!sapply(temp_dt, \(x) all(x > 0 ) | all( x < 0))

    A     B     C     D     E 
 TRUE FALSE FALSE  TRUE FALSE

For positive only columns:

sapply(temp_dt, \(x) all(x > 0 ))

   A     B     C     D     E 
FALSE  TRUE  TRUE FALSE FALSE

We're just applying a function over each column

CodePudding user response:

Also you could do:

only for positive

apply(sign(temp_dt) == 1, 2, all)
    A     B     C     D     E 
FALSE  TRUE  TRUE FALSE FALSE 

colSums(sign(temp_dt)) == nrow(temp_dt)
    A     B     C     D     E 
FALSE  TRUE  TRUE FALSE FALSE 

for mixed values:

abs(colSums(sign(temp_dt)) < nrow(temp_dt)
  A     B     C     D     E 
 TRUE FALSE FALSE  TRUE FALSE 

only for negative values:

-colSums(sign(temp_dt)) == nrow(temp_dt)
 A     B     C     D     E 
FALSE FALSE FALSE FALSE  TRUE 
     
apply(sign(temp_dt) == -1, 2, all)
    A     B     C     D     E 
FALSE FALSE FALSE FALSE  TRUE 
  • Related