needed to update the table header with display frequency

below is my function and is working fine but i want to have column name to have TT1 should be displayed like "TT1 (n= 11)" due to coersion i am not able to update . how can i do that

data <- data.frame(col1= c(2,1,3,2,0,4,2,3,1,5,4),
col2= c(1,2,2,1,4,3,2,4,0,5,0),
col3= c(1,1,0,3,2,4,1,0,1,3,5))  

freq<-function(data,var){  
t0<-table(data[[var]])
t0<-as.data.frame(t0)
t0
}

var_name <- "TT1"
var_list<-c("col1", "col2", "col3")

  data<-data[unlist(var_list)]
  
  total_column<-NA^!rowSums(!is.na(data[,1:ncol(data)]))
  lst1 <- lapply(names(data[,1:ncol(data)]), function(x) freq(data,x))
  lst2 <- lst1[!sapply(lst1, is.null)]
  tab1<-Reduce(rbind,lst2)
  tab<-tab1%>% mutate(Var1=as.character(Var1),
                      Freq=as.numeric(Freq),
                      N=sum(total_column,na.rm = TRUE))%>%
    mutate(UQ(rlang::sym(var_name)) := Freq*100/N)%>%
    select(Var1,!!var_name,N)
  

the output should be look like below

image

library(glue)
tab |>
  rename_with(.cols = var_name,
              .fn=\(x)glue("{x} (n={total_column})"))

or without glue

tab |>
  rename_with(.cols = var_name,
              .fn=\(x)paste0(x, " (n=",total_column,")"))

(x) this giving error while adding

tab <-

  • rename_with(.cols = var_name,
    
  •             .fn = paste0(x, " (n=",total_column,")"))
    

Error in UseMethod("rename_with") :
no applicable method for 'rename_with' applied to an object of class "character"

this is a modern R language feature for anonymous functions, its a shortcut for the longer
function(x) that you can use instead.

tab |>
  rename_with(.cols = var_name,
              .fn=function(x)paste0(x, " (n=",total_column,")"))

why its working for you but not me showing me error

Error in rename_with():
! .fn must return a vector of length 1, not 11.

Backtrace:

  1. ├─tab %>% ...
  2. ├─dplyr::rename_with(...)
  3. └─dplyr:::rename_with.data.frame(...)
    Run rlang::last_trace(drop = FALSE) to see 2 hidden frames.

oh, my bad, you had a strange way of making
total_column<-NA^!rowSums(!is.na(data[,1:ncol(data)]))
I changed it in my code to be

total_columns <- nrow(na.omit(data))
it means you can get rid of the sum over total_columns later as well

i am sorry ,i am totally confused , so my data will be multi response question where people have given multi responses in different variables . so please let me know what should i update i the function. because i am confused now. and i am not the expert of R like you

what does total_column mean to you ? you had this complicated thing

total_column<-NA^!rowSums(!is.na(data[,1:ncol(data)]))

you havent shown that you do this for any reason other than later to sum it together

 N=sum(total_column,na.rm = TRUE)

the point of it is to make the number 11 right ?

total_column<-NA^!rowSums(!is.na(data[,1:ncol(data)]))

so basically there will be many variable where a person didn't respond so here i taking sum of all columns excluding NA and NA means no response provided . and this logic is working perfectly fine.

the only required objective is to make column name as TT1 (n=11)

i am sorry for the confusion happened

you are rowSums , so you are counting rows not columns.
dim(data) shows 3 columns and 11 rows, and you seem happy with n=11 and not wanting n=3
so i suggested a simpler formula for that.

Anyway, here is what I did; you can backtrack and replace your own logic for how to calculate n as you please.

library(tidyverse)
data <- data.frame(col1= c(2,1,3,2,0,4,2,3,1,5,4),
                   col2= c(1,2,2,1,4,3,2,4,0,5,0),
                   col3= c(1,1,0,3,2,4,1,0,1,3,5))  

freq<-function(data,var){  
  t0<-table(data[[var]])
  t0<-as.data.frame(t0)
  t0
}

var_name <- "TT1"
var_list<-c("col1", "col2", "col3")

data<-data[unlist(var_list)]

n_num <-nrow(na.omit(data))
lst1 <- lapply(names(data[,1:ncol(data)]), function(x) freq(data,x))
lst2 <- lst1[!sapply(lst1, is.null)]
tab1<-Reduce(rbind,lst2)
tab<-tab1%>% mutate(Var1=as.character(Var1),
                    Freq=as.numeric(Freq),
                    N=n_num)%>%
  mutate(UQ(rlang::sym(var_name)) := Freq*100/N)%>%
  select(Var1,!!var_name,N) |>
  rename_with(.cols = var_name,
              .fn = function(x)paste0(x, " (n=",n_num,")"))

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.