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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:38:25+00:00 2026-06-14T21:38:25+00:00

I have got a dataframe made up by three columns (see example in the

  • 0

I have got a dataframe made up by three columns (see example in the code). the first column contains categories (a), the second column the number of observations (b) and the third column the average value of these observations(c).

    #create a test df
    a<-factor(c("aaa","aaa","aaa","ddd","eee","ddd","aaa","ddd"))
    b<-c(3,4,1,3,5,7,3,2)
    c<-c(1,2,NA,4,5,6,7,NA)
    df.abc<-data.frame(a=a,b=b,c=c)
    df.abc

If the number of observations was 1 or 2 the entries where marked as missing values (NA).

So the aim of my function is to substitute theses missing values by the mean value of each category.

I took me while but I got a function working, that substitutes all the missing values for one category (in case that the observation was 1). It looks like this:

    #function to substitue the missing values in row c by their means 
    #according to their categories
    function.abc<-function(x){
        ifelse(
            (df.abc[,1]==x)&(df.abc[,2]==1),
            mean(df.abc$c[df.abc$a ==x],na.rm=TRUE),
            df.abc[,3]
        )
    }

Testing this function:

    #test the function for the category "ccc"
    function.abc("aaa")

It works quite well (but is only the mean rather than the average mean) The output is:

[1] 1.000000 2.000000 3.333333 4.000000 5.000000 6.000000 7.000000 NA

Now my problem is, that i have quite a lot of categories (n=32) and I tried to apply this function over a vector containing my categories. A simpe example in this case would be:

    #test the function for a testvector
    test.vector<-c("aaa","ddd")
    function.abc(test.vector)

the output is:

[1] 1.0 2.0 4.5 4.0 5.0 6.0 7.0 NA

So obviously this won’t work out…

Can anybody help me to rearrange the function? I’m quite new to programming and it is still a big challenge for me to design short and goodworking functions…

Edit:

I would like the output to be:
[1] 1.000000 2.000000 3.20000 4.000000 5.000000 6.000000 7.000000 5.000000

so that the average of group aaa (3.20000) substitutes the NA value in aaa and the average of group ddd (5.0000000) substitutes the NA in ddd…

  • 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-14T21:38:26+00:00Added an answer on June 14, 2026 at 9:38 pm

    In order to work with multiple columns at once within a category you will need to use something that splits the dataframe and then works on the components. The lapply( split(df, fac), function(x) {...}) paradigm works well for this. Or you can use transform or the plyr package.

    > lapply( split( df.abc, df.abc$a), 
                   function(dfrm) { dfrm[is.na(dfrm$c), "c"] <- 
                      weighted.mean(dfrm[!is.na(dfrm$c) , "c"], dfrm[!is.na(dfrm$c), "b"])
                             dfrm} )  
                    # need to evaluate dfrm in order to return the full value.
    $aaa
        a b   c
    1 aaa 3 1.0
    2 aaa 4 2.0
    3 aaa 1 3.2
    7 aaa 3 7.0
    
    $ddd
        a b   c
    4 ddd 3 4.0
    6 ddd 7 6.0
    8 ddd 2 5.4
    
    $eee
        a b c
    5 eee 5 5
    

    You can then rbind them using `do.call:

     do.call( rbind, lapply( split( df.abc, df.abc$a), 
              function(dfrm) { dfrm[is.na(dfrm$c), "c"] <-
                     weighted.mean(dfrm[!is.na(dfrm$c) , "c"], dfrm[!is.na(dfrm$c), "b"])
                       dfrm} ) )
            a b   c
    aaa.1 aaa 3 1.0
    aaa.2 aaa 4 2.0
    aaa.3 aaa 1 3.2
    aaa.7 aaa 3 7.0
    ddd.4 ddd 3 4.0
    ddd.6 ddd 7 6.0
    ddd.8 ddd 2 5.4
    eee   eee 5 5.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got some problems with plotting a sliced DataFrame with entire columns filled
I have got this code: function init(){ if (typeof window.jQuery !== 'function') { var
I have got two pages. example.com/php.com and example.com. I use this function to set
I've got a dataframe with a text column name and factor city . It
I've got a dataframe with several (numeric) columns, and want to make a new
I've got a dataframe dat of size 30000 x 50. I also have a
I have got a huge 1000 x 100000 dataframe like following to recode to
I have got a List<Problem> which I want to export to a column based
I have a dataframe(df) with 3 columns: test1 test2 res 500 23 30.75 200
Have got an NSString *str = @12345.6789 and want to find out if there

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.