Home > Enterprise >  How do i get true value
How do i get true value

Time:07-31

code:
x=table(sample_data$store_name=="35",sample_data$STATUS=="TERMINATED")
x

result:
        FALSE  TRUE
  FALSE 47232  1278
  TRUE    936   207

How can I only get the value 207? I have tried other way but still can't find any solution

CodePudding user response:

Use sum instead of table and combine the two conditions with &.

x = sum(sample_data$store_name=="35" & sample_data$STATUS=="TERMINATED")

Here's an example with built-in mtcars dataset to demonstrate -

table(mtcars$cyl == 4, mtcars$mpg > 30)
       
#        FALSE TRUE
#  FALSE    21    0
#  TRUE      7    4

sum(mtcars$cyl == 4 &  mtcars$mpg > 30)
#[1] 4

CodePudding user response:

Just use

x=table(sample_data$store_name=="35",
        sample_data$STATUS=="TERMINATED")[2,2]

Here treating table object as matrix .

Or for consistency we can use

x=table(sample_data$store_name=="35",
        sample_data$STATUS=="TERMINATED")["TRUE","TRUE"]
  •  Tags:  
  • r
  • Related