[R] Stacking of vectors to form a column vector
Marc Schwartz
marc_schwartz at me.com
Thu Apr 30 18:32:50 CEST 2015
Hi,
Given that a data frame is a list:
unlist(mydata[, 1:3])
For example:
> all(unlist(iris[, 1:3]) == do.call(c, iris[, 1:3]))
[1] TRUE
Also, note that the returned result in both cases above retains names:
> unlist(iris[, 1:3])
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:
> unlist(iris[, 1:3], use.names = FALSE)
[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
> On Apr 30, 2015, at 10:52 AM, 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:
>> 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:
>>> 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:
>>>> 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.
>>>>
>>>> On April 29, 2015 6:56:46 PM PDT, Olufemi Bolarinwa
>>>> <dafemlions at yahoo.co.uk> wrote:
>>>>> 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
More information about the R-help
mailing list