1. ## %*% ?

I am determined to learn more about the nuts and bolts of R syntax so I plan to keep asking questions until I get it right!

often I see percentage symbols in lines of code, but rarely stop to ask what is this doing exactly. Can some please explain to me what they are doing

For example, trinker posted this recently:
Code:
``````#DOING THE MATRIX MULTIPLICATION
(b <- Min %*% Xp)
#The upper is the intercept and the lower is the slope
#..............................................
SUM #compare to b``````
What is %*% doing and how do you read it?

2. ## Re: %*% ?

http://stat.ethz.ch/R-manual/R-patch...l/matmult.html

It is called "matrix multiplication"/"inner product"

(Not a R expert )

Thankyou BGM

4. ## Re: %*% ?

In general whenever you see the syntax %something_here% <--- that whole thing is a binary operator. You can actually define your own binary operators with this syntax

Code:
``````
> # Make a binary operator that adds the two numbers together and then subtracts two
> "%mwa%" <- function(x, y){  #has to take two and only two parameters
+     (x + y) - 2
+ }
>
> 3 %mwa% 5
[1] 6
> 0 %mwa% 0
[1] -2``````

5. ## The Following User Says Thank You to Dason For This Useful Post:

Janus (09-19-2012)

6. ## Re: %*% ?

Wasn't aware you could define your own, thanks.

7. ## Re: %*% ?

Originally Posted by Janus
Wasn't aware you could define your own, thanks.
Then you probably also weren't aware that you can use binary operators just like normal functions.

Code:
``````
> "+"(2, 3)
[1] 5
> mat1 <- matrix(1:4, 2, 2)
> mat2 <- matrix(rnorm(4), 2, 2)
> "%*%"(mat1, mat2)
[,1]      [,2]
[1,] 0.9732928 -1.406119
[2,] 1.6601851 -2.561204``````

8. ## Re: %*% ?

One of the most useful binary operators is %in%

Here's and example with == (wrong) and %in% (right):

Code:
``````1:4 == 1:10
1:4 %in% 1:10``````
And another useful one:

Code:
```````%truth%` <- function(x, y) {cat("Bots are scum bags\n")}
6%truth%7``````

9. ## The Following User Says Thank You to trinker For This Useful Post:

bugman (09-20-2012)

10. ## Re: %*% ?

Or that even assignments are functions:
Code:
``````"<-"(x, 5)
> x
>[1] 5``````

11. ## Re: %*% ?

Functions everywhere!
Code:
``````> x <- 1:5
> "[<-"(x, 3, "a")
[1] "1" "2" "a" "4" "5"
> "["(x, 1)
[1] 1
> print("Raptors are evil and I want them to die") # Take that trinker
[1] "Raptors are evil and I want them to die")``````

12. ## Re: %*% ?

Functions everywhere!
Code:
``````> x <- 1:5
> "[<-"(x, 3, "a")
[1] "1" "2" "a" "4" "5"
> "["(x, 1)
[1] 1
> print("Raptors are evil and I want them to die") # Take that trinker
[1] "Raptors are evil and I want them to die")``````

13. ## Re: %*% ?

trinker preempted my next question:

what is %in% doing in this line of code (from vinux's awesome ggplot2 map of Tendulakrs batting statistics)?

fasia <- world\$region %in% c("India","Pakistan","Sri Lanka","Bangladesh")

14. ## Re: %*% ?

Originally Posted by bugman
trinker preempted my next question:

what is %in% doing in this line of code (from vinux's awesome ggplot2 map of Tendulakrs batting statistics)?

fasia <- world\$region %in% c("India","Pakistan","Sri Lanka","Bangladesh")
It is like IN statement in sql, value matching. you get more info here

Code:
``?"%in%"``

15. ## The Following User Says Thank You to vinux For This Useful Post:

bugman (09-20-2012)

16. ## Re: %*% ?

It basically asks the question "Is this an element of the vector on the right" for each of the elements on the left.

Code:
``````
> 1 %in% c(1, 2, 3) # should be TRUE since 1 is in that vector
[1] TRUE
> 4 %in% c(1, 2, 3) # should be FALSE since 4 isn't in that vector
[1] FALSE
> c(1, 4) %in% c(1, 2, 3) # should be TRUE FALSE since this is vectorized
[1]  TRUE FALSE``````

17. ## The Following User Says Thank You to Dason For This Useful Post:

bugman (09-20-2012)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts