Meaning of %o% in R

谁说我不能喝 提交于 2019-11-30 17:41:49

问题


I encountered the following in R:

x=x+y%o%c(1.5,1.5)

I am wondering what is the meaning of %o% here. I tried googling but didn't have much luck


回答1:


There are a number of shortcuts in R that use the %...% notation. %o% is the outer product of arrays

> 1:3 %o% 1:3
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9

There are a number of others, my most used is %in%:

3 %in% c(1,2,3,4) #TRUE
5 %in% c(1,2,3,4) #FALSE
3.4 %in% c(1,2,3,4) #FALSE

There are a few others, I don't know them all off the top of my head. But when you encounter them, you can check for documentation by using backticks around the %o% like ?`%o%`, or quotes ?'%o%' (or ?"%o%").

They are obviously difficult to google because of the percent sign.




回答2:


An intuition. %o% is outer product, look at the example, it returns a matrix.
a[1] * b is the first row of matrix,
a[2] * b is the second row of the matrix.

> a = c(1, 2, 3)
> b = c(0, 2, 4)
> a %o% b
     [,1] [,2] [,3]
[1,]    0    2    4
[2,]    0    4    8
[3,]    0    6   12


来源:https://stackoverflow.com/questions/28993483/meaning-of-o-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!