1

I am once again asking for you support!

I have the data frame, where different indecies on nation are presented, you can see them in the picture here it is.

I want to compute all these indecies into one new variable but I do not know how to do it. I tried rowMeans() function but I might be a very stupid person, so I have not succeded in computing.

mean() did not worked - it appeared that "the argument is not numeric or logic: NAs to return". I have also concerns that mean() computes the means in the whole column as a vector, not by the rows, as I need. Once again - I need to recieve means from nation# variables for each observation in the data frame.

Could anyone, please, help me with this pretty simple task?

r2evans
  • 141,215
  • 6
  • 77
  • 149
rg4s
  • 811
  • 5
  • 22
  • What is your expected output – akrun Mar 25 '20 at 17:49
  • Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557/3358272 (and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans Mar 25 '20 at 17:50
  • @akrun I want a new varible with a value of ONLY "nation#" means – rg4s Mar 25 '20 at 18:04
  • @r2evans sorry, my bad – rg4s Mar 25 '20 at 18:04
  • 2
    Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, plus verbatim errors/warnings received), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and expected output. Refs: https://stackoverflow.com/questions/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Mar 25 '20 at 18:06
  • I've mentioned that I am the stupid person, haven't I? – rg4s Mar 25 '20 at 18:18

2 Answers2

1

We can use rowMeans with startsWith

rowMeans(dat[startsWith(colnames(dat), "nation")])
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you, but this variant still computes means for all variables :( – rg4s Mar 25 '20 at 17:59
  • @k1rgas it computes only for 'nation' variables (similar to the other answer) – akrun Mar 25 '20 at 17:59
  • I tried your variant and have recieved the whole NA column – rg4s Mar 25 '20 at 18:03
  • @k1rgas do you have. NAs in the dataset or are the columns numeric?. If it is not numeric, then it can end up with NA – akrun Mar 25 '20 at 18:04
  • there are no NAs, but other variables are factors or characters. not all the values are numerics or integers. – rg4s Mar 25 '20 at 18:05
  • @k1rgas if the nation. variables are all numeric, then this should work because I tested on the data. provided in my competitors post – akrun Mar 25 '20 at 18:06
  • they are all numerics, and this still does not work :( – rg4s Mar 25 '20 at 18:07
  • @k1rgas i meant have you checked `str(yourdata)`. and are all of them `numeric` class or `factor` – akrun Mar 25 '20 at 18:08
0
dat <- data.frame(
  nation1 = c(0,3,5,3,5,4), nation2 = c(4,2,5,3,5,4),
  ethnicity1 = c(5,5,5,5,5,2), ethnicity2 = c(5,5,5,2,4,4)
)
rowMeans(dat[, grepl("^nation", colnames(dat))])
# [1] 2.0 2.5 5.0 3.0 5.0 4.0
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • Thank you, but it did not help me though :( – rg4s Mar 25 '20 at 18:02
  • 2
    Good luck, k1rgas. We don't have your data (you are *expecting* us to transcribe it manually), and you have not given us any errors. Then in your comment to akrun you said *"computes only for 'nation' variables"*, can you explain why that is at odds with your other comment *"want a new varible with a value of ONLY "nation#" means"*. – r2evans Mar 25 '20 at 18:05