Re: [R] help in R

2006-04-27 Thread Petr Pikal
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 <[EMAIL PROTECTED]>
To:     r-help@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@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
[EMAIL PROTECTED]

__
R-help@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


Re: [R] help in R

2006-04-26 Thread Anupam Tyagi
Anamika Chaudhuri  yahoo.com> writes:

> 
> Hi,
> 
>   I cant understand where I am going wrong.Below is my code.I would really 
> appreciate your help.

Dear Anamika, to improve your chances to get a response from this list, and to
help in people responde quickly and accurately to you, please run through your
code line-by-line and identify the line/lines where the problem is: even better
if you can broadly identify what the problem is. It will be difficult for
someone to replicate your programming problem, without having your dataset, and
knowing it's contents. Anupam.

__
R-help@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


[R] help in R

2006-04-26 Thread Anamika Chaudhuri
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@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


Re: [R] help in R calling C function

2004-06-27 Thread Uwe Ligges
Lei Liu wrote:
Hi there,
I want to call a C function in R. I have some experience on it, but this 
time I need to call another C function in the "main" C function. As a 
simple example, I use the following C code:

#include 
#include 
#include 
#include 
void main(double *alpha)
1. don't call it "main".
2. alpha must not be a pointer.

{
double test();
double beta;
beta= *alpha *2 + test(*alpha);
}
double test(double *alpha)
{
double value;
value = *alpha *3;
return(value);
}
I used "gcc -c main.c" and "Rcmd SHLIB main.o" and I got the main.dll 
file. In R I use the following code to call the C function "main":
3. "Rcmd SHLIB main" is sufficient.

dyn.load("c:/Program Files/R/rw1060/bin/main.dll")
4. Why do you compile in R's /bin directory? That's not necessary, it's 
rather confusing.

Uwe Ligges
a=4.2
b=.C('main', as.double(a))
But the R system crashed. I know I can only define the function type in 
C as "void" to be called by R. But what if I want to call another C 
function in the "main" C function? Thank you for your help!

Sincerely,
Lei Liu
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


[R] help in R calling C function

2004-06-27 Thread Lei Liu
Hi there,
I want to call a C function in R. I have some experience on it, but this 
time I need to call another C function in the "main" C function. As a 
simple example, I use the following C code:

#include 
#include 
#include 
#include 
void main(double *alpha)
{
double test();
double beta;
beta= *alpha *2 + test(*alpha);
}
double test(double *alpha)
{
double value;
value = *alpha *3;
return(value);
}
I used "gcc -c main.c" and "Rcmd SHLIB main.o" and I got the main.dll file. 
In R I use the following code to call the C function "main":

dyn.load("c:/Program Files/R/rw1060/bin/main.dll")
a=4.2
b=.C('main', as.double(a))
But the R system crashed. I know I can only define the function type in C 
as "void" to be called by R. But what if I want to call another C function 
in the "main" C function? Thank you for your help!

Sincerely,
Lei Liu
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html