[R] Simple Stacking of Two Columns

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Tue Apr 4 03:54:17 CEST 2023

```Combined, the answers are close to an example in the documentation for microbenchmark where they (in part) look at execution times for c() versus append(). Here is the code, but c() has the shortest execution time in this example. If I remove a3 and a4 then c() is significantly shorter than append().

install.packages('microbenchmark')
library(microbenchmark)
c1 <- c("Lilia","Sam")
c2 <- c("Skywalker","Voldemort")
res <- microbenchmark(a1 <- c(c1, c2),
a2 <- append(c1, c2),
a3 <- {c3 <- data.frame(Name1=c1, Name2=c2)
stack(c3)},
a4 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=with(c3, c(Name1, Name2)))},
times=100L)
print(res)

min     lq   mean median     uq     max neval cld
500    900   1647   1700   1900   11000   100 a
2300   3000   4604   4800   5300   14600   100 a
779500 812950 843367 832750 868800 1129300   100   c
486800 506650 527375 527950 544000  621900   100  b

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of avi.e.gross using gmail.com
Sent: Monday, April 3, 2023 9:28 PM
To: 'Heinz Tuechler' <tuechler using gmx.at>; r-help using r-project.org
Subject: Re: [R] Simple Stacking of Two Columns

[External Email]

I may be missing something but using the plain old c() combine function seems to work fine:

df <- data.frame(left = 1:5, right = 6:10) df.combined <- data.frame(comb = c(df\$left, df\$right))

df
left right
1    1     6
2    2     7
3    3     8
4    4     9
5    5    10

df.combined
comb
1     1
2     2
3     3
4     4
5     5
6     6
7     7
8     8
9     9
10   10

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Heinz Tuechler
Sent: Monday, April 3, 2023 4:39 PM
To: r-help using r-project.org
Subject: Re: [R] Simple Stacking of Two Columns

Jeff Newmiller wrote/hat geschrieben on/am 03.04.2023 18:26:
> unname(unlist(NamesWide))
Why not:

NamesWide <- data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
NamesLong <- data.frame(Names=with(NamesWide, c(Name1, Name2)))

>
> On April 3, 2023 8:08:59 AM PDT, "Sparks, John" <jspark4 using uic.edu> wrote:
>> Hi R-Helpers,
>>
>> Sorry to bother you, but I have a simple task that I can't figure out
>> how
to do.
>>
>> For example, I have some names in two columns
>>
>> NamesWide<-data.frame(Name1=c("Tom","Dick"),Name2=c("Larry","Curly"))
>>
>> and I simply want to get a single column
>> NamesLong<-data.frame(Names=c("Tom","Dick","Larry","Curly"))
>>> NamesLong
>>  Names
>> 1   Tom
>> 2  Dick
>> 3 Larry
>> 4 Curly
>>
>>
>> Stack produces an error
>> NamesLong<-stack(NamesWide\$Name1,NamesWide\$Names2)
>> Error in if (drop) { : argument is of length zero
>>
>> So does bind_rows
>>> NamesLong<-dplyr::bind_rows(NamesWide\$Name1,NamesWide\$Name2)
>> Error in `dplyr::bind_rows()`:
>> ! Argument 1 must be a data frame or a named atomic vector.
>> Run `rlang::last_error()` to see where the error occurred.
>>
>> I tried making separate dataframes to get around the error in
>> bind_rows
but it puts the data in two different columns
>> Name1<-data.frame(c("Tom","Dick"))
>> Name2<-data.frame(c("Larry","Curly"))
>> NamesLong<-dplyr::bind_rows(Name1,Name2)
>>> NamesLong
>>  c..Tom....Dick.. c..Larry....Curly..
>> 1              Tom                <NA>
>> 2             Dick                <NA>
>> 3             <NA>               Larry
>> 4             <NA>               Curly
>>
>> gather makes no change to the data
>> NamesLong<-gather(NamesWide,Name1,Name2)
>>> NamesLong
>>  Name1 Name2
>> 1   Tom Larry
>> 2  Dick Curly
>>
>>
>>
>> Thanks,
>> John Sparks
>>
>>
>>
>>
>>
>>      [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> t.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.e
>> du%7C35b7ed1179cf4b8c549f08db34abeb5b%7C0d4da0f84a314d76ace60a62331e1
>> b84%7C0%7C0%7C638161685242095287%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
>> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7
>> C&sdata=dDPUQYH5pkdJl7gdcjAtA1QeK2J%2FlQOguTu3mbiaQjk%3D&reserved=0
>> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see