Print a multiplication table with minimal code

后端 未结 2 732
再見小時候
再見小時候 2020-12-20 07:42

In R, what is the fastest way(shortest code) to print multiplication table? The functions seq rep and the bind functions help, but I\'m looking for the shortest line(s) of c

2条回答
  •  执念已碎
    2020-12-20 08:11

    tbl <- outer(1:6, 1:12, "*")
    rownames(tbl) <- paste(1:6, "'s", sep="")
    tbl
    

    You could make slightly more compact by using paste0(1:6, "'s")

    This seems a slight improvement:

    > v<-setNames(1:6, paste0(1:6, "\'s"))
    > v %o% v
        1's 2's 3's 4's 5's 6's
    1's   1   2   3   4   5   6
    2's   2   4   6   8  10  12
    3's   3   6   9  12  15  18
    4's   4   8  12  16  20  24
    5's   5  10  15  20  25  30
    6's   6  12  18  24  30  36
    

提交回复
热议问题