+ Reply to Thread
Results 1 to 13 of 13

Thread: %*% ?

  1. #1
    Super Moderator
    Points: 14,585, Level: 78
    Level completed: 34%, Points required for next Level: 265
    bugman's Avatar
    Posts
    1,492
    Thanks
    88
    Thanked 139 Times in 108 Posts

    %*% ?



    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?
    The earth is round: P<0.05

  2. #2
    TS Contributor
    Points: 14,811, Level: 78
    Level completed: 91%, Points required for next Level: 39

    Posts
    2,295
    Thanks
    8
    Thanked 371 Times in 351 Posts

    Re: %*% ?

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

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

    (Not a R expert )

  3. #3
    Super Moderator
    Points: 14,585, Level: 78
    Level completed: 34%, Points required for next Level: 265
    bugman's Avatar
    Posts
    1,492
    Thanks
    88
    Thanked 139 Times in 108 Posts

    Re: %*% ?

    Thankyou BGM
    The earth is round: P<0.05

  4. #4
    RotParaTon
    Points: 46,105, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,063
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

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

    Janus (09-19-2012)

  6. #5
    Points: 2,160, Level: 28
    Level completed: 7%, Points required for next Level: 140

    Location
    Chicago, IL
    Posts
    105
    Thanks
    1
    Thanked 19 Times in 15 Posts

    Re: %*% ?

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

  7. #6
    RotParaTon
    Points: 46,105, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,063
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: %*% ?

    Quote Originally Posted by Janus View Post
    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
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  8. #7
    FormerlyKnownAsRaptor
    Points: 24,227, Level: 94
    Level completed: 88%, Points required for next Level: 123
    Awards:
    Master Tagger
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    3,155
    Thanks
    882
    Thanked 544 Times in 492 Posts

    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
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

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

    bugman (09-20-2012)

  10. #8
    TS Contributor
    Points: 6,533, Level: 52
    Level completed: 92%, Points required for next Level: 17
    Lazar's Avatar
    Location
    Sydney
    Posts
    660
    Thanks
    109
    Thanked 164 Times in 149 Posts

    Re: %*% ?

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

  11. #9
    RotParaTon
    Points: 46,105, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,063
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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")
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  12. #10
    RotParaTon
    Points: 46,105, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,063
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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")
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  13. #11
    Super Moderator
    Points: 14,585, Level: 78
    Level completed: 34%, Points required for next Level: 265
    bugman's Avatar
    Posts
    1,492
    Thanks
    88
    Thanked 139 Times in 108 Posts

    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")
    The earth is round: P<0.05

  14. #12
    Bhoot
    Points: 1,275, Level: 19
    Level completed: 75%, Points required for next Level: 25

    Posts
    1,758
    Thanks
    40
    Thanked 124 Times in 106 Posts

    Re: %*% ?

    Quote Originally Posted by bugman View Post
    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. #13
    RotParaTon
    Points: 46,105, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterActivity AwardCommunity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,063
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    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
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

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

    bugman (09-20-2012)

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats