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 947705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:07:23+00:00 2026-05-15T23:07:23+00:00

I have a data.frame that looks like this > head(df) Memory Memory Memory Memory

  • 0

I have a data.frame that looks like this

> head(df)
            Memory    Memory    Memory    Memory    Memory     Naive     Naive
10472501  6.075714  5.898929  6.644946  6.023901  6.332126  8.087944  7.520194
10509163  6.168941  6.495393  5.951124  6.052527  6.404401  7.152890  8.335509
10496091 10.125575  9.966211 10.075613 10.310952 10.090649 11.803949 11.274480
10427035  6.644921  6.658567  6.569745  6.499243  6.990852  8.010784  7.798154
10503695  8.379494  8.153917  8.246484  8.390747  8.346748  9.540236  9.091740
10451763 10.986717 11.233819 10.643245 10.230697 10.541396 12.248487 11.823138  

and I’d like to find the mean of the Memory columns and the mean of the Naive columns. The aggregate function aggregates rows. This data.frame could potentially have a large number of rows, and hence transposing then applying aggregate by the colnames of the original data.frame strikes me as bad, and is generally annoying:

> head(t(aggregate(t(df),list(colnames(df)), mean)))
         [,1]       [,2]      
Group.1  "Memory"   "Naive"   
10472501 "6.195123" "8.125439"
10509163 "6.214477" "7.733625"
10496091 "10.11380" "11.55348"
10427035 "6.672665" "8.266854"
10503695 "8.303478" "9.340436"

What’s the blindingly obvious thing I’m missing?

  • 1 1 Answer
  • 4 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-15T23:07:24+00:00Added an answer on May 15, 2026 at 11:07 pm

    I am a big advocate of reformatting data so that it’s in a “long” format. The utility of the long format is especially evident when it comes to problems like this one. Fortunately, it’s easy enough to reshape data like this into almost any format with the reshape package.

    If I understood your question right, you want the mean of Memory and Naive for every row. For whatever reason, we need to make column names unique for reshape::melt().

    colnames(df) <- paste(colnames(df), 1:ncol(df), sep = "_")
    

    Then, you’ll have to create an ID column. You could either do

    df$ID <- 1:nrow(df)
    

    or, if those rownames are meaningful

    df$ID <- rownames(df)
    

    Now, with the reshape package

    library(reshape)
    df.m <- melt(df, id = "ID")
    df.m <- cbind(df.m, colsplit(df.m$variable, split = "_", names = c("Measure", "N")))
    df.agg <- cast(df.m, ID ~ Measure, fun = mean)
    

    df.agg should now look like your desired output snippit.

    Or, if you want just the overall means across all the rows, Zack’s suggestion will work. Something like

    m <- colMeans(df)
    tapply(m, colnames(df), mean)
    

    You could get the same result, but formatted as a dataframe with

    cast(df.m, .~variable, fun = mean)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a data.frame named pp that looks like this: > head(pp) X02R X03N
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data frame that looks like this: user1,product1,0 user1,product2,2 user1,product3,1 user1,product4,2 user2,product3,0
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >
I have a large data frame (named z ) that looks like this: RPos
I have a dataset that looks like this: a <- data.frame(rep(1,5),1:5,1:5) b <- data.frame(rep(2,5),1:5,1:5)
I have a data.frame that looks like this: ID Date.A Date.B Variable A 01/01/2012

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.