I'm trying to teach myself basics of R and I couldn't find the answer:

Say, I have a csv file and I want to calculate weighted mean for each subject such that I have a mean mu = 0.015*1030+0.16*26930+0.24*0+0.87*250+0.29*310+0.77*6240+0.98*3730+0.98*0+0.08*1400 for Subject A, for example.

How would I accomplish it in R or Excel?

Also, if I need to multiply each pair by corresponding value from the same row from the column salary:



enter image description here


enter image description here


Given your data is in DataFrame called df, you can simply do this:

mu = sum(df$Probability * df$SubjectA * df$salary)
  • $\begingroup$ Thank you! A stupid noob question: how do I create a new row containing mu for all subjects in the existing csv or at least generate a new csv with that data? Thanks! $\endgroup$ Dec 28 '18 at 10:38
  • $\begingroup$ Assuming you mean a new column containing mu: df$mu = rep(sum(df$Probability * df$SubjectA * df$salary),nrow(df)) $\endgroup$
    – bradS
    Dec 28 '18 at 11:16
  • $\begingroup$ I've updated the question with the actual dataset. It contains columns of states and rows of professions. There is also a column denoting how likely this occupation is to disappear due to automation. I want to calculate total job attrition for each state. Perhaps, it's be better to generate a new data frame with a list of states and total attrition (mu) for each one. How would I do it? Thanks $\endgroup$ Dec 28 '18 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.