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

  • Home
  • SEARCH
  • 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 8586345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:17:18+00:00 2026-06-11T22:17:18+00:00

My data frame looks like this: > df id u.1t u.2 v.1 v.2 1

  • 0

My data frame looks like this:

> df
  id u.1t u.2 v.1 v.2
1  A    1  NA   5  NA
2  A    2  NA   4   6
3  A    1   4   5  NA
4  B   10  13  40  NA
5  B   10  12  42  NA
6  B   10  NA  41  NA

and I would like to know the id-specific means of the u.* and the v.* columns respectively like this:

> mean
  id u.mean v.mean
1  A      2      5
2  B     11     41

this is the data

df<-data.frame(id=c("A","A","A","B","B","B"),u.1t=c(1,2,1,10,10,10),u.2=c(NA,NA,4,13,12,NA),v.1=c(5,4,5,40,42,41),v.2=c(NA,6,NA,NA,NA,NA))

As is clear, by introducing NA’s, the overall mean is unequal to the mean of the row- or column-means, which is the problem here.

I thought this to be a job for by, but it turns out I can’t get by to do anything but columnwise operations?

Help is greatly appreciated–thanks

  • 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-06-11T22:17:19+00:00Added an answer on June 11, 2026 at 10:17 pm

    If you want to use by, try something like this:

    by(x, x$id, function(x) colMeans(x[,-1], na.rm=TRUE))
    

    The output is a bit ugly. While you can tidy it up, I would use the plyr package:

    library(plyr)
    ddply(x, .(id), function(x) colMeans(x[,-1], na.rm=TRUE))
    

    This doesn’t quite achieve what you are after, as it takes the average of each column: it doesn’t combine the u.* and v.* columns. To do that, I would melt the data first and then use plyr:

    library(reshape2)
    y <- melt(x)
    y$variable <- gsub("\\..*", '', y$variable)
    y
    #   id variable value
    #1   A        u     1
    #2   A        u     2
    #3   A        u     1
    #4   B        u    10
    #5   B        u    10
    #6   B        u    10
    #7   A        u    NA
    #    (etc)
    
    z <- ddply(y, .(id, variable), summarise, mean = mean(value, na.rm=TRUE))
    z
    #  id variable mean
    #1  A        u    2
    #2  A        v    5
    #3  B        u   11
    #4  B        v   41
    

    If you choose to, you can cast this back:

    dcast(z, id~variable)
    #  id  u  v
    #1  A  2  5
    #2  B 11 41    
    
    • 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 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 3 column data frame which looks a little like this: id
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 semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a data.frame that looks like this: ID Date.A Date.B Variable A 01/01/2012
I have the following question: I have data frame which looks like this. I
I have a column in a data frame that looks something like this: [1]

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.