[R] Error in inherits(x, "data.frame") : subscript out of bounds

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Mar 13 19:17:54 CET 2010



On 13.03.2010 17:25, kloytyno at mappi.helsinki.fi wrote:
> Lainaus "Uwe Ligges" <ligges at statistik.tu-dortmund.de>:
>
>>
>>
>> On 05.03.2010 15:24, kloytyno at mappi.helsinki.fi wrote:
>>> Hi,
>>> I have a list p with different size dataframes and length of over 8000.
>>> I'm trying to
>>> calculate correlations between the rows of dataframes of this list and
>>> columns of another
>>> dataset (type data.frame also) so that first column is correlated with
>>> all the rows in
>>> the list dataframe. Some information from another dataset is also
>>> included to the final
>>> output (all.corrs). This worked a couple of weeks ago when I wrote it
>>> but now it seems
>>> not to, and gives an error message:
>>>
>>> Error in inherits(x, "data.frame") : subscript out of bounds
>>> In addition: There were 50 or more warnings (use warnings() to see the
>>> first 50)
>>> warnings()
>>> Warning messages: 1: In corrs[j] <- cbind(expressions[j, 1:5],
>>> SNP.expr.cor) :
>>> number of items to replace is not a multiple of replacement length
>>>
>>> which indicates that the problem is with getting correlation and other
>>> information into
>>> corrs. cbind(expressions[j,1:5], SNP.expr.cor) is type data.frame.
>>> Changing corrs into
>>> matrix, dataframe, list or any other type has not helped.
>>>
>>> I've updated R from 2.9.0 to the recent version in between. Would anyone
>>> have a solution
>>> for this problem? I very much appreciate all help.
>>>
>>> SNP.expr.cor<-NULL
>>> all.corrs<-NULL
>>> corrs<-NULL
>>>
>>> for(i in 1:length(p)){
>>> dim.exp<-dim(p[[i]])
>>> expressions<-p[[i]]
>>> expressions.m<-as.matrix(expressions[,6:48])
>>> for(j in
>>> 1:dim.exp[1]){
>>> SNP.expr.cor<-cor(genotypes[,i],expressions.m[j,],use="na.or.complete")
>>> corrs[j]<-cbind(expressions[j,1:5], SNP.expr.cor)
>>> }
>>> all.corrs[i]<-list(cbind(map[i,1:6], corrs))
>>> }
>>>
>>>
>>> BR
>>> Katja
>>>
>>
>>
>> Your example is not reproducible, since we do not have the data. What
>> I guess is that you need to make your objects lists in advance. I
>> cannot try out, but maybe the following works better right away
>>
>> all.corrs <- vector(mode = "list", length = length(p))
>> for(i in seq(along = p)){
>> dim.exp <- dim(p[[i]])
>> corrs <- vector(mode = "list", length = dim.exp[1])
>> expressions <- p[[i]]
>> expressions.m <- as.matrix(expressions[,6:48])
>> for(j in 1:dim.exp[1]){
>> SNP.expr.cor <- cor(genotypes[, i], expressions.m[j, ], use =
>> "na.or.complete")
>> corrs[[j]] <- cbind(expressions[j, 1:5], SNP.expr.cor)
>> }
>> all.corrs[[i]] <- list(cbind(map[i, 1:6]), corrs)
>> }
>>
>>
>> Best,
>> Uwe Ligges
>>
>>
>
> Hi, sorry about that, here's a new try:
>
> corrs<-NULL #I've tried everything from vector to matrix to data.frame
> to list
> a<-rnorm(30)
> b<-cbind(a,a,a,a,a)
> e<-as.matrix(t(b))
> c<-c("one", "two", "three", "four", "five")
> d<-c("one", "two", "three", "four", "five")
> c<-rbind(c,c,c,c,c)
>
> k<-dim(dat)

Well, dat is still unknown to us.


> for(j in 1:k[1]){
> dat.cor<-cor(b[,i],e[j,],use="na.or.complete")

i is unknown to us now. Your code is still *not* reproducible (note that 
I forgot what happened a week ago). Please post your whole reproducible 
code! We do not remember all things for weeks....


> corrs[j]<-cbind(c[j,1:5], dat.cor)

That cannnot work: corrs is a vector (given you said nothing else and 
given your indexing). You cannot assign cbind(c[j,1:5], dat.cor) which 
is a matrix of 6 columns at least, into a single element of a vector 
corrs[j].

If your dat.cor and you c are numeric values (rather than more complex 
objects that I believe they are intended to be from your former 
message), then you caould assign into rows of a matrix corrs by 
initialiszing it before and assigning rowwise by

corrs[j,] <- cbind(c[j,1:5], dat.cor)


But since you do not know this basic assigment trick, I really doubt you 
know how complex your objects really are...
It might make sense to learn a bit more of R and reading some R 
introducing material as well as asking a local expert.

Best,
Uwe Ligges




> }
>
> The trouble seems to be in the inner loop. I'd love to have it as
> data.frame or matrix, not as a list. I think there has been similar
> problems presented here before but I haven't been able to find a working
> solution yet.
>
> BR
> Katja Löytynoja
>
>
>



More information about the R-help mailing list