繁体   English   中英

冒号运算符:list

[英]Colon Operator factor:list

在f1和f2是整数的情况下,我知道f1:f2代表从a到b的整数列表,它们之间的差为1。在a和b都是因子的情况下,f1:f2代表一个新因子,每个唯一组合的水平因子水平f1和f2(相互作用),如下所示。 如果我使用f1:x,其中x是一个与因子相反的列表怎么办?

f1<-gl(2,3) #create factor with 2 levels 
f2<-gl(3,2) #create factor with 3 levels 
x<-rnorm(6) #create list with 6 real numbers(sampled from normal)

> f1
[1] 1 1 1 2 2 2
Levels: 1 2

> f2
[1] 1 1 2 2 3 3
Levels: 1 2 3

> x
[1]  0.6705013 -0.2116773 -0.3812724 -0.3687866  1.4878815  0.3095373

f1:f2

[1] 1:1 1:1 1:2 2:2 2:3 2:3
Levels: 1:1 1:2 1:3 2:1 2:2 2:3

f1:x

[1] 1
Warning messages:
1: In f1:x : numerical expression has 6 elements: only the first used
2: In f1:x : numerical expression has 6 elements: only the first used

如您所见,我得到带有警告消息的输出1。 怎么了?

一些实验表明, R将冒号两边的表达式强制为数字,采用每个表达式的第一个元素,然后使用冒号seq(from, to)评估表达式。 例如

> factor(c("C","B","A")):(7:8)
[1] 3 4 5 6 7

在您的示例中, 1: 0.6705013计算结果为1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM