[R] problems with character objects and calls to list()

Neil Shephard nshephard at gmail.com
Mon Jul 23 16:50:39 CEST 2007


Hi All,

I have a problem trying to get a set of columns recognised as a list
and can't work out how to do it despite trawling through the mailing
list archives, and docs.

A short example...

to.convert <- NULL
n <- 6
for(x in 1:n){
  to.convert <- paste(to.convert, paste((2 * x) -1, (2 * x), sep=":"), sep=",")
}
to.convert <- gsub("^,", "", to.convert)
typeof(to.convert)
[1] "character"
## This is the problem....
list(to.convert)
[[1]]
[1] "1:2,3:4,5:6"

Really I'd like the call to list() to behave as though the text had
been entered directly so that you get....

> list(1:2, 3:4, 5:6)
[[1]]
[1] 1 2

[[2]]
[1] 3 4

[[3]]
[1] 5 6


But how do I get the quote's that surround to.convert removed, given
that it is an object of type character?

Thanks in advance,

Neil

Background
========

This is a refined example from a larger problem that I'm working on.
Given a data frame of pairs of columns containing genotype data I
would like to convert them to genotype objects using makeGenotypes.
The number of loci (i.e. ncol() / 2) is variable, so I like to be able
to determine how many loci there are and then convert all of them.

Thus an example data set would be...

<-----Start test.dat------>
ID,rs1.1,rs1.2,rs2.1,rs2.2,rs3.1,rs3.2,rs4.1,rs4.2,rs5.1,rs5.2,rs6.1,rs6.2
A0001,1,1,1,2,1,1,2,2,1,2,1,1
A0002,1,2,2,1,1,2,2,1,1,2,1,2
A0003,1,2,2,1,1,1,,,1,2,2,2
A0004,2,2,1,1,2,2,2,2,1,1,2,2
<-----End test.dat----->

I'd then like to read in and convert as follows...

genotype <-read.csv("test.dat")
genotype <- genotype[,-1]    ## Removes ID column
n.loci <- ncol(genotype) / 2  ## Calcualtes how many loci
## Build a list of the pairs of columns
to.convert <-NULL
for(x in 1:n.loci){
  to.convert <- paste(to.convert, paste((2 * x) -1, (2 * x), sep=":"), sep=",")
}
to.convert <- gsub("^,", "", to.convert)
## Then convert to genotype object, but get errors...
genotype2 <- makeGenotypes(genotype, convert=list(to.convert))
Error in makeGenotypes(genotype, convert = list(to.convert)) :
	When convert is a list, each element must be a 2-vector.
## I've also tried giving the column names but the data disappears
genotype2 <- makeGenotypes(genotype, convert=c(colnames(genotype))
  rs1.1 rs1.2 rs2.1 rs2.2 rs3.1 rs3.2 rs4.1 rs4.2 rs5.1 rs5.2 rs6.1 rs6.2
1  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
2  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
3  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
4  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>  <NA>
## What I would like to happen is demonstrated by...
genotype2 <- makeGenotypes(genotype, convert=c(1:2,3:4,5:6,7:8,9:10,11:12))
genotype2
  rs1.1/rs1.2 rs2.1/rs2.2 rs3.1/rs3.2 rs4.1/rs4.2 rs5.1/rs5.2 rs6.1/rs6.2
1         1/1         1/2         1/1         2/2         1/2         1/1
2         2/1         1/2         1/2         2/1         1/2         2/1
3         2/1         1/2         1/1        <NA>         1/2         2/2
4         2/2         1/1         2/2         2/2         1/1         2/2

In the above the data has been converted to genotypes.

-- 
"In mathematics you don't understand things. You just get used to
them."  - Johann von Neumann

Email - nshephard at gmail.com / n.shephard at sheffield.ac.uk
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/



More information about the R-help mailing list