Reputation: 23
Hi I am trying to create a function for EXPSS table, sample data below
dput( df<-data.frame(
aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,3,4,4,4,1,2,5,3,5),
col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,7,2,7,7,2,3,7,2,4)
)
)
function i created is
sum1 <- cro_cpct(df1[[1]],df2[[2]])
}
now i want to add a criteria in this function on total, if the total falls in (3,4,5) then the whole column will replace by "--".
Upvotes: 0
Views: 74
Reputation: 4846
Something like this:
library(expss)
dataa<-data.frame(
aa = c("q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c","q","r","y","v","g","y","d","s","n","k","y","d","s","t","n","u","l","h","x","c"),
col1=c(1,2,3,2,1,2,3,4,4,4,5,3,4,2,1,2,5,3,2,1,2,4,2,1,3,2,1,2,3,1,2,3,4,4,4,1,2,5,3,5),
col2=c(2,1,1,7,4,1,2,7,5,7,2,6,2,2,6,3,4,3,2,5,7,5,6,4,4,6,5,6,4,1,7,7,2,7,7,2,3,7,2,4)
)
tab1 <- cro_cpct(dataa$aa,dataa$col1)
total_row = grep("#", tab1[[1]])
tab1[total_row, -1] = ifelse(tab1[total_row, -1]<8, "--", tab1[total_row, -1])
tab1
# | | | dataa$col1 | | | | |
# | | | 1 | 2 | 3 | 4 | 5 |
# | -------- | ------------ | ---------- | ---- | ---- | ---- | -- |
# | dataa$aa | c | 12.5 | | | | 25 |
# | | d | 12.5 | | 37.5 | | |
# | | g | 12.5 | | 12.5 | | |
# | | h | | | 12.5 | | 25 |
# | | k | 12.5 | | | 12.5 | |
# | | l | | 8.3 | | | 25 |
# | | n | 12.5 | | 12.5 | 25.0 | |
# | | q | 12.5 | 8.3 | | | |
# | | r | | 8.3 | | 12.5 | |
# | | s | | 8.3 | | 37.5 | |
# | | t | | 8.3 | | 12.5 | |
# | | u | 12.5 | 8.3 | | | |
# | | v | 12.5 | 8.3 | | | |
# | | x | | 8.3 | 12.5 | | |
# | | y | | 33.3 | 12.5 | | 25 |
# | | #Total cases | 8.0 | 12.0 | 8.0 | 8.0 | -- |
Upvotes: 1