logo
down
shadow

Summary stats by factor level for multiple variables


Summary stats by factor level for multiple variables

By : mcbr
Date : November 22 2020, 02:42 PM
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 : facebook icon twitter icon
Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level

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

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

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

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

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.
code :
summary(cars)
Related Posts Related Posts :
  • How to sort groups within sorted groups?
  • How to get randomForest model output in probability using Caret?
  • how to plot a data frame into R
  • Calculate mean of a range of rows
  • How to sum the values in a text file?
  • Looking up polygons in a shapefile that point belong in... why does it work with some shapefiles, not with others?
  • R creating a list using variable columns from df
  • Determine programmatically if a function call refers to your own function or a package's (or base R's) function?
  • Adding Special Characters to a plot in R
  • How to dynamically change R .libPaths() based on hostname?
  • Print arguments of a function in R
  • Creating Variables via A loop in r
  • How to randomly divide an integer into a fixed number of integers, such that the obtained tuples are uniformly distribut
  • Remove columns using column name based on levels of factors
  • What units are the 'width = ' in geom_bar(aes = ) and position_dodge(width = ) rendered in?
  • Assigning Dates to Fiscal Year
  • conditional removal from data frame
  • Getting variables out of a function in R
  • Bar colors in ggplot2 geom_bar not applied correctly (R)
  • Create a vector in a dataframe based on matching a second vector to a row name in another object
  • Convert julian day to mm/dd and add year from another column in dataframe in r
  • Change SMOTE parameters inside CARET k-fold cross-validation classification
  • rollapply classes each segment the same
  • rename the header of the spliting group in r
  • Processing Rmarkdown inside chunks
  • R: how to join the duplicate rows in one dataframe
  • Creating a unified time-series, with dates coming from different (natural) languages
  • R error: attempt to apply non-function , randomForest , rfImpute
  • assign multiple color to each vertex in igraph
  • How do I add shading and color to the confidence intervals in ggplot 2 generated Kaplan-Meier plot?
  • How to generate spatial points with a pattern
  • ggplot2 + stat_contour variable binwidth
  • Add fake data to a data frame based on variable condition
  • Utilizing a character vector inside a function
  • match fundction with data frames that are differently constructed
  • Overlaying plots with a horizontal date in R
  • lapply'ing a for loop returns NULL
  • Dynamically add UI elements and gather their input in a dataframe in shiny
  • All the possible outcomes
  • How to Use corrplot with method="number" and Drop Leading Zero?
  • cbind converting factor to numeric
  • create names of loop when iterating data frame
  • R reading multiple raster files gives error in for loop
  • Debugging into and changing R-functions in packages
  • Categorical variables in R - which one does R pick as reference?
  • Create new data frame based on values from another data frame
  • Subplots using Plotly in R (bug fixed)
  • WinBUGS error 'expected key word END' caused by wrong exponential code (not length of data)
  • Predict with gls
  • R list to wide (sparse) data frame
  • Installing ZeligMultilevel. Error : object ‘describe’ is not exported by 'namespace:Zelig'
  • How to use a variable created in a function in your console
  • Dynamic grid plots in shiny R
  • Display unicode in R
  • value of NULL after inserting value
  • Model multiple imputation with interaction terms
  • Dynamically Splitting and naming the dataframe
  • Cross-correlation of 5 time series (distance) and interpretation
  • Grouping columns based on pairwise equality
  • Checking if multiple variables are all the same value in R
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co