简体   繁体   English

计算R中单词的所有排列中的字母数量

[英]Counting the amount of letters in all permutations of words in R

I have some words: 我有几句话:

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

I want to arrange them in pairs: 我想成对安排它们:

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"

I want to count each of the groupings of the letters in the pairs, for instance first pair is "6" for "Square" and "8" for "Triangle" giving me "14" for the first pair, and so on. 我想对成对字母中的每个字母进行分组,例如,第一对是“ Square”的“ 6”,“三角”是“ 8”的第一对是“ 14”,依此类推。

Use apply and nchar : 使用applynchar

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

Since combn also has a FUN argument, you can do it all in one go: 由于combn也具有FUN参数,因此您可以一次完成所有操作:

> combn(shapes, 2, FUN=function(x) sum(nchar(x)))
[1] 14 13 13 15 15 14

One way: 单程:

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

Benchmarks: 基准测试:

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

You're likely looking for nchar . 您可能正在寻找nchar

To make things faster, don't compute all permutations of strings but of string lengths: 为了使事情更快,请不要计算字符串的所有排列,而是计算字符串的长度:

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

If you're looking for one expression, simply inline the call to nchar() : 如果要查找一个表达式,只需内联对nchar()的调用:

colSums(combn(nchar(shapes), 2))

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

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