Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何选择特定的命名列_R - Fatal编程技术网

R 如何选择特定的命名列

R 如何选择特定的命名列,r,R,我正在尝试自动分析R中的一些数据表 我有这样的桌子: θ1θ2θ3θ4西格玛1西格玛2ω1ω2ω3ω4 但是,T和O的数量因分析而异。所以我想让代码自动计算出有多少θ,sigmas和omegas,然后把它们放在各自的矩阵中,分别进行分析。我在想某种正则表达式?但我更熟悉斯塔塔的命令 我想我已经很接近了,但我的代码如下: mcmcChain <- as.matrix(MCMC) #switch MCMC object to matrix mcmcNames <- colnames(mc

我正在尝试自动分析R中的一些数据表

我有这样的桌子:

θ1θ2θ3θ4西格玛1西格玛2ω1ω2ω3ω4

但是,T和O的数量因分析而异。所以我想让代码自动计算出有多少θ,sigmas和omegas,然后把它们放在各自的矩阵中,分别进行分析。我在想某种正则表达式?但我更熟悉斯塔塔的命令

我想我已经很接近了,但我的代码如下:

mcmcChain <- as.matrix(MCMC) #switch MCMC object to matrix
mcmcNames <- colnames(mcmcChain) #make vector with column names

mcmcNames
[1] "THETA1"    "THETA2"    "THETA3"    "THETA4"    "THETA5"    "SIGMA.1.1"
[7] "SIGMA.2.1" "SIGMA.2.2" "OMEGA.1.1" "OMEGA.2.1" "OMEGA.2.2" "OMEGA.3.1"
[13] "OMEGA.3.2" "OMEGA.3.3" "OMEGA.4.1" "OMEGA.4.2" "OMEGA.4.3" "OMEGA.4.4"
[19] "OMEGA.5.1" "OMEGA.5.2" "OMEGA.5.3" "OMEGA.5.4" "OMEGA.5.5" "MCMCOBJ"  

nVar <- length(mcmcNames) #number of variables
for (i in 1:nVar) {
  thetaNames <- mcmcNames[i] # !!some way to identify only theta's!!
}
nTheta <- length(thetaNames) #n theta variables
thetaSamp <- NULL
for (j in 1:nTheta) { #cbind all thetas to one column
thetaSamp <- cbind(thetaSamp, 
                mcmcChain[,paste("THETA",j,sep="") ] )
}

mcmcChain
thetadata这将隔离名称中带有“THETA”的所有列,并将它们存储在自己的称为“THETA”的矩阵中:


thetas为什么不使用
grep
来识别相关列?谢谢。这很有效:thetaSamp
thetadata<-data.frame(mcmcChain[,grep("^THETA",colnames(mcmcChain))]
thetas <- mcmcChain[, c(grep("THETA", colnames(mcmcChain)))]