What does this R command translate into in plain English?

  • Thread starter user366312
  • Start date
  • #1
user366312
Gold Member
71
3

Summary:

mean(mat[2, mat[1, ] == 1] == 1)

What does this R command translate into plain English?

Main Question or Discussion Point

According to my understanding:

243198

So the mean is: (1+1+2+1)/4 = 1.25.

Is my understanding correct?

If I am correct, what does it actually try to achieve?
 

Answers and Replies

  • #2
Ygggdrasil
Science Advisor
Insights Author
Gold Member
2019 Award
3,062
2,771
mean(mat[2, mat[1, ] == 1] == 1)
Why would anyone write a command like that?

It helps to break the problem down into steps:
  • First, consider what does mat[1,] evaluate as?
  • Next, what is the output of mat[1,]==1?
  • Next, what is mat[2, mat[1, ] == 1]?
  • Next, what is mat[2, mat[1, ] == 1] == 1?
  • Finally, you can calculate mean(mat[2, mat[1, ] == 1] == 1).
Given the matrix you provide in the OP, I get 0.5 as the final answer.
 
  • Like
Likes sysprog
  • #3
user366312
Gold Member
71
3
Given the matrix you provide in the OP, I get 0.5 as the final answer.
Yeah, I figured that out myself.

Thanks for the help anyway.
 
  • #4
1,582
860
Yeah, I figured that out myself.

Thanks for the help anyway.
That response looks as if you cared mainly about the answer, and not so much about the specifics of the clarifyingly cogent method carefully elucidated by @Ygggdrasil by which he arrived at it.

I'm sure your gratitude is appreciated by him; however, your edification would be immensely more gratifying.

Please at least don't fail to notice the value of the innermost-first innermore-next approach taken to parsing the expression.
 
  • #5
user366312
Gold Member
71
3
That response looks as if you cared mainly about the answer, and not so much about the specifics of the clarifyingly cogent method carefully elucidated by @Ygggdrasil by which he arrived at it.

I'm sure your gratitude is appreciated by him; however, your edification would be immensely more gratifying.

Please at least don't fail to notice the value of the innermost-first innermore-next approach taken to parsing the expression.
Speculation is bad for health!

I spent almost 4 hours on this to understand it properly.
 
  • #6
1,582
860
user366312 said:
Speculation is bad for health!

I spent almost 4 hours on this to understand it properly.
I didn't mean to disparage the value of your thinking things through for yourself. When I posted, I of course could not see all the avenues of your reasoning, and was responding merely as an observer of the prior posts. Thanks for reading and responding to what I wrote.
 

Related Threads on What does this R command translate into in plain English?

Replies
20
Views
845
Replies
12
Views
334
  • Last Post
Replies
1
Views
2K
Replies
6
Views
3K
  • Last Post
Replies
1
Views
590
Replies
5
Views
1K
Replies
1
Views
2K
  • Last Post
Replies
4
Views
4K
  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
7
Views
5K
Top