首页 > 解决方案 > Using gsub to seperate 1 and 11 from Q1 and Q1Q1 to Q1 and Q2

问题描述

I am trying to merge the xy vector with z vector according to the interaction terms that are in xy to the terms in z. Then change the final code to Q1, Q2...Q1*Q2. I have a problem when using

I have two vectors that need to match as vector xy:

x<-c(1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,6,6,9,10,16,21)
y<-c(1,2,3,5,6,8,18,1,2,5,6,7,8,12,15,16,11,17,18,19,20,21)

I want any of 2*6,or 6*11 to be added to the vector z for any case of z because according to vector xy there are interactions between 2,6,11 according to vector z

      xy=paste0(x,"*",y,collapse=",")
      xy
    # [1] #"1*1,1*2,1*3,1*5,1*6,1*8,1*18,2*1,2*2,2*5,2*6,2*7,3*8,3*12,3*15,4*16,6*11,6*17,#9*18,10*19,16*20,21*21"
     z<-c(1,6,11)
     z
    #[1]  1  6 11

I want a fourth vector to have all interactions of z from vector xy and combined into a new vector xyz

     xyz<-print("1+6+11+1*6+6*11")
    #[1] "1+6+11+1*6+6*11"
     xyz
    #[1] "1+6+11+2*6+2*11+6*11"

then for each varaible 1,6,11 convert to Q1,Q2,Q3 So the end product looks like...

     xyz<-print("Q1+Q2+Q3+Q1*Q2+Q2*Q3")
    #[1]
  #End result:
 #"Q1+Q2+Q3+Q1*Q2+Q2*Q3"

I have this answer. For the gsub function I cant seem to get it to register the 1 and 11 apart. It keeps going for Q1 and Q1Q1.

xyz <- as.character(z)

for(i in 1:length(x)){
  if(x[i] %in% z & y[i] %in% z x[i] != y[i]){
    xyz <- c(xyz, (paste0(x[i], "*", y[i])))
  }
}
print(xyz)
xyz <- paste0(xyz, collapse = "+")
print(xyz)

z_map <- c("Q1","Q2","Q3","Q4")
for(i in 1:length(z)){
  xyz <- gsub(z[i],z_map[i], xyz)
  }
xyz<-paste0('y~',xyz)
print(xyz)

标签: r

解决方案


您应该在 中使用边界条件gsub,如下所示:

 xyz <- gsub(paste0("\\b",z[i],"\\b"),z_map[i], xyz)

边界条件将帮助正则表达式能够区分 1 和 11。否则,它会将 11(十一)的实例视为 1 和 1 的两个实例。

输出:

> print(xyz)
[1] "y~Q1+Q2+Q3+Q1*Q2+Q2*Q3"

推荐阅读