2
a,b和c是列表。列表“a”和“b”具有相同数量的对象和元素。“仅使用列表的第一个元素”R
a<-list(c(3,4,5),c(1,3))
b<-list(c(5,8,7),c(6,8))
c<-list(10,9)
> a
[[1]]
[1] 3 4 5
[[2]]
[1] 1 3
> b
[[1]]
[1] 5 8 7
[[2]]
[1] 6 8
> c
[[1]]
[1] 10
[[2]]
[1] 9
我要计算的总和 “选择(C,A:B)”,在相应的位置。预期的结果是一个列表,它也有两个分别包含3个和2个元素的对象。正如:
[[1]]
[1] X1 X2 X3
[[2]]
[1] Y1 Y2
例如:X1 =总和(选择(10,3:5)),X2 =总和(选择(10,4:8)),Y1 =总和(选择(9,1 :6))......
我尝试使用下面的代码来计算:
mapply(function(a,b,c) sum(choose(c,a:b)), a,b,c)
,但我得到的警告:
[1] 582 465
Warning messages:
1: In a:b : numerical expression has 3 elements: only the first used
2: In a:b : numerical expression has 3 elements: only the first used
3: In a:b : numerical expression has 2 elements: only the first used
4: In a:b : numerical expression has 2 elements: only the first used
只有在每个第一要素对象已被使用,可以哟你帮我弄明白了吗?谢谢!
谢谢!哦,':'是原因,难怪我可以计算''a + b'',但是不能计算':'。 – lightsnail