I wish this helpful for you I want to produce dataframes containing summary statistics for each factor level for multiple variables. , You could melt your data first: code :
library(reshape2)
df < melt(mydf,id.vars = 1)
df1 < ddply(df, c("Factor","variable"), function(x) my.summary(x$value))
df2 < split(df1,df1$variable)
list2env(df2,.GlobalEnv)
Share :

Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level
By : Shiv Prasad
Date : March 29 2020, 07:55 AM
around this issue I have an R data frame containing a factor that I want to "expand" so that for each factor level, there is an associated column in a new data frame, which contains a 1/0 indicator. E.g., suppose I have: , Use the model.matrix function: code :
model.matrix( ~ Species  1, data=iris )

stats::lm() in R: getting number of observations for factor level
By : Clemon Allison
Date : March 29 2020, 07:55 AM
Any of those help Since the comments above indicate a willingness to accept this as an answer: code :
table(model.frame(fit)$Species)

Appeding summary row of each factor level using dplyr in R
By : Netcrater
Date : March 29 2020, 07:55 AM
I hope this helps . Not sure it's more compact, but by reversing the order of the two group by processes, you can eliminate several lines of code: code :
df %>%
group_by(Reg, Res) %>% # group by Reg and Res first
summarise(Pop = sum(Pop), Pop1 = sum(Pop1)) %>%
bind_rows(
group_by(., Reg) %>% # now group by Reg and bind_rows with previous result
summarise(Pop = sum(Pop), Pop1 = sum(Pop1), Res = 'Total'),
. # <<<<<< bind total above other Res by passing previous
# result as second argument in the bind_rows
) %>% arrange(Reg)
# A tibble: 15 x 4
# Reg Pop Pop1 Res
# <chr> <int> <int> <chr>
# 1 A 2000853 2135 Total
# 2 A 1000517 1050 Rural
# 3 A 1000336 1085 Urban
# 4 B 3000844 2938 Total
# 5 B 1499485 1446 Rural
# 6 B 1501359 1492 Urban
# 7 C 1997259 2007 Total
# 8 C 999234 987 Rural
# 9 C 998025 1020 Urban
# 10 D 2000712 2052 Total
# 11 D 998760 1058 Rural
# 12 D 1001952 994 Urban
# 13 E 2999304 3050 Total
# 14 E 1501848 1547 Rural
# 15 E 1497456 1503 Urban

Appeding summary row of total for each factor level
By : Phillip Crowley
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Here is an option based on purrr::map_df and split. We can split df using City then loop through each City: bind at the top of each City a row Total which sum Pop and Pop1 code :
library(dplyr)
library(purrr)
df %>%
group_by(Res) %>%
summarise_if(is.numeric, sum, na.rm = TRUE) %>%
arrange(Res= factor(Res, levels=c('Urban','Rural'))) %>%
mutate(City = "State") %>%
bind_rows(df) %>%
mutate(City=factor(City, levels = c('State','A','B','C'))) %>%
split(.$City) %>%
map_df(., ~bind_rows(summarise_if(.x,is.numeric, sum) %>% mutate(City='Total', Res=''),
.x %>% mutate_if(is.factor, as.character)) %>%
select(City, Res, Pop, Pop1))
# A tibble: 12 x 4
City Res Pop Pop1
<chr> <chr> <int> <int>
1 Total "" 3001765 2972
2 State Urban 1501974 1483
3 State Rural 1499791 1489
4 Total "" 1000915 987
5 A Urban 500414 493
6 A Rural 500501 494
7 Total "" 999938 1009
8 B Urban 499922 497
9 B Rural 500016 512
10 Total "" 1000912 976
11 C Urban 501638 493
12 C Rural 499274 483

Looping through variables in a dataframe to find summary stats
By : David Webb
Date : March 29 2020, 07:55 AM
hope this fix your issue this should work for you. you don't need a loop. just use the summary() function.

