[R] Large matrix into a vector
A Ezhil
ezhil02 at yahoo.com
Thu Mar 29 11:12:50 CEST 2007
Hi All,
Thank you very much for all your suggestions. It's a
great learning for me. All the three suggested
solutions seem working. I don't know what 'side
effects' that you were talking about. To summarize the
responses:
> s <- read.table("sample.txt", sep="\t")
> s1 <- as.matrix(s)
> s1
V1 V2 V3
1 0.59 0.47 0.44
2 0.85 0.42 0.57
3 0.48 0.57 0.57
4 0.61 0.24 0.24
5 0.38 0.21 0.36
6 0.65 0.42 1.50
7 0.49 0.23 0.42
8 0.60 0.51 0.53
9 0.00 0.00 0.00
> s2 <- as.vector(s1)
> s2
[1] 0.59 0.85 0.48 0.61 0.38 0.65 0.49 0.60 0.00 0.47
0.42 0.57 0.24 0.21 0.42 0.23 0.51 0.00 0.44 0.57 0.57
0.24 0.36
[24] 1.50 0.42 0.53 0.00
> s3 <- c(s1)
> s3
[1] 0.59 0.85 0.48 0.61 0.38 0.65 0.49 0.60 0.00 0.47
0.42 0.57 0.24 0.21 0.42 0.23 0.51 0.00 0.44 0.57 0.57
0.24 0.36
[24] 1.50 0.42 0.53 0.00
> s3 <- s1
> dim(s3) <- NULL
> s3
[1] 0.59 0.85 0.48 0.61 0.38 0.65 0.49 0.60 0.00 0.47
0.42 0.57 0.24 0.21 0.42 0.23 0.51 0.00 0.44 0.57 0.57
0.24 0.36
[24] 1.50 0.42 0.53 0.00
Interestingly, if I apply the same three solutions to
the data.frame 's', the results are different: (1)
as.vector() keeps the data frame as it is. (2) c()
changes into lists, and (3) making dim() <- NULL keeps
as structure.
Thanks again.
Kind regards,
Ezhil
--- Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:
> Marc Schwartz <marc_schwartz at comcast.net> writes:
>
> > On Wed, 2007-03-28 at 19:55 -0200, Alberto
> Monteiro wrote:
> >> Prof Brian Ripley wrote:
> >> >
> >> > We have already seen three solutions.
> >> >
> >> > I don't like to see the use of c() for its side
> effects. In this
> >> > case Marc's as.vector seems to me to be
> self-explanatory, and that
> >> > is a virtue in programming that is too often
> undervalued.
> >> >
> >> I agree; but for our enlightnment, what are the
> side effects of
> >> c()?
> >>
> >> Alberto Monteiro
> >
> > I believe that Prof. Ripley is referring to the
> following, from the
> > Details section in ?c:
> >
> >
> > "c is sometimes used for its side effect of
> removing attributes except
> > names, for example to turn an array into a vector.
> as.vector is a more
> > intuitive way to do this, but also drops names."
> >
> >
> > There are also examples in ?c of this behavior.
>
> The terminology is a bit unfortunate, though. "Side
> effect" usually
> means an effect that is not reflected in the return
> value of a
> function, like printing, plotting, or assignment.
>
> --
> O__ ---- Peter Dalgaard Øster
> Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099,
> 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark
> Ph: (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)
> FAX: (+45) 35327907
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained,
> reproducible code.
>
____________________________________________________________________________________
Food fight? Enjoy some healthy debate
More information about the R-help
mailing list