Rho*_*odo 0 r permutation counting
我有一些话:
shapes<- c("Square", "Triangle","Octagon","Hexagon")
Run Code Online (Sandbox Code Playgroud)
我想成对安排它们:
shapescount<-combn(shapes, 2)
shapescount
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "Square" "Square" "Square" "Triangle" "Triangle" "Octagon"
[2,] "Triangle" "Octagon" "Hexagon" "Octagon" "Hexagon" "Hexagon"
Run Code Online (Sandbox Code Playgroud)
我想计算成对中每个字母的分组,例如第一对对于"Square"为"6"而对于"Triangle"为"8"对于第一对给出"14",依此类推.
使用apply和nchar:
> apply(shapescount, 2, function(x) sum(nchar(x)))
[1] 14 13 13 15 15 14
Run Code Online (Sandbox Code Playgroud)
既然combn也有一个FUN参数,你可以一气呵成:
> combn(shapes, 2, FUN=function(x) sum(nchar(x)))
[1] 14 13 13 15 15 14
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |