[R] help in R

Petr Pikal petr.pikal at precheza.cz
Thu Apr 27 09:06:40 CEST 2006


Hi

I did not go through your code completely bud from error message it 
seems to me that this command is wrong

> 
matrix(unlist(apply(phen.dat[,phen.name],2,test.allsnp)),byrow=T,ncol=
> 15) Error in apply(phen.dat[, phen.name], 2, test.allsnp) : 
>         dim(X) must have a positive length

apply shall be used on data.frames or matrices

> apply(dfr,2,mean)
        x         y 
 0.451210 -1.238681 
> apply(dfr[,"x"],2,mean)
Error in apply(dfr[, "x"], 2, mean) : dim(X) must have a positive 
length
>

from help page

If X is not an array but has a dimension attribute, apply attempts to 
coerce it to an array via as.matrix if it is two-dimensional (e.g., 
data frames) or via as.array. 

see dim(dfr) and dim(dfr[,"x"])

HTH
Petr



On 26 Apr 2006 at 12:46, Anamika Chaudhuri wrote:

Date sent:      	Wed, 26 Apr 2006 12:46:22 -0700 (PDT)
From:           	Anamika Chaudhuri <canamika16 at yahoo.com>
To:             	r-help at stat.math.ethz.ch
Subject:        	[R] help in R

> Hi,
> 
>   I cant understand where I am going wrong.Below is my code.I would
>   really appreciate your help.
> 
>   Thanks.
> 
> 
>   > genfile<-read.table("c:/tina/phd/bs871/hw/genfile.txt",skip=1) > >
> #read in SNP data > snp.dat <- as.matrix(genfile) > snp.name <-
> scan("c:/tina/phd/bs871/hw/genfile.txt",nline=1,what="character") Read
> 100 items > n.snp <- length(snp.name) > n.id <- 1 #number of fields
> for ids, sex and affection status > > ###form gntp using the two
> alleles of each SNP > allele1 <- snp.dat[,seq(1,2*n.snp,2)+n.id ] >
> allele2 <- snp.dat[,seq(2,2*n.snp,2)+n.id ] > temp <-
> matrix(paste(allele1,allele2,sep="|"),dim(allele1)) > temp <-
> data.frame(temp) > convt <- function(x) x <-
> factor(as.character(x),exclude="0|0") > > gntp <-
> as.data.frame(lapply(temp,convt)) > > ###create new snp data with ids
> and gntp only > names(gntp) <- snp.name > ids <-
> as.data.frame(snp.dat[,1:n.id]) > names(ids) <- c("fid") > > snp.dat
> <- cbind(ids,gntp) > > > > #read in phenotype data >
> #------------------------------------------------------ > phen.dat <-
> read.table("c:/tina/phd/bs871/fram.csv",header=T,sep=",",na.string='')
> > phen.name <-
> scan("c:/tina/phd/bs871/fram.csv",nline=1,what="character") Read 1
> item > n.phen <- length(phen.name) > > names(phen.dat) <- c("id",
> phen.name) > > test.allsnp <- function(phen) + +
> apply(phen.dat[,snp.name],glm.reg,phen=phen) > ##Above:since snp gntp
> data are defined as factor, use lapply instead of apply(matrix only) >
> > #Below; apply() results in three dimensional list (phen,snp) > #use
> unlist to convert the list into a vector, then use matrix to convert
> the vector > #into a matrix of 15 columns, each row for a (phen,snp)
> combination with byrow=T  > > final <-
> matrix(unlist(apply(phen.dat[,phen.name],2,test.allsnp)),byrow=T,ncol=
> 15) Error in apply(phen.dat[, phen.name], 2, test.allsnp) : 
>         dim(X) must have a positive length
> > 
> > 
> 
> 
> 
> ---------------------------------
> 
>  [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz




More information about the R-help mailing list