Problem with series of conditionals

Hi everyone.

I have this df, and I want to make four new columns:

cuadri conforme AQ n
1 FALSE 2a HTR 1
1 FALSE 3a HMI 1
1 FALSE PB HTR 1
1 FALSE Pediatria 1
1 FALSE PV 4
1 TRUE 2a HTR 8
1 TRUE 3a HMI 9
[...]
2p FALSE 2a HTR 5
2p FALSE 3a HMI 7
2p FALSE 4a HG 7
2p FALSE 8a HG 4
2p FALSE Arritmias 1
2p FALSE PB HTR 7
2p FALSE Pediatria 4
  • c1t: If 'cuadri' is 1 AND 'conforme' is true, this cell is the value in 'n'
  • c1f: If 'cuadri' is 1 AND 'conforme' is false, this cell is the value in 'n'
  • c2t: If 'cuadri' is 2 AND 'conforme' is true, this cell is the value in 'n'
  • c2f: If 'cuadri' is 2 AND 'conforme' is false, this cell is the value in 'n'

For example, with AQ=2a HTR,
image
would have c1f=1, c1t=8, c2f=5 and c2t=8

I tried with mutate and ifelse, but I haven't got it:
AQsum2<-AQsum %>%
mutate(c1t=ifelse(cuadri==1 & conforme==TRUE,AQsum$n,"")) %>%
mutate(c1f=ifelse(cuadri==1 & conforme==FALSE,AQsum$n,"")) %>%
mutate(c2t=ifelse(cuadri=="2p" & conforme==TRUE,AQsum$n,"")) %>%
mutate(c2f=ifelse(cuadri=="2p" & conforme==FALSE,AQsum$n,""))
How I can do it?
Thanks

In R tables all values in a column must be numeric or strings (not mixed).
You don't mention what error you encountered but adapting your solution in the following way, works for me:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)

AQsum <- data.frame(
  cuadri = c("1","1", "2p","2p"),
  conforme = c(F,T,F,T),
  AQ = rep("2a HTR",4),
  n = c(1,8,5,8)
)

AQsum %>%
  mutate(
    c1t = ifelse(cuadri == "1"  & conforme == T, n, -999) ,
    c1f = ifelse(cuadri == "1"  & conforme == F, n, -999) ,
    c2t = ifelse(cuadri == "2p"  & conforme == T, n, -999) ,
    c2f = ifelse(cuadri == "2p"  & conforme == F, n, -999)
  ) %>%
  print()
#>   cuadri conforme     AQ n  c1t  c1f  c2t  c2f
#> 1      1    FALSE 2a HTR 1 -999    1 -999 -999
#> 2      1     TRUE 2a HTR 8    8 -999 -999 -999
#> 3     2p    FALSE 2a HTR 5 -999 -999 -999    5
#> 4     2p     TRUE 2a HTR 8 -999 -999    8 -999
Created on 2022-10-29 with reprex v2.0.2
1 Like

This solves one of my problems, thanks!
But it is posible to arrange all the results in the same row?
Like this:

AQ c1t c1f c2t c2f
2a HTR 8 1 8 5
3a HMI 9 1 10 7
4a HG 75 0 96 7

I made the table with Excel, still have not been able to do it with R

And extra bonus: Add a p-value for the comparison between c1prop and c2prop

AQ c1t c1f c2t c2f c1tot c2tot c1prop c2prop
2a HTR 8 1 8 5 9 13 88.89 61.54
3a HMI 9 1 10 7 10 17 90 58.82
4a HG 75 0 96 7 75 103 100 93.2

This pivots your data.frame.
No bonus for me.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(magrittr)

AQsum <- data.frame(
  cuadri = c("1","1", "2p","2p","1"),
  conforme = c(F,T,F,T,T),
  AQ = c(rep("2a HTR",4),"4a HG"),
  n = c(1,8,5,8,3)
) %>%
  print()
#>   cuadri conforme     AQ n
#> 1      1    FALSE 2a HTR 1
#> 2      1     TRUE 2a HTR 8
#> 3     2p    FALSE 2a HTR 5
#> 4     2p     TRUE 2a HTR 8
#> 5      1     TRUE  4a HG 3

AQsum %>%
  mutate(cuadriT = ifelse(cuadri == "1" ,T, F) ) %>%
  group_by(AQ,cuadriT,conforme) %>%  # to sum over AQ,cuadriT,conforme
  mutate(n=sum(n)) %>%               # when this is not done
  ungroup() %>%                      # before
  tidyr::pivot_wider(id_cols=AQ,
                     names_from = c(cuadriT,conforme),
                     values_from = n,
                     values_fill =0) %>%
  select(AQ,c1t=TRUE_TRUE,c1f=TRUE_FALSE,c2t=FALSE_TRUE,c2f=FALSE_FALSE)
#> # A tibble: 2 × 5
#>   AQ       c1t   c1f   c2t   c2f
#>   <chr>  <dbl> <dbl> <dbl> <dbl>
#> 1 2a HTR     8     1     8     5
#> 2 4a HG      3     0     0     0
Created on 2022-10-29 with reprex v2.0.2
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.