Re: [R] Large matrix into a vector

2007-03-29 Thread A Ezhil
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 [EMAIL PROTECTED] wrote:

 Marc Schwartz [EMAIL PROTECTED] 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
 ~~ - ([EMAIL PROTECTED]) 
 FAX: (+45) 35327907
 
 __
 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
 and provide commented, minimal, self-contained,
 reproducible code.
 



 

Food fight? Enjoy some healthy debate

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-29 Thread Marc Schwartz
On Thu, 2007-03-29 at 02:12 -0700, A Ezhil wrote:
 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. 

As Peter noted, the phrase 'side effect' may be sub-optimal. 

However, I would use the interpretation that in this case, the removal
of dim attributes on a single object as a consequence of the use of c(),
though documented, is not the primary intended purpose of the function.

Stated differently, perhaps in a manner consistent with Prof. Ripley's
reply, the coercion of a matrix to a vector by the use of c(), is
applying a function to an object to gain the benefit of behavior that is
secondary to the intended functionality of combining objects.

 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. 

That's because the underlying structure of a data frame is a list,
whereas a matrix is a vector.

HTH,

Marc

snip

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Ravi Varadhan
c(HR) will do it.

Ravi.


---

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: [EMAIL PROTECTED]

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 




-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of A Ezhil
Sent: Wednesday, March 28, 2007 1:28 PM
To: r-help@stat.math.ethz.ch
Subject: [R] Large matrix into a vector

Hi,

I have a matrix HR(9x27). I would like to make a
single vector with elements: t(HR[,1]) followed by
t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
way of converting this matrix into a vector rather
doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
..)?

Thanks in Advance.
Kind regards,
Ezhil


 


TV dinner still cooling? 
Check out Tonight's Picks on Yahoo! TV.

__
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
and provide commented, minimal, self-contained, reproducible code.

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Gabor Csardi
A matrix is just a vector with a dim attribute, so if you remove 
the dim attribute it'll be a vector. The elements of a matrix 
are stored columnwise so you'll get just what you want, if i get
your question right.

 g - matrix(1:6, nc=2, nr=3)
 g
 [,1] [,2]
[1,]14
[2,]25
[3,]36
 dim(g) - NULL
 g
[1] 1 2 3 4 5 6
 

Gabor

On Wed, Mar 28, 2007 at 10:27:48AM -0700, A Ezhil wrote:
 Hi,
 
 I have a matrix HR(9x27). I would like to make a
 single vector with elements: t(HR[,1]) followed by
 t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
 way of converting this matrix into a vector rather
 doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
 ..)?
 
 Thanks in Advance.
 Kind regards,
 Ezhil
 
 
  
 
 TV dinner still cooling? 
 Check out Tonight's Picks on Yahoo! TV.
 
 __
 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
 and provide commented, minimal, self-contained, reproducible code.

-- 
Csardi Gabor [EMAIL PROTECTED]MTA RMKI, ELTE TTK

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Marc Schwartz
On Wed, 2007-03-28 at 10:27 -0700, A Ezhil wrote:
 Hi,
 
 I have a matrix HR(9x27). I would like to make a
 single vector with elements: t(HR[,1]) followed by
 t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
 way of converting this matrix into a vector rather
 doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
 ..)?

