0

我有几句话:

shapes<- c("Square", "Triangle","Octagon","Hexagon")

我想成对排列它们:

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"

我想计算成对中字母的每个分组,例如第一对是“6”代表“方形”,“8”代表“三角形”,第一对是“14”,依此类推。

4

3 回答 3

5

使用applynchar

> apply(shapescount, 2, function(x) sum(nchar(x)))
[1] 14 13 13 15 15 14

由于combn也有FUN参数,您可以一口气完成所有操作:

> combn(shapes, 2, FUN=function(x) sum(nchar(x)))
[1] 14 13 13 15 15 14
于 2013-11-13T15:50:44.387 回答
4

单程:

colSums(combn(shapes, 2, FUN=nchar))
# [1] 14 13 13 15 15 14

基准:

shapes <- rep(c("Square", "Triangle","Octagon","Hexagon"), 50)
matthew <- function() colSums(combn(shapes, 2, FUN=nchar))
ananda <- function() combn(shapes, 2, FUN=function(x) sum(nchar(x)))
tstenner <- function() {
    shapelengths <- nchar(shapes)
    colSums(combn(shapelengths, 2))
}
microbenchmark(matthew(), ananda(), tstenner())
# Unit: milliseconds
#        expr   min    lq median    uq    max neval
#   matthew() 53.27 55.84  56.37 57.04  98.83   100
#    ananda() 74.74 78.42  79.88 80.60 123.29   100
#  tstenner() 29.27 31.03  31.52 33.44  76.01   100
于 2013-11-13T15:51:09.947 回答
4

您可能正在寻找nchar.

为了使事情更快,不要计算字符串的所有排列,而是字符串长度:

shapes<- c("Square", "Triangle","Octagon","Hexagon")
shapelengths <- nchar(shapes)
colSums(combn(shapelengths, 2))

如果您正在寻找一个表达式,只需将调用内联nchar()

colSums(combn(nchar(shapes), 2))
于 2013-11-13T15:52:18.800 回答