number of count in function

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

number of count in function

shaaib
Hi I have created function for a summary and i want "N" (number of
frequency) should come at the second row but its coming in between
variables, not coming at top and bottom. please help where i am missing or
where i need to change.

multi<-function(dataset,var_list,var_name){
data<-dataset[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)]
tab<-Reduce(rbind,lst2)
tab<-tab%>%
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)%>%
dplyr::rename(" "=Var1)
tab[,2]<- mask_m(tab[,2],tab[,3])
tab[,2]<-ifelse(tab[,2]=="--","--", paste0(roundUp(tab[,2]),"%"))
tab<-rbind(c("N",sum(total_column,na.rm = TRUE)),tab[,1:2])
tab
}

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.