Keep in mind that a matrix is simply a vector, with a 'dim' attribute.
In addition, the matrix elements are stored in column order, so:

  mat - matrix(1:(9 * 27), ncol = 27)


 mat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]1   10   19   28   37   46   55   64   738291   100
 [2,]2   11   20   29   38   47   56   65   748392   101
 [3,]3   12   21   30   39   48   57   66   758493   102
 [4,]4   13   22   31   40   49   58   67   768594   103
 [5,]5   14   23   32   41   50   59   68   778695   104
 [6,]6   15   24   33   42   51   60   69   788796   105
 [7,]7   16   25   34   43   52   61   70   798897   106
 [8,]8   17   26   35   44   53   62   71   808998   107
 [9,]9   18   27   36   45   54   63   72   819099   108
  [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
 [1,]   109   118   127   136   145   154   163   172   181   190   199
 [2,]   110   119   128   137   146   155   164   173   182   191   200
 [3,]   111   120   129   138   147   156   165   174   183   192   201
 [4,]   112   121   130   139   148   157   166   175   184   193   202
 [5,]   113   122   131   140   149   158   167   176   185   194   203
 [6,]   114   123   132   141   150   159   168   177   186   195   204
 [7,]   115   124   133   142   151   160   169   178   187   196   205
 [8,]   116   125   134   143   152   161   170   179   188   197   206
 [9,]   117   126   135   144   153   162   171   180   189   198   207
  [,24] [,25] [,26] [,27]
 [1,]   208   217   226   235
 [2,]   209   218   227   236
 [3,]   210   219   228   237
 [4,]   211   220   229   238
 [5,]   212   221   230   239
 [6,]   213   222   231   240
 [7,]   214   223   232   241
 [8,]   215   224   233   242
 [9,]   216   225   234   243


 as.vector(mat)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
 [17]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
 [33]  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64
 [65]  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112
[113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
[129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
[161] 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
[177] 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
[193] 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
[209] 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
[225] 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
[241] 241 242 243


HTH,

Marc Schwartz

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Alberto Monteiro
A Ezhil wrote:
 
 I have a matrix HR(9x27). I would like to make a
 single vector with elements: t(HR[,1]) followed by
 t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
 way of converting this matrix into a vector rather
 doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
 ..)?
 
It might be simpler than you thought...

HR - matrix(1:(9*27), nrow=9) # just to create a 9x27 matrix
c(HR)  # oops, here it is!

Alberto Monteiro

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Prof Brian Ripley
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.

On Wed, 28 Mar 2007, Marc Schwartz wrote:

 On Wed, 2007-03-28 at 10:27 -0700, A Ezhil wrote:
 Hi,

 I have a matrix HR(9x27). I would like to make a
 single vector with elements: t(HR[,1]) followed by
 t(HR[,2]) and then t(HR[,3] ... etc. Is there any neat
 way of converting this matrix into a vector rather
 doing something like c(t(HR[,1]), t(HR[,2]), t(HR[,3])
 ..)?

 Keep in mind that a matrix is simply a vector, with a 'dim' attribute.
 In addition, the matrix elements are stored in column order, so:

  mat - matrix(1:(9 * 27), ncol = 27)


 mat
  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
 [1,]1   10   19   28   37   46   55   64   738291   100
 [2,]2   11   20   29   38   47   56   65   748392   101
 [3,]3   12   21   30   39   48   57   66   758493   102
 [4,]4   13   22   31   40   49   58   67   768594   103
 [5,]5   14   23   32   41   50   59   68   778695   104
 [6,]6   15   24   33   42   51   60   69   788796   105
 [7,]7   16   25   34   43   52   61   70   798897   106
 [8,]8   17   26   35   44   53   62   71   808998   107
 [9,]9   18   27   36   45   54   63   72   819099   108
  [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23]
 [1,]   109   118   127   136   145   154   163   172   181   190   199
 [2,]   110   119   128   137   146   155   164   173   182   191   200
 [3,]   111   120   129   138   147   156   165   174   183   192   201
 [4,]   112   121   130   139   148   157   166   175   184   193   202
 [5,]   113   122   131   140   149   158   167   176   185   194   203
 [6,]   114   123   132   141   150   159   168   177   186   195   204
 [7,]   115   124   133   142   151   160   169   178   187   196   205
 [8,]   116   125   134   143   152   161   170   179   188   197   206
 [9,]   117   126   135   144   153   162   171   180   189   198   207
  [,24] [,25] [,26] [,27]
 [1,]   208   217   226   235
 [2,]   209   218   227   236
 [3,]   210   219   228   237
 [4,]   211   220   229   238
 [5,]   212   221   230   239
 [6,]   213   222   231   240
 [7,]   214   223   232   241
 [8,]   215   224   233   242
 [9,]   216   225   234   243


 as.vector(mat)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
 [17]  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32
 [33]  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48
 [49]  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64
 [65]  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
 [81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96
 [97]  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112
 [113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
 [129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
 [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
 [161] 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
 [177] 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
 [193] 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
 [209] 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 [225] 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
 [241] 241 242 243


 HTH,

 Marc Schwartz

 __
 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
 and provide commented, minimal, self-contained, reproducible code.


-- 
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread John Kane
Is this something like what you want?

ab  - rep(1,4)
bb - rep(2,4)
cc - rep(3,4)
mydata  - data.frame(ab,bb,cc)
unlist(mydata)
unlist(data.frame(t(mydata)))


--- A Ezhil [EMAIL PROTECTED] wrote:

 Hi,
 
 I have a matrix HR(9x27). I would like to make a
 single vector with elements: t(HR[,1]) followed by
 t(HR[,2]) and then t(HR[,3] ... etc. Is there any
 neat
 way of converting this matrix into a vector rather
 doing something like c(t(HR[,1]), t(HR[,2]),
 t(HR[,3])
 ..)?
 
 Thanks in Advance.
 Kind regards,
 Ezhil
 
 
  


 TV dinner still cooling? 
 Check out Tonight's Picks on Yahoo! TV.
 
 __
 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
 and provide commented, minimal, self-contained,
 reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Marc Schwartz
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.

HTH,

Marc

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Alberto Monteiro
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

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Large matrix into a vector

2007-03-28 Thread Peter Dalgaard
Marc Schwartz [EMAIL PROTECTED] 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
~~ - ([EMAIL PROTECTED])  FAX: (+45) 35327907

__
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
and provide commented, minimal, self-contained, reproducible code.