-4

I need to create a new variable in my dataset that finds the mean of three variables currently in the dataset. Each row of data is different. I cannot seem to work out how to do it without finding the mean of the total of the variables.

I have three columns called 74, 75 and 78, all of which contain a number. I need to create a new column called mean.re, that calculates the mean of the three numbers for each individual row.

Cheers,

  • Do you have a data frame with 3 columns? Do you want a mean for each column (try `colMeans`)? Do you want the mean of 3 values in every row (try `rowMeans`)? – Eumenedies Mar 05 '18 at 11:56
  • Think mines a bit different. Not sure tried creating this new variable mean.re <- .colMeans(df$re74, df$re75, df$re78, na.rm = FALSE) sorry I'm awful at this never used it before – Edward Gray Mar 05 '18 at 12:03
  • @Eumenedies I've got three columns and want the values in all three of each row to have a mean – Edward Gray Mar 05 '18 at 12:10
  • I've tried mean.re <- rowMeans(df$re74, df$re75, df$re78, na.rm = FALSE, dims =1) but doesn't work – Edward Gray Mar 05 '18 at 12:14
  • 1
    @EdwardGray Welcome to SO! Please give a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Put additional Information not in comments but in your question, i.e. **edit your question!** https://stackoverflow.com/posts/49109900/edit – jogo Mar 05 '18 at 12:21
  • 1
    @Niek isn't that the same as `rowMeans(df[,c('re74','re75','re78')])` but slower? – Eumenedies Mar 05 '18 at 12:50
  • none of these are working thanks anyway though guys – Edward Gray Mar 05 '18 at 13:03

1 Answers1

0

Sorry, can't comment but basically you just need to try colMeans(df), the whole matrix/data.frame, not column by column. Whenever you have troubles about a function, you can type e.g. ?colMeans in the console to see description, including what arguments it needs. Also, this is definitely a duplicate question, look better next time.

EDIT: Sorry, I misunderstood your question. I thought you wanted column means, but it seems you want row means? In that case you use rowMeans, just as suggested above.

yassem
  • 41
  • 6
  • Possibly but never used R before so appreciating any basic rudimentary advice – Edward Gray Mar 05 '18 at 12:28
  • I either case, did `colMeans(df)` do the trick? – yassem Mar 05 '18 at 12:41
  • it might do, but tbh I wouldn't have a clue how to program that anyway – Edward Gray Mar 05 '18 at 12:47
  • I don't understand, you've tried ` mean.re <- .colMeans(df$re74, df$re75, df$re78, na.rm = FALSE)`, yes? It's just that instead of giving each column as a separate argument, you need to give the whole dataframe/matrix, so ` mean.re <- .colMeans(df, na.rm = FALSE)`, and you'll have a vector of length 3 with the mean of each column - that's what you want, yes? – yassem Mar 05 '18 at 12:55
  • Yes i tried that. But like i say never used r before don't have a clue what any of these terms mean. Its typical crap uni teaching, they go on strike for a month and expect you to teach yourself. – Edward Gray Mar 05 '18 at 13:05
  • If you have to use it, I strongly recommend you at least acquire the basic. There are lots of free tutorials online. [This one](https://cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf) is quite extensive, though pretty technical. You can probably skip most of it, but several sections in Chapter 3 might be very helpful. Once you get the hang of the basics it's fairly simple. – yassem Mar 05 '18 at 13:18