Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:59:40+00:00 2026-05-27T10:59:40+00:00

I am pretty sure I am complicating things. I have a data frame with

  • 0

I am pretty sure I am complicating things. I have a data frame with p variables (here: v1 to v3) and two factor variable (here: sex and unemp):

> head(df)
  sex unemp v1 v2 v3
1   0     0  2  4  4
2   0     0  2  1  1
3   1     0  3  3  5
4   1     1  2  3  5
5   0     0  1  2  5
6   1     0  3  5  4

I now would like to modify (i.e. compute median and mean and then rearrange the summary table) my data in such way that the resulting data frame looks like this (for men or women):

> df.res.men
   median.unemp.1 median.unemp.0 mean.unemp.1 mean.unemp.0
v1            2.0            2.0     2.666667     2.391304
v2            2.0            3.5     2.500000     3.369565
v3            4.5            3.0     4.166667     2.956522

Here is the full code:

library(plyr)
## generate data
set.seed(1)
df <- data.frame(sex=rbinom(100, 1, 0.5),
                 unemp=rbinom(100, 1, 0.2),
                 v1=sample(1:5, 100, replace=TRUE),
                 v2=sample(1:5, 100, replace=TRUE),
                 v3=sample(1:5, 100, replace=TRUE)
                 )
head(df)

## compute mean and median for all variables by sex and unemp
df.mean <- ddply(df, .(unemp, sex), .fun=colMeans, na.rm=TRUE)
df.mean
df.median <- ddply(df, .(unemp, sex), .fun=function(x)apply(x,2,median, na.rm=TRUE))
df.median

## rearrange summary table
df.res.men <- cbind(t(subset(df.median, sex==0 & unemp==1)),
                 t(subset(df.median, sex==0 & unemp==0)),
                 t(subset(df.mean, sex==0 & unemp==1)),
                 t(subset(df.mean, sex==0 & unemp==0)))
df.res.men <- df.res.men[-c(1:2),]
colnames(df.res.men) <- c("median.unemp.1", "median.unemp.0", 
                          "mean.unemp.1", "mean.unemp.0")
df.res.men
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:59:40+00:00Added an answer on May 27, 2026 at 10:59 am

    Here is one approach

    library(plyr); library(reshape2)
    dfm <- melt(df, id = c('sex', 'unemp'))
    df2 <- ddply(dfm, .(variable, unemp, sex), summarize, 
      avg = mean(value), med = median(value))
    
    df2m <- melt(df2, id = 1:3, variable.name = 'sum_fun')
    df_0 <- dcast(df2m, sex + variable ~ sum_fun + unemp, subset = .(sex == 0))
    
       sex variable    avg_0  avg_1 med_0 med_1
    1   0       v1 2.794872 3.0000     3   3.5
    2   0       v2 3.102564 2.8750     3   3.0
    3   0       v3 3.205128 3.1875     3   4.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty sure I have seen this before, but I haven't found out
I'm pretty sure this is a simple question in regards to formatting but here's
I pretty sure I have to create some sort of mold of what the
I am pretty sure that I have initialised everything, but it still throws Object
Pretty sure that I'm missing something really simple here: I'm trying to display a
I'm not pretty sure if this question is for here, but I want to
I'm pretty sure what sort of things can be done with NHibernate but I
I'm pretty sure I have the Inheritance overriding right, but I was hoping for
I am pretty sure my logic is flowed, so please help: I have a
I'm pretty sure I don't have any other options other than what I've uncovered,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.