camouro
camouro

Reputation: 45

R - how to use outer

I have an easy problem but I can't figure it out. I want to create a multiplication table using outer, but I want result to be for example 5x7, instead of 35. I tried:

a <- paste(1:10,collapse="x")
b <- 1:10
outer(a,b)

but it doesn't work, I guess because my "a" isn't a vector. How can I do it?

Upvotes: 1

Views: 55

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101189

I guess what you want to do is like below

a <- sprintf("%sx", 1:10)
b <- 1:10
outer(a, b, paste0)

giving

      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]
 [1,] "1x1"  "1x2"  "1x3"  "1x4"  "1x5"  "1x6"  "1x7"  "1x8"  "1x9"  "1x10"
 [2,] "2x1"  "2x2"  "2x3"  "2x4"  "2x5"  "2x6"  "2x7"  "2x8"  "2x9"  "2x10"
 [3,] "3x1"  "3x2"  "3x3"  "3x4"  "3x5"  "3x6"  "3x7"  "3x8"  "3x9"  "3x10" 
 [4,] "4x1"  "4x2"  "4x3"  "4x4"  "4x5"  "4x6"  "4x7"  "4x8"  "4x9"  "4x10"
 [5,] "5x1"  "5x2"  "5x3"  "5x4"  "5x5"  "5x6"  "5x7"  "5x8"  "5x9"  "5x10"
 [6,] "6x1"  "6x2"  "6x3"  "6x4"  "6x5"  "6x6"  "6x7"  "6x8"  "6x9"  "6x10"
 [7,] "7x1"  "7x2"  "7x3"  "7x4"  "7x5"  "7x6"  "7x7"  "7x8"  "7x9"  "7x10"
 [8,] "8x1"  "8x2"  "8x3"  "8x4"  "8x5"  "8x6"  "8x7"  "8x8"  "8x9"  "8x10"
 [9,] "9x1"  "9x2"  "9x3"  "9x4"  "9x5"  "9x6"  "9x7"  "9x8"  "9x9"  "9x10"
[10,] "10x1" "10x2" "10x3" "10x4" "10x5" "10x6" "10x7" "10x8" "10x9" "10x10"

Upvotes: 0

Sirius
Sirius

Reputation: 5429

Try this:


a <- 1:10
b <- 1:10
outer(a,b,FUN=paste, sep="x" )

Output:


      [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]   [,10]  
 [1,] "1x1"  "1x2"  "1x3"  "1x4"  "1x5"  "1x6"  "1x7"  "1x8"  "1x9"  "1x10" 
 [2,] "2x1"  "2x2"  "2x3"  "2x4"  "2x5"  "2x6"  "2x7"  "2x8"  "2x9"  "2x10" 
 [3,] "3x1"  "3x2"  "3x3"  "3x4"  "3x5"  "3x6"  "3x7"  "3x8"  "3x9"  "3x10" 
 [4,] "4x1"  "4x2"  "4x3"  "4x4"  "4x5"  "4x6"  "4x7"  "4x8"  "4x9"  "4x10" 
 [5,] "5x1"  "5x2"  "5x3"  "5x4"  "5x5"  "5x6"  "5x7"  "5x8"  "5x9"  "5x10" 
 [6,] "6x1"  "6x2"  "6x3"  "6x4"  "6x5"  "6x6"  "6x7"  "6x8"  "6x9"  "6x10" 
 [7,] "7x1"  "7x2"  "7x3"  "7x4"  "7x5"  "7x6"  "7x7"  "7x8"  "7x9"  "7x10" 
 [8,] "8x1"  "8x2"  "8x3"  "8x4"  "8x5"  "8x6"  "8x7"  "8x8"  "8x9"  "8x10" 
 [9,] "9x1"  "9x2"  "9x3"  "9x4"  "9x5"  "9x6"  "9x7"  "9x8"  "9x9"  "9x10" 
[10,] "10x1" "10x2" "10x3" "10x4" "10x5" "10x6" "10x7" "10x8" "10x9" "10x10"

Upvotes: 3

Related Questions