From: Liaw, Andy <andy_liaw_at_merck.com>

Date: Tue 17 May 2005 - 11:55:00 EST

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 Received on Tue May 17 11:58:40 2005

Date: Tue 17 May 2005 - 11:55:00 EST

If you have the data in files in such format, with one entry per line, you
can do something like:

dat <- matrix(scan("fileWithData", sep=":"), ncol=2, byrow=TRUE)

Then the first column of dat would be the indices, and the second column would be the values.

Andy

*> From: Luke
**>
*

> Hi Gabor, thanks a lot. I understand it now. But Andrew's method is

*> easier for me to understand. Can I extend my question? I have a data
**> file, every line has such format:
**> 2:102 5:85 ...
**> The number before colon is data entry index, the number after colon is
**> data entry value, and other data entries are zero. I use scan to read
**> them in R and need to separate the index and value to different
**> vectors. For example:
**>
**> > foo
**> [1] "2:102" "5:85"
**> > foo2 <- strsplit(foo, split=":")
**> > foo2
**> [[1]]
**> [1] "2" "102"
**>
**> [[2]]
**> [1] "5" "85"
**>
**> > myIndex <- as.numeric(unlist(lapply(foo2, function(x) x[1])))
**> or
**> > myIndex <- as.numeric(foo2, "[", 1)
**> > myIndex
**> [1] "2" "5"
**>
**> Is this the simplest way to get the index or value vector?
**>
**> -Luke
**>
**> On 5/16/05, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
**> > Its the indexing function written in ordinary function
**> form. That is,
**> > foo[1:2] can be written as "["(foo, 1:2)
**> >
**> > On 5/16/05, Luke <jyzz88@gmail.com> wrote:
**> > > Yes, it works. Althought I can understand the help page
**> of sapply, I
**> > > don't know why it works. What is "["?
**> > >
**> > > -Luke
**> > >
**> > > On 5/16/05, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
**> > > > On 5/16/05, Luke <jyzz88@gmail.com> wrote:
**> > > > > I have a simple question, but I couldn't find the
**> answer in R manuals.
**> > > > >
**> > > > > Assume I have a list:
**> > > > > > foo <- list()
**> > > > > > foo[[1]] <- c(1, 2, 3)
**> > > > > > foo[[2]] <- c(11,22,33)
**> > > > > > foo[[3]] <- c(111,222,333)
**> > > > > > foo
**> > > > > [[1]]
**> > > > > [1] 1 2 3
**> > > > >
**> > > > > [[2]]
**> > > > > [1] 11 22 33
**> > > > >
**> > > > > [[3]]
**> > > > > [1] 111 222 333
**> > > > >
**> > > > > How to use list index to get a vector of, say, the
**> first elements of
**> > > > > list elements?
**> > > > > That is, how to get a vector c(1, 11, 111) from foo?
**> > > > >
**> > > > > foo[[]][1] doesn't work.
**> > > > >
**> > > >
**> > > > Try this:
**> > > >
**> > > > sapply(foo, "[", 1)
**> > > >
**> > >
**> >
**>
**> ______________________________________________
**> 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@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 Received on Tue May 17 11:58:40 2005

*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:31:46 EST
*