arranging rows in summary table

i have a table below , N should always be in second row and rest as it is . is the there any solution for this ...????

row_labels c d g h k l n q
1 50 25 50 NA 50 NA 25 50
2 NA 25 NA NA NA 50 NA 50
3 NA 50 50 50 NA NA 25 NA
4 NA NA NA NA 50 NA 50 NA
5 50 NA NA 50 NA 50 NA NA
N 18 4 2 2 2 2 4 2

.......??? you asking for that question...??
i deleted that question because i realized that there was a typo in my code and now it was working perfectly

to use a function like dplyr arrange on the row_labels column, you will need to stipulate an ordering where its not natural. This would be done by defining a factor

example_df <- function(intext) {
tf <- tempfile()
writeLines(intext, con = tf)
require(tidyverse)
as_tibble(read.delim(tf))
}
(df <- example_df("
row_labels	c	d	g	h	k	l	n	q
1	50	25	50	NA	50	NA	25	50
2	NA	25	NA	NA	NA	50	NA	50
3	NA	50	50	50	NA	NA	25	NA
4	NA	NA	NA	NA	50	NA	50	NA
5	50	NA	NA	50	NA	50	NA	NA
N	18	4	2	2	2	2	4	2"))

df$row_labels <- factor(df$row_labels,
                        levels = c(1,"N",2,3,4,5))

dplyr::arrange(df,
               row_labels)

i this scenario my labels are 1,2,3,4,5 but originally they are labelled already for eg:(CA,NY,WA,LA,TX.....). rows can be varies but i wan N in second row always.

because all row values are converting to NA's, any other amendments....??

so the whole function works like this

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,2,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,3,2,7,7,2,3,7,2,4)
)


dat1 = dat
var1 = "Q5.7"
grouping_var = "V8"

#dat1 = dataa
#var1 = "col1"
#grouping_var = "aa"



#tab_std_cross <- function(dat1,var1,grouping_var){
  
  var1 <- rlang::parse_expr(var1)
  var2 <- rlang::parse_expr(grouping_var)
  
  dat1 <- dat1 %>% select(!!var1,!!var2)
  var_lab(dat1[[1]]) <- ""
  var_lab(dat1[[2]]) <- ""
  tab1 <- expss::cro_cpct(dat1[[1]],dat1[[2]])
  tab1 <- as.data.frame(tab1)
  all <- sum(tab1[nrow(tab1), -1])
  overall <- as.data.frame(table(dat1[[1]])) %>% adorn_totals()
  overall <- overall %>% mutate(Freq_pct = (Freq / last(Freq))*100) %>% select(1,3)
  overall[[2]] <- format(round(overall[[2]],digits = 1),nsmall = 1)
  #overall[nrow(overall),2] <- colSums(as.data.frame(dat1[[1]]))
  tab1 <- left_join(tab1,overall,by=c("row_labels"="Var1")) %>% select(c("row_labels","Total"=Freq_pct,everything()))
  tab1 <- as.data.frame(tab1)
  tab1[which(tab1[,1]=="#Total cases"),1] <- "N"
  tab1[is.na(tab1)] <- 0
  
  tab1 <- tab1 %>%
    mutate(
      across(
        .cols = where(is.numeric),
        .fns = ~ round(.x,digits = 1)
      )
    )
  tab1[tab1 == 0] <- '--' # --- (1)
  mask_indices <- sapply(tab1, function(x) x[length(x)] %in% c(3, 4, 5)) %>%
    which()  
  tab1[, mask_indices] <- "--"
  tab1[-nrow(tab1), -c(1, mask_indices)] <- sapply(
    tab1[-nrow(tab1),-c(1, mask_indices)], function(x) # --- (2)
      ifelse(x != '--', paste(format(as.numeric(x), nsmall = 1), "%"), x)) # --- (3)
  tab1[nrow(tab1),2] <- all
  tab1$row_labels <- factor(tab1$row_labels,
                          levels = c(1,"N",2,3,4,5))
  
  dplyr::arrange(tab1,
                 row_labels)
  tab1 <- flextable::flextable(tab1)
  tab1

do we have any solution by removing 1st and last row to remove and then rbind them again

like we use in dplyr for columns rbind(N,everything())

(hiris <- head(iris))

(store_row <- hiris[2,] )
#delete row 
(hiris<-hiris[-2.,])
# add row
(hiris <- rbind(store_row,hiris))

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