我试图根据xy中的交互项将zy向量与z向量合并到z中的项。然后将最终代码更改为Q1,Q2 ...... Q1 * Q2。我在使用
时遇到问题我有两个向量需要匹配为向量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)
对于z的任何情况,我希望将2 * 6或6 * 11中的任何一个添加到向量z,因为根据向量xy,根据向量z在2,6,11之间存在相互作用
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
我希望第四个向量能够从向量xy中获得z的所有相互作用并组合成一个新的向量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"
然后每个可变的1,6,11转换为Q1,Q2,Q3所以最终产品看起来像......
xyz<-print("Q1+Q2+Q3+Q1*Q2+Q2*Q3")
#[1]
#End result:
#"Q1+Q2+Q3+Q1*Q2+Q2*Q3"
我有这个答案。对于gsub函数,我似乎无法让它分别注册1和11。它一直持续到Q1和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)
答案 0 :(得分:0)
您应该使用gsub
中的边界条件,如下所示:
xyz <- gsub(paste0("\\b",z[i],"\\b"),z_map[i], xyz)
边界条件将帮助正则表达式区分1和11.否则,它将11(11)的实例视为1和1的两个实例。
<强>输出:强>
> print(xyz)
[1] "y~Q1+Q2+Q3+Q1*Q2+Q2*Q3"
答案 1 :(得分:0)
我认为你可以完全跳过for循环。完整问题的可能解决方案:
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem: unable to get local issuer
certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
Failed to install 'cordova-plugin-accountkit': Error: pod: Command failed with exit code 1 Error output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
at ChildProcess.whenDone (/Applications/apps/v2/accountkit/platforms/ios/cordova/node_modules/cordova-common/src/superspawn.js:169:23)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:925:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
(node:53637) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: pod: Command failed with exit code 1 Error output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
(node:53637) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
给出:
i <- x %in% z & y %in% z & x != y q <- paste0('Q',1:4) x1 <- q[match(x[i], z)] y1 <- q[match(y[i], z)] xyz <- paste0('y~', paste0(c(union(x1, y1), paste(x1, y1, sep = '*')), collapse = '+'))
使用过的数据:
> xyz
[1] "y~Q1+Q2+Q3+Q1*Q2+Q2*Q3"