Skip to content

Stacking of vectors to form a column vector

10 messages · Olufemi Bolarinwa, Jeff Newmiller, Jim Lemon +3 more

#
Hello,I am estimating a system of nonlinear equation where I need to stack my vector of y. I have data of about 6000units. I tried using the rbind but instead of having a vector of 1 by 18000, it is giving me a 3 by 6000 so that my matrix multiplication is non-conformable. The stack command requires an identifier but in this case, I do not have a unique identifier.
I would like to stack the the first 6000 units of y1 on the 2nd 6000 units of y2 and 6000 units of y3.
Any help will be greatly appreciated.
ThanksOlufemi

?
#
Vectors are not "columns" or "rows". Use the c() function to concatenate vectors.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On April 29, 2015 6:56:46 PM PDT, Olufemi Bolarinwa <dafemlions at yahoo.co.uk> wrote:
#
Thank you Jeff for your response.
My y1, y2, y3 are actually 3 columns in the data so I cannot use the c() function to concatenate them. I am confusing the "columns" with vectors. I actually meant columns.
Any help will be much appreciated
Olufemi?
On Wednesday, 29 April 2015, 22:31, Jeff Newmiller <jdnewmil at dcn.davis.CA.us> wrote:
Vectors are not "columns" or "rows". Use the c() function to concatenate vectors.
---------------------------------------------------------------------------
Jeff Newmiller? ? ? ? ? ? ? ? ? ? ? ? The? ? .....? ? ? .....? Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>? ? ? ? Basics: ##.#.? ? ? ##.#.? Live Go...
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Live:? OO#.. Dead: OO#..? Playing
Research Engineer (Solar/Batteries? ? ? ? ? ? O.O#.? ? ? #.O#.? with
/Software/Embedded Controllers)? ? ? ? ? ? ? .OO#.? ? ? .OO#.? rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

  
  
#
I am sure you can use c() because columns may be vectors even though vectors are not columns, but you really need to follow the posting guide and provide a reproducible example for us to show you how. You might find [1] helpful, in particular as it describes the use of the dput function to give us a few rows of your data.

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example 
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On April 29, 2015 7:48:24 PM PDT, Olufemi Bolarinwa <dafemlions at yahoo.co.uk> wrote:
#
Hi Olufemi,
I sounds like you have a data frame (let's call it "mydata") with at
least three elements (columns). You may be trying to use c() in this
way:

y1to3<-c(y1,y2,y3)

in which case it won't work. However:

y1to3<-c(mydata$y1,mydata$y2,mydata$y3)

might do what you want, substituting whatever the name of your data
frame is for "mydata".

Jim


On Thu, Apr 30, 2015 at 1:20 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
#
Here are two correct uses of the "stack command", if by that you mean the
stack() function.
values ind
1      1   a
2      2   a
3      3   a
4      4   b
5      5   b
6      6   b
7      7   c
8      8   c
9      9   c
values ind
1      1   a
2      2   a
3      3   a
4      4   b
5      5   b
6      6   b
7      7   c
8      8   c
9      9   c

I would say that the "values" column of the output fits your description
of what you want to do. (though I used names a, b, c instead of y1, y2,
y3).

But without a reproducible example, one can't say for sure.
#
... and if this is what is wanted, somewhat cleaner and more
generalizable for programming would be:

do.call(c, mydata[,1:3])

## where the column indices might have to be adjusted to get the
desired columns.

Cheers,

Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Wed, Apr 29, 2015 at 11:51 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
#
Hi,

Given that a data frame is a list:

  unlist(mydata[, 1:3])

For example:
[1] TRUE


Also, note that the returned result in both cases above retains names:
Sepal.Length1   Sepal.Length2   Sepal.Length3   Sepal.Length4 
            5.1             4.9             4.7             4.6 
  Sepal.Length5   Sepal.Length6   Sepal.Length7   Sepal.Length8 
            5.0             5.4             4.6             5.0 
  Sepal.Length9  Sepal.Length10  Sepal.Length11  Sepal.Length12 
            4.4             4.9             5.4             4.8 
...

You can just get the plain vector by using:
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7
 [17] 5.4 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4
 [33] 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6
 [49] 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1
...


As Bert notes, the indices would need to be adjusted for the actual data frame in question.

Regards,

Marc
#
Thank you. your suggestions all worked.
Best Regards?
On Thursday, 30 April 2015, 11:52, Bert Gunter <gunter.berton at gene.com> wrote:
... and if this is what is wanted, somewhat cleaner and more
generalizable for programming would be:

do.call(c, mydata[,1:3])

## where the column indices might have to be adjusted to get the
desired columns.

Cheers,

Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Wed, Apr 29, 2015 at 11:51 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
#
Yes, I think unlist() is still better.

One caution (for all): make sure the columns are all of the same
type/class/mode or you may be in for nasty surprises.

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
Clifford Stoll
On Thu, Apr 30, 2015 at 9:32 AM, Marc Schwartz <marc_schwartz at me.com> wrote: