Ramakrishna S
Ramakrishna S

Reputation: 437

Change of column value on multiple conditions

From the following table, I need to put OUT = 0 for rows where OUT = ? and any of the two rows or both LIT, STB = 0. Also suggest code how to remove these rows and keep reduced dataset.

library(QCA)
#> Warning: package 'QCA' was built under R version 3.6.3
#> Loading required package: admisc
#> Warning: package 'admisc' was built under R version 3.6.3
#> 
#> To cite package QCA in publications, please use:
#>   Dusa, Adrian (2019) QCA with R. A Comprehensive Resource.
#>   Springer International Publishing.
#> 
#> To run the graphical user interface, use: runGUI()
ttLF = truthTable(LF,
           outcome = "SURV",
           incl.cut = .8,
           show.cases = TRUE,
           complete = TRUE,
           sort.by = "OUT")
ttLF$tt
#>    DEV URB LIT IND STB OUT n              incl                PRI       cases
#> 1    0   0   0   0   0   0 3 0.215976331360947                  0    GR,PT,ES
#> 2    0   0   0   0   1   0 2 0.278026905829596                  0       IT,RO
#> 3    0   0   0   1   0   ? 0                 -                  -            
#> 4    0   0   0   1   1   ? 0                 -                  -            
#> 5    0   0   1   0   0   0 2 0.520900321543409  0.113095238095238       HU,PL
#> 6    0   0   1   0   1   0 1 0.528571428571429  0.228070175438597          EE
#> 7    0   0   1   1   0   ? 0                 -                  -            
#> 8    0   0   1   1   1   ? 0                 -                  -            
#> 9    0   1   0   0   0   ? 0                 -                  -            
#> 10   0   1   0   0   1   ? 0                 -                  -            
#> 11   0   1   0   1   0   ? 0                 -                  -            
#> 12   0   1   0   1   1   ? 0                 -                  -            
#> 13   0   1   1   0   0   ? 0                 -                  -            
#> 14   0   1   1   0   1   ? 0                 -                  -            
#> 15   0   1   1   1   0   ? 0                 -                  -            
#> 16   0   1   1   1   1   ? 0                 -                  -            
#> 17   1   0   0   0   0   ? 0                 -                  -            
#> 18   1   0   0   0   1   ? 0                 -                  -            
#> 19   1   0   0   1   0   ? 0                 -                  -            
#> 20   1   0   0   1   1   ? 0                 -                  -            
#> 21   1   0   1   0   0   ? 0                 -                  -            
#> 22   1   0   1   0   1   1 2 0.804270462633452  0.719387755102041       FI,IE
#> 23   1   0   1   1   0   0 1 0.378205128205128 0.0396039603960395          AU
#> 24   1   0   1   1   1   0 2 0.708771929824562  0.634361233480176       FR,SE
#> 25   1   1   0   0   0   ? 0                 -                  -            
#> 26   1   1   0   0   1   ? 0                 -                  -            
#> 27   1   1   0   1   0   ? 0                 -                  -            
#> 28   1   1   0   1   1   ? 0                 -                  -            
#> 29   1   1   1   0   0   ? 0                 -                  -            
#> 30   1   1   1   0   1   ? 0                 -                  -            
#> 31   1   1   1   1   0   0 1 0.445255474452555 0.0499999999999999          DE
#> 32   1   1   1   1   1   1 4 0.904205607476635  0.885793871866295 BE,CZ,NL,UK

Created on 2020-12-04 by the reprex package (v0.3.0)

Upvotes: 0

Views: 50

Answers (2)

Agaz Wani
Agaz Wani

Reputation: 5684

I assume you are looking for this, if not, please show the expected output.

df <- ttLF$tt
df$OUT <- ifelse(df$OUT == '?', 0, 
             ifelse(df$LIT == 0 | df$STB == 0, 0, df$OUT))

Output

    DEV URB LIT IND STB OUT n              incl                PRI       cases
1    0   0   0   0   0   0 3 0.215976331360947                  0    GR,PT,ES
2    0   0   0   0   1   0 2 0.278026905829596                  0       IT,RO
3    0   0   0   1   0   0 0                 -                  -            
4    0   0   0   1   1   0 0                 -                  -            
5    0   0   1   0   0   0 2 0.520900321543409  0.113095238095238       HU,PL
6    0   0   1   0   1   0 1 0.528571428571429  0.228070175438597          EE
7    0   0   1   1   0   0 0                 -                  -            
8    0   0   1   1   1   0 0                 -                  -            
9    0   1   0   0   0   0 0                 -                  -            
10   0   1   0   0   1   0 0                 -                  -            
11   0   1   0   1   0   0 0                 -                  -            
12   0   1   0   1   1   0 0                 -                  -            
13   0   1   1   0   0   0 0                 -                  -            
14   0   1   1   0   1   0 0                 -                  -            
15   0   1   1   1   0   0 0                 -                  -            
16   0   1   1   1   1   0 0                 -                  -            
17   1   0   0   0   0   0 0                 -                  -            
18   1   0   0   0   1   0 0                 -                  -            
19   1   0   0   1   0   0 0                 -                  -            
20   1   0   0   1   1   0 0                 -                  -            
21   1   0   1   0   0   0 0                 -                  -            
22   1   0   1   0   1   1 2 0.804270462633452  0.719387755102041       FI,IE
23   1   0   1   1   0   0 1 0.378205128205128 0.0396039603960395          AU
24   1   0   1   1   1   0 2 0.708771929824562  0.634361233480176       FR,SE
25   1   1   0   0   0   0 0                 -                  -            
26   1   1   0   0   1   0 0                 -                  -            
27   1   1   0   1   0   0 0                 -                  -            
28   1   1   0   1   1   0 0                 -                  -            
29   1   1   1   0   0   0 0                 -                  -            
30   1   1   1   0   1   0 0                 -                  -            
31   1   1   1   1   0   0 1 0.445255474452555 0.0499999999999999          DE
32   1   1   1   1   1   1 4 0.904205607476635  0.885793871866295 BE,CZ,NL,UK

To remove the rows

df[(df$OUT != '?' & (df$LIT != 0 | df$STB != 0)) , ]

DEV URB LIT IND STB OUT n              incl                PRI       cases
2    0   0   0   0   1   0 2 0.278026905829596                  0       IT,RO
5    0   0   1   0   0   0 2 0.520900321543409  0.113095238095238       HU,PL
6    0   0   1   0   1   0 1 0.528571428571429  0.228070175438597          EE
22   1   0   1   0   1   1 2 0.804270462633452  0.719387755102041       FI,IE
23   1   0   1   1   0   0 1 0.378205128205128 0.0396039603960395          AU
24   1   0   1   1   1   0 2 0.708771929824562  0.634361233480176       FR,SE
31   1   1   1   1   0   0 1 0.445255474452555 0.0499999999999999          DE
32   1   1   1   1   1   1 4 0.904205607476635  0.885793871866295 BE,CZ,NL,UK

Upvotes: 2

dc37
dc37

Reputation: 16178

Not sure, I'm getting clearly what you are asking for but if you want to modify rows where OUT = ? AND any of LIT = 0 or STB = 0, you can use case_when from dplyr package:

library(dplyr)
df %>% 
  mutate(OUT = case_when(OUT == "?" & (LIT == 0 | STB == 0) ~ "0",
                         TRUE ~ OUT))
# A tibble: 20 x 3
   OUT     LIT   STB
   <chr> <int> <int>
 1 0         0     0
 2 1         1     0
 3 0         0     1
 4 0         1     0
 5 0         0     1
 6 1         1     1
 7 0         0     1
 8 0         0     1
 9 1         1     0
10 1         0     0
11 0         0     1
12 1         0     0
13 1         1     0
14 ?         1     1
15 1         1     0
16 0         1     0
17 0         0     1
18 0         0     1
19 ?         1     1
20 1         1     0

If you want to remove these rows, just use the same in filter:

df %>% 
  filter(!(OUT == "?" & (LIT == 0 | STB == 0)))

Is it what you are looking for ?


Reproducible example:

df <- tibble(OUT = sample(c("0","?","1"),20, replace = TRUE),
             LIT = sample(0:1,20, replace = TRUE),
             STB = sample(0:1,20, replace = TRUE))

# A tibble: 20 x 3
   OUT     LIT   STB
   <chr> <int> <int>
 1 0         0     0
 2 1         1     0
 3 0         0     1
 4 0         1     0
 5 ?         0     1
 6 1         1     1
 7 ?         0     1
 8 ?         0     1
 9 1         1     0
10 1         0     0
11 0         0     1
12 1         0     0
13 1         1     0
14 ?         1     1
15 1         1     0
16 ?         1     0
17 0         0     1
18 0         0     1
19 ?         1     1
20 1         1     0

Upvotes: 0

Related Questions