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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:51:02+00:00 2026-06-16T22:51:02+00:00

I have the following data frame: dat <- read.table(text= X prob 1 1 0.1

  • 0

I have the following data frame:

dat <- read.table(text="  X prob
1 1  0.1
2 2  0.2
3 3  0.4
4 4  0.3", header=TRUE)

Is there any built-in function or elegant way to calulate mean and variance for discrete random variables in R?

  • 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-16T22:51:03+00:00Added an answer on June 16, 2026 at 10:51 pm

    There is a weighted.mean function in base R and the Hmisc package has a bunch of wtd.* functions.

    > with(dat, weighted.mean(X, prob))
    [1] 2.9
    
    require(Hmisc)
    >  wtd.var(x=dat$X, weights=dat$prob)
    [1] Inf
    # Huh ?  On investigation the weights argument is suppsed to be replicate weights
    # So it's more appropriate to use normwt=TRUE
    > wtd.var(x=dat$X, weights=dat$prob, normwt=TRUE)
    [1] 1.186667
    

    The survey package from Thomas Lumley provides much more than this simplistic example illustrates. It has the mechanism for handling complex weighting schemes for a variety of statistical modeling procedures:

    require(survey)
    > dclus1<-svydesign(id=~1, weights=~prob, data=dat)
    >   v<-svyvar(~X, dclus1)
    > v
      variance     SE
    X   1.1867 0.7011
    

    These are sample statistics rather than the variances that would be calculated for abstract random variables. This result does seem appropriate for a statistical system, but might not be the correct answer for a probability homework question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data frame x <- read.table(text = id1 id2 val1 val2
Let's say I have the following data frame. dat <- data.frame(city=c(Chelsea,Brent,Bremen,Olathe,Lenexa,Shawnee), tag=c(rep(AlabamaCity,3), rep(KansasCity,3))) I
I have the following data frame: > dat V1 V2 1 1 6868 2
Let's say I have the following dataset bodysize=rnorm(20,30,2) bodysize=sort(bodysize) survive=c(0,0,0,0,0,1,0,1,0,0,1,1,0,1,1,1,0,1,1,1) dat=as.data.frame(cbind(bodysize,survive)) I'm aware that
I have following code to draw my logistic distribution: x=seq(-2000,2000,length=1000) dat <- data.frame(x=x) dat$value
I have the following data frame: id<-c(1,2,3,4,1,1,2,3,4,4,2,2) period<-c(first,calib,valid,valid,calib,first,valid,valid,calib,first,calib,valid) df<-data.frame(id,period) typing table(df) results in period
I have the following data.frame: sample <- data.frame(day=c(1,2,5,10,12,12,14)) sample.table <- as.data.frame(table(sample$day)) Now what I'd
I have the following data read into R as a data frame named data_old:
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
I have the following data test<-data.frame(group=1:10, var.a=rnorm(n=10,mean=500,sd=20), var.b=runif(10)) I would like a barplot with

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.