Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Ebert,Timothy Aaron
Originally this post was to just look at execution times for different 
approaches to solving this problem.
Now I have a question:
   I change the code for calculating a1 from c(c1, c2) to data.frame(c(c1,c2)). 
This changes the execution times of all the other variables. What am I missing?

Original
For efficiency, the answer Avi provided is still the best option with these 
data. The append() method is next. Both of these approaches avoid having to 
make a data frame in the wide format. The slowest method is pivot_longer(). 
Note that the order of elements is different in the pivot_longer() approach. If 
the order matters then some of these answers will need sorting to get the 
correct output. Also note that a1 and a2 are vectors, while the others are data 
frames. However, all of these appear correct from our understanding of the 
problem.

library(tidyverse)
library(microbenchmark)
c1 <- c("Tom","Dick")
c2 <- c("Larry","Curly")
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)))},
  a5 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=unlist(c3), row.names=NULL)},
  a6 <- {c3 <- data.frame(Name1=c1, Name2=c2)
pivot_longer(c3, cols=everything(),names_to="Names")},
  a7 <- {c3 <- data.frame(Name1=c1, Name2=c2)
data.frame(Names=c(c3$Name1,c3$Name2))},
  times=100L)
print(res)

Mean execution times for seven different methods where a1 <- c(c1,c2)
Method  Mean(ms)CLD
a1  1998a  
a2  5749a  
a3  1055501  b 
a4  592548   b 
a5  682491   b 
a6  6962660c 
a7  608337   b


Mean execution times for seven different methods where a1 <- 
data.frame(c(c1,c2))
Method  meancld
a1  272.467   b
a2  5.768   a
a3  907.171   d
a4  561.863 c
a5  581.989 c
a6  6371.465e
a7  552.208 c

-Original Message-
From: R-help  On Behalf Of Richard O'Keefe
Sent: Tuesday, April 4, 2023 8:21 AM
To: Sparks, John 
Cc: r-help@r-project.org
Subject: Re: [R] Simple Stacking of Two Columns

[External Email]

Just to repeat:
you have

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

and you want

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

There must be something I am missing, because

   NamesLong <- data.frame(Names = c(NamesWide$Name1, NamesWide$Name2))

appears to do the job in the simplest possible manner.  There are all sorts of 
alternatives, such as
   data.frame(Name = as.vector(as.matrix(NamesWide[, 1:2])))

As for stack(), the main problem there was a typo (Names2 for Name2).

> stack(NamesWide)
  values   ind
1Tom Name1
2   Dick Name1
3  Larry Name2
4  Curly Name2

If there were multiple columns, you might do

> stack(NamesWide[,c("Name1","Name2")])$values
[1] "Tom"   "Dick"  "Larry" "Curly"


On Tue, 4 Apr 2023 at 03:09, Sparks, John  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::

Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Richard O'Keefe
Just to repeat:
you have

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

and you want

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

There must be something I am missing, because

   NamesLong <- data.frame(Names = c(NamesWide$Name1, NamesWide$Name2))

appears to do the job in the simplest possible manner.  There are all sorts
of alternatives, such as
   data.frame(Name = as.vector(as.matrix(NamesWide[, 1:2])))

As for stack(), the main problem there was a typo (Names2 for Name2).

> stack(NamesWide)
  values   ind
1Tom Name1
2   Dick Name1
3  Larry Name2
4  Curly Name2

If there were multiple columns, you might do

> stack(NamesWide[,c("Name1","Name2")])$values
[1] "Tom"   "Dick"  "Larry" "Curly"


On Tue, 4 Apr 2023 at 03:09, Sparks, John  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..TomDick.. c..LarryCurly..
> 1  Tom
> 2 Dick
> 3Larry
> 4Curly
>
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
> > NamesLong
>   Name1 Name2
> 1   Tom Larry
> 2  Dick Curly
>
>
> Please help me solve what should be a very simple problem.
>
> Thanks,
> John Sparks
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-04 Thread Kimmo Elo
Hi,

or maybe this?

NamesLong<-data.frame(Names=unlist(NamesWide), row.names = NULL)

HTH,

Kimmo

ma, 2023-04-03 kello 16:23 +, Ebert,Timothy Aaron kirjoitti:
> 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  On Behalf Of Marc
> Schwartz via R-help
> Sent: Monday, April 3, 2023 11:44 AM
> To: Sparks, John ; r-help@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:
> 
> > stack(NamesWide)
>   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:
> 
> > stack(NamesWide)$values
> [1] "Tom"   "Dick"  "Larry" "Curly"
> 
> returns a vector, or:
> 
> > stack(NamesWide)[, 1, drop = FALSE]
>   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
> (jspa...@uic.edu (mailto:jspa...@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..TomDick.. c..LarryCurly..
> > 1 Tom
> > 2 Dick
> > 3 Larry
> > 4 Curly
> > 
> > gather makes no change to the data
> > NamesLong<-gather(NamesWide,Name1,Name2)
> > > NamesLong
> > Name1 Name2
> > 1 Tom Larry
> > 2 Dick Curly
> > 
> > 
> > Please help me solve what should be a very simple problem.
> > 
> > Thanks,
> > John Sparks
> > 
> > 
> > 
> > 
> > 
> > [[alternative HTML version deleted]]
> > 
> > __
> > R-help@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.
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, 

Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Ebert,Timothy Aaron
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
500900   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  On Behalf Of avi.e.gr...@gmail.com
Sent: Monday, April 3, 2023 9:28 PM
To: 'Heinz Tuechler' ; r-help@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
11 6
22 7
33 8
44 9
5510

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  On Behalf Of Heinz Tuechler
Sent: Monday, April 3, 2023 4:39 PM
To: r-help@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"  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..TomDick.. c..LarryCurly..
>> 1  Tom
>> 2 Dick
>> 3Larry
>> 4Curly
>>
>> gather makes no change to the data
>> NamesLong<-gather(NamesWide,Name1,Name2)
>>> NamesLong
>>  Name1 Name2
>> 1   Tom Larry
>> 2  Dick Curly
>>
>>
>> Please help me solve what should be a very simple problem.
>>
>> Thanks,
>> John Sparks
>>
>>
>>
>>
>>
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsta
>> 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&res

Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread avi.e.gross
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
11 6
22 7
33 8
44 9
5510

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  On Behalf Of Heinz Tuechler
Sent: Monday, April 3, 2023 4:39 PM
To: r-help@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"  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..TomDick.. c..LarryCurly..
>> 1  Tom
>> 2 Dick
>> 3Larry
>> 4Curly
>>
>> gather makes no change to the data
>> NamesLong<-gather(NamesWide,Name1,Name2)
>>> NamesLong
>>  Name1 Name2
>> 1   Tom Larry
>> 2  Dick Curly
>>
>>
>> Please help me solve what should be a very simple problem.
>>
>> Thanks,
>> John Sparks
>>
>>
>>
>>
>>
>>  [[alternative HTML version deleted]]
>>
>> __
>> R-help@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.
>

__
R-help@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.

__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Heinz Tuechler

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"  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..TomDick.. c..LarryCurly..
1  Tom
2 Dick
3Larry
4Curly

gather makes no change to the data
NamesLong<-gather(NamesWide,Name1,Name2)

NamesLong

 Name1 Name2
1   Tom Larry
2  Dick Curly


Please help me solve what should be a very simple problem.

Thanks,
John Sparks





[[alternative HTML version deleted]]

__
R-help@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.




__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Jeff Newmiller
unname(unlist(NamesWide))

On April 3, 2023 8:08:59 AM PDT, "Sparks, John"  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..TomDick.. c..LarryCurly..
>1  Tom
>2 Dick
>3Larry
>4Curly
>
>gather makes no change to the data
>NamesLong<-gather(NamesWide,Name1,Name2)
>> NamesLong
>  Name1 Name2
>1   Tom Larry
>2  Dick Curly
>
>
>Please help me solve what should be a very simple problem.
>
>Thanks,
>John Sparks
>
>
>
>
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Ebert,Timothy Aaron
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  On Behalf Of Marc Schwartz via 
R-help
Sent: Monday, April 3, 2023 11:44 AM
To: Sparks, John ; r-help@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:

> stack(NamesWide)
  values   ind
1Tom 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:

> stack(NamesWide)$values
[1] "Tom"   "Dick"  "Larry" "Curly"

returns a vector, or:

> stack(NamesWide)[, 1, drop = FALSE]
  values
1Tom
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 (jspa...@uic.edu 
(mailto:jspa...@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..TomDick.. c..LarryCurly..
> 1 Tom
> 2 Dick
> 3 Larry
> 4 Curly
>
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
> > NamesLong
> Name1 Name2
> 1 Tom Larry
> 2 Dick Curly
>
>
> Please help me solve what should be a very simple problem.
>
> Thanks,
> John Sparks
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.

__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Marc Schwartz via R-help
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:

> stack(NamesWide)
  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:

> stack(NamesWide)$values
[1] "Tom"   "Dick"  "Larry" "Curly"

returns a vector, or:

> stack(NamesWide)[, 1, drop = FALSE]
  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 (jspa...@uic.edu 
(mailto:jspa...@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..TomDick.. c..LarryCurly..
> 1 Tom  
> 2 Dick  
> 3 Larry
> 4 Curly
>
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
> > NamesLong
> Name1 Name2
> 1 Tom Larry
> 2 Dick Curly
>
>
> Please help me solve what should be a very simple problem.
>
> Thanks,
> John Sparks
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.

__
R-help@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.


Re: [R] Simple Stacking of Two Columns

2023-04-03 Thread Eric Berger
pivot_longer()

Sent from my iPhone

> On 3 Apr 2023, at 18:09, Sparks, John  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..TomDick.. c..LarryCurly..
> 1  Tom
> 2 Dick
> 3Larry
> 4Curly
> 
> gather makes no change to the data
> NamesLong<-gather(NamesWide,Name1,Name2)
>> NamesLong
>  Name1 Name2
> 1   Tom Larry
> 2  Dick Curly
> 
> 
> Please help me solve what should be a very simple problem.
> 
> Thanks,
> John Sparks
> 
> 
> 
> 
> 
>[[alternative HTML version deleted]]
> 
> __
> R-help@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.

__
R-help@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.


[R] Simple Stacking of Two Columns

2023-04-03 Thread Sparks, John
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..TomDick.. c..LarryCurly..
1  Tom
2 Dick
3Larry
4Curly

gather makes no change to the data
NamesLong<-gather(NamesWide,Name1,Name2)
> NamesLong
  Name1 Name2
1   Tom Larry
2  Dick Curly


Please help me solve what should be a very simple problem.

Thanks,
John Sparks





[[alternative HTML version deleted]]

__
R-help@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.