本文介绍了R中向量中的字符相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有矢量:
x = c("a","b","c")
我想创建一个返回以下内容的函数:
I would like to create a function that returns the following:
[1] "a*b" "a*c" "b*c"
我假设我们将使用paste0()函数或使用某种for循环遍历向量中的字符,但是我不确定如何执行此操作.为了清楚起见,我正在寻找一个输出,一次从向量中获取两个字符,并使用某种paste0(...,sep ="*")来获得上面期望的输出.我不想一次将向量中的所有字符相乘,一次只乘两个字符.
I assume we would use the paste0() function or use some sort of for loop to loop through the characters in the vector but I am not fully sure how to go about this. Just to clarify, I am looking for an output that takes two characters from the vector at a time and uses some sort of paste0(...,sep="*") to get the desired output above. I do not want to multiply all the characters in the vector at once, just two characters at a time.
推荐答案
我们可以使用 base R
combn(x, 2, FUN = paste, collapse="*")
#[1] "a*b" "a*c" "b*c"
这篇关于R中向量中的字符相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!