Skip to content
Prev 394024 / 398498 Next

Simple Stacking of Two Columns

My first thought was pivot_longer, and stack() is new to me. 
How about append(c1,c2) as another solution? Or data.frame(append(c1,c2)) if you want that form.

Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Marc Schwartz via R-help
Sent: Monday, April 3, 2023 11:44 AM
To: Sparks, John <jspark4 at uic.edu>; r-help at r-project.org
Subject: Re: [R] Simple Stacking of Two Columns

[External Email]

Hi,

You were on the right track using stack(), but you just pass the entire data frame as a single object, not the separate columns:
values   ind
1    Tom Name1
2   Dick Name1
3  Larry Name2
4  Curly Name2

Note that stack also returns the index (second column of 'ind' values), which tells you which column in the source data frame the stacked values originated from.

Thus, if you just want the actual data:
[1] "Tom"   "Dick"  "Larry" "Curly"

returns a vector, or:
values
1    Tom
2   Dick
3  Larry
4  Curly

which returns a data frame with a single column named 'values'.

Regards,

Marc Schwartz
On April 3, 2023 at 11:08:59 AM, Sparks, John (jspark4 at uic.edu (mailto:jspark4 at uic.edu)) wrote:

            
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=MEaORpaFihsIHu3Iu2GwO15ey%2BvZP3Wxa6UiS3g0PyQ%3D&reserved=0
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Ce0f22e022c0b48d2766408db345aa062%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C638161336072628296%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=ZwKZkaGoEVMu8Jp%2BbcIj%2FLVi9%2Fwug%2Fi48uarb8yg5KY%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.