简体   繁体   English

将字符向量的每个元素与第二个向量r的所有元素连接起来

[英]concatenate each element of character vector with all elements of a second vector r

I am trying to concatenate two character vectors in a way that the following output is produced 我试图以产生以下输出的方式连接两个字符向量

aggmodes<-c("17x8","17x7x8","17x28x8")
listdata<-c("Motion.Age","res.Context.Only")

The output should be like this 输出应该是这样的

"Motion.Age,17x8"  
"Motion.Age,17x7x8"
"Motion.Age,17x28x8"
"res.Context.Only,17x8"
"res.Context.Only,17x7x8"
"res.Context.Only,17x28x8"

I have written following code: 我写了以下代码:

c<-as.vector(sapply(1:length(listdata), function(i){
sapply(1:length(aggmodes),function(j){paste(aggmodes,listdata)})
}))

but it gives me an a 10 dimensional vector. 但这给了我一个10维向量 I am sorry if it is a duplicate, but i couldnot find a correct answer for solving my problem 很抱歉,如果它是重复的,但是我找不到解决问题的正确答案

c(sapply(listdata,paste,aggmodes,sep=","))
# [1] "Motion.Age,17x8"          "Motion.Age,17x7x8"        "Motion.Age,17x28x8"      
# [4] "res.Context.Only,17x8"    "res.Context.Only,17x7x8"  "res.Context.Only,17x28x

We paste each element of listdata to all of aggmodes with sapply, and then unwrap it all. 我们使用listdataaggmodes每个元素粘贴到所有aggmodes中,然后将其全部解包。

Your code is suboptimal because you don't leverage the fact paste is vectorized, however it can work with a slight modification: 您的代码是次优的,因为您没有利用paste矢量化的事实,但是可以稍作修改就可以工作:

as.vector(sapply(1:length(listdata), function(i){
  sapply(1:length(aggmodes),function(j){paste(aggmodes[j],listdata[i])})
}))
as.character(outer(listdata, aggmodes, paste, sep = ","))

outer takes three arguments: x , y , and FUN . outer采用三个参数: xyFUN It applies FUN to all elements of x and y - in this case, pasting them together. 它将FUN应用于xy所有元素-在这种情况下,将它们粘贴在一起。 Since outer returns a matrix, wrap it in as.character to return a vector! 由于outer返回一个矩阵,敷在as.character返回一个向量!

暂无
暂无

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

相关问题 将向量中的所有元素乘以R中for循环中的每个元素 - Multiplying all elements of a vector by each element in a for loop in R 为 R 中的字符向量中的每个元素匹配 substring - Match a substring for each element in a character vector in R 如何将字符串连接到可能的空字符向量的每个元素? - How to concatenate a string to each element of a possible empty character vector? 根据R中向量中每个列表的共同第一个元素,组合列表向量中的所有元素 - Combining all elements in a vector of lists based on the common first element of each list in the vector in R 避免在R中使用lapply(),并找到向量B的所有元素,满足向量A的每个元素的条件 - Avoiding lapply() in R, and finding all elements of Vector B that meet a condition of for each element of Vector A 将向量的每个元素与另一个向量的每个元素连接起来 - Concatenate each element of a vector with each element of another vector 将向量的所有元素(具有9个元素)除以另一个向量(具有100个元素)的第一,第二,…百个元素 - Divide all element of a vector (with 9 elements) by first, second, …hundredth element from another vector (with 100 elements) 在 R 中提取向量中的字符元素 - Extract character elements in a vector in R 在 R 中提取向量中的字符元素 - Extract character elements in a vector in R 根据元素名称连接字符向量中的字符串 - concatenate strings in a character vector based on element names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM