R检查数据框中是否缺少列

时间:2012-05-09 16:09:50

标签: r

我拥有2032行和130列的庞大数据帧,我正在遍历data_frame的每一行来执行某些操作。 这是我经过几次迭代后得到的结果:

Error in `[.data.frame`(a, i) : undefined columns selected

我知道如果选择了未定义的列,就会出现此错误,即数据框中是否存在“缺失”列。现在R中是否有快速检查哪个列丢失?

这是我的代码。我试图在两个时间序列之间绘制ccf

a = read.table("a.data",row.names=1, header=T)
b = read.table("b.data",row.names=1,header=T) 
fileConn = file("max_lag.data")
data='word max_correlation sameword lag_atwhich_maxcorr'
write(data,file="max_lag.data",ncolumns=2, append=TRUE)
for (i in 1:dim(a)[1]) #iterate through the rows of a. dim(a) returns a matrix containing number of rows and columns
{
print (c(i,row.names(a[i])))
tweet = c(t(a[i,])) #convert 1st row to a vector with no dimensions. Needed for ccf to work
query = c(t(b[i,])) 
a_ts = ts(tweet)#here freq=1, data have been collected at regular interval of (timewindow=11)minutes.
b_ts = ts(query)
ccfvalues=ccf(a_ts,b_ts,30, main=row.names(a)[i])
dev.copy2pdf(file=paste(row.names(a)[i],".pdf"))
#Find the lag where correlation is maximum 
cor = ccfvalues$acf[,,1]
lag = ccfvalues$lag[,,1]
plot(lag,cor,type="o")
res = data.frame(cor,lag)
res_max = res[which(res$cor == max(res$cor)),]  #find indices of multiple maxs in a vector
data=paste(row.names(a)[i],res_max)
write(data,file="max_lag.data",ncolumns=2, append=TRUE)
}

我没有引用数据框的列,只是行。但我仍然得到这个错误,这是令人困惑的。

0 个答案:

没有答案