separate and gather functions

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

separate and gather functions

reichmaj
R-Help Forum

 

I have a data set from which I have extracted two columns Column 1 is a
listing of Federal agencies and Column 2 lists functions like this

 

Col1                       Col2

Agency A             Function1, Function2, Function3, Function4

Agency B              Function2, Function4

Agency C              Function1, Function3, Function4

 

What I need is a long list like this

 

Col1                       Col2

Agency A             Function1

Agency A             Function2

Agency 4              Function3 etc

 

Is there a more elegant /efficient way other than what I did which was to
separate Col2 into separate columns and then gather the data back up

 

myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
"x4", "x5"), sep = ",")  

myDat4 <- gather(data=myDat3, key="type", value = "Value",
"x1","x2","x3","x4","x5", na.rm = TRUE)

 

while it works, looking for a more elegant solution, if there is one

 

Jeff


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: separate and gather functions

R help mailing list-2
This one uses only core R functions.  Does that count toward "elegance"?

> # your data, I assume, in a form one can copy and paste into R
> d <- data.frame(stringsAsFactors = FALSE,
    Col1 = c("Agency A", "Agency B", "Agency C"),
    Col2 = c("Function1, Function2, Function3, Function4",
        "Function2, Function4", "Function1, Function3, Function4"))
> # split Col2 by comma following by any number of spaces
> tmp <- strsplit(d$Col2, split=", *")
> data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp),
stringsAsFactors=FALSE)
      Col1      Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Mon, Aug 12, 2019 at 4:23 PM <[hidden email]> wrote:

> R-Help Forum
>
>
>
> I have a data set from which I have extracted two columns Column 1 is a
> listing of Federal agencies and Column 2 lists functions like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1, Function2, Function3, Function4
>
> Agency B              Function2, Function4
>
> Agency C              Function1, Function3, Function4
>
>
>
> What I need is a long list like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1
>
> Agency A             Function2
>
> Agency 4              Function3 etc
>
>
>
> Is there a more elegant /efficient way other than what I did which was to
> separate Col2 into separate columns and then gather the data back up
>
>
>
> myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
> "x4", "x5"), sep = ",")
>
> myDat4 <- gather(data=myDat3, key="type", value = "Value",
> "x1","x2","x3","x4","x5", na.rm = TRUE)
>
>
>
> while it works, looking for a more elegant solution, if there is one
>
>
>
> Jeff
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: separate and gather functions

reichmaj
William

 

Yes that works a little better as I don’t have to create notional variables. Thank you

 

Jeff

 

From: William Dunlap <[hidden email]>
Sent: Monday, August 12, 2019 6:46 PM
To: Jeff Reichman <[hidden email]>
Cc: [hidden email]
Subject: Re: [R] separate and gather functions

 

This one uses only core R functions.  Does that count toward "elegance"?

 

> # your data, I assume, in a form one can copy and paste into R

> d <- data.frame(stringsAsFactors = FALSE,
    Col1 = c("Agency A", "Agency B", "Agency C"),
    Col2 = c("Function1, Function2, Function3, Function4",
        "Function2, Function4", "Function1, Function3, Function4"))

> # split Col2 by comma following by any number of spaces

> tmp <- strsplit(d$Col2, split=", *")
> data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp), stringsAsFactors=FALSE)
      Col1      Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4

 

Bill Dunlap
TIBCO Software
wdunlap tibco.com <http://tibco.com>

 

 

On Mon, Aug 12, 2019 at 4:23 PM <[hidden email] <mailto:[hidden email]> > wrote:

R-Help Forum



I have a data set from which I have extracted two columns Column 1 is a
listing of Federal agencies and Column 2 lists functions like this



Col1                       Col2

Agency A             Function1, Function2, Function3, Function4

Agency B              Function2, Function4

Agency C              Function1, Function3, Function4



What I need is a long list like this



Col1                       Col2

Agency A             Function1

Agency A             Function2

Agency 4              Function3 etc



Is there a more elegant /efficient way other than what I did which was to
separate Col2 into separate columns and then gather the data back up



myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
"x4", "x5"), sep = ",")  

myDat4 <- gather(data=myDat3, key="type", value = "Value",
"x1","x2","x3","x4","x5", na.rm = TRUE)



while it works, looking for a more elegant solution, if there is one



Jeff


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] <mailto:[hidden email]>  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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: separate and gather functions

Ista Zahn
How about

> library(tidyr)
> separate_rows(d, Col2)
      Col1      Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4


On Mon, Aug 12, 2019 at 11:06 PM <[hidden email]> wrote:

>
> William
>
>
>
> Yes that works a little better as I don’t have to create notional variables. Thank you
>
>
>
> Jeff
>
>
>
> From: William Dunlap <[hidden email]>
> Sent: Monday, August 12, 2019 6:46 PM
> To: Jeff Reichman <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] separate and gather functions
>
>
>
> This one uses only core R functions.  Does that count toward "elegance"?
>
>
>
> > # your data, I assume, in a form one can copy and paste into R
>
> > d <- data.frame(stringsAsFactors = FALSE,
>     Col1 = c("Agency A", "Agency B", "Agency C"),
>     Col2 = c("Function1, Function2, Function3, Function4",
>         "Function2, Function4", "Function1, Function3, Function4"))
>
> > # split Col2 by comma following by any number of spaces
>
> > tmp <- strsplit(d$Col2, split=", *")
> > data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp), stringsAsFactors=FALSE)
>       Col1      Col2
> 1 Agency A Function1
> 2 Agency A Function2
> 3 Agency A Function3
> 4 Agency A Function4
> 5 Agency B Function2
> 6 Agency B Function4
> 7 Agency C Function1
> 8 Agency C Function3
> 9 Agency C Function4
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
>
>
>
>
>
> On Mon, Aug 12, 2019 at 4:23 PM <[hidden email] <mailto:[hidden email]> > wrote:
>
> R-Help Forum
>
>
>
> I have a data set from which I have extracted two columns Column 1 is a
> listing of Federal agencies and Column 2 lists functions like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1, Function2, Function3, Function4
>
> Agency B              Function2, Function4
>
> Agency C              Function1, Function3, Function4
>
>
>
> What I need is a long list like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1
>
> Agency A             Function2
>
> Agency 4              Function3 etc
>
>
>
> Is there a more elegant /efficient way other than what I did which was to
> separate Col2 into separate columns and then gather the data back up
>
>
>
> myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
> "x4", "x5"), sep = ",")
>
> myDat4 <- gather(data=myDat3, key="type", value = "Value",
> "x1","x2","x3","x4","x5", na.rm = TRUE)
>
>
>
> while it works, looking for a more elegant solution, if there is one
>
>
>
> Jeff
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] <mailto:[hidden email]>  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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: separate and gather functions

Eric Berger
Chapeau Ista :-)

On Tue, Aug 13, 2019 at 4:22 PM Ista Zahn <[hidden email]> wrote:

> How about
>
> > library(tidyr)
> > separate_rows(d, Col2)
>       Col1      Col2
> 1 Agency A Function1
> 2 Agency A Function2
> 3 Agency A Function3
> 4 Agency A Function4
> 5 Agency B Function2
> 6 Agency B Function4
> 7 Agency C Function1
> 8 Agency C Function3
> 9 Agency C Function4
>
>
> On Mon, Aug 12, 2019 at 11:06 PM <[hidden email]> wrote:
> >
> > William
> >
> >
> >
> > Yes that works a little better as I don’t have to create notional
> variables. Thank you
> >
> >
> >
> > Jeff
> >
> >
> >
> > From: William Dunlap <[hidden email]>
> > Sent: Monday, August 12, 2019 6:46 PM
> > To: Jeff Reichman <[hidden email]>
> > Cc: [hidden email]
> > Subject: Re: [R] separate and gather functions
> >
> >
> >
> > This one uses only core R functions.  Does that count toward "elegance"?
> >
> >
> >
> > > # your data, I assume, in a form one can copy and paste into R
> >
> > > d <- data.frame(stringsAsFactors = FALSE,
> >     Col1 = c("Agency A", "Agency B", "Agency C"),
> >     Col2 = c("Function1, Function2, Function3, Function4",
> >         "Function2, Function4", "Function1, Function3, Function4"))
> >
> > > # split Col2 by comma following by any number of spaces
> >
> > > tmp <- strsplit(d$Col2, split=", *")
> > > data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp),
> stringsAsFactors=FALSE)
> >       Col1      Col2
> > 1 Agency A Function1
> > 2 Agency A Function2
> > 3 Agency A Function3
> > 4 Agency A Function4
> > 5 Agency B Function2
> > 6 Agency B Function4
> > 7 Agency C Function1
> > 8 Agency C Function3
> > 9 Agency C Function4
> >
> >
> >
> > Bill Dunlap
> > TIBCO Software
> > wdunlap tibco.com <http://tibco.com>
> >
> >
> >
> >
> >
> > On Mon, Aug 12, 2019 at 4:23 PM <[hidden email] <mailto:
> [hidden email]> > wrote:
> >
> > R-Help Forum
> >
> >
> >
> > I have a data set from which I have extracted two columns Column 1 is a
> > listing of Federal agencies and Column 2 lists functions like this
> >
> >
> >
> > Col1                       Col2
> >
> > Agency A             Function1, Function2, Function3, Function4
> >
> > Agency B              Function2, Function4
> >
> > Agency C              Function1, Function3, Function4
> >
> >
> >
> > What I need is a long list like this
> >
> >
> >
> > Col1                       Col2
> >
> > Agency A             Function1
> >
> > Agency A             Function2
> >
> > Agency 4              Function3 etc
> >
> >
> >
> > Is there a more elegant /efficient way other than what I did which was to
> > separate Col2 into separate columns and then gather the data back up
> >
> >
> >
> > myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2", "x3",
> > "x4", "x5"), sep = ",")
> >
> > myDat4 <- gather(data=myDat3, key="type", value = "Value",
> > "x1","x2","x3","x4","x5", na.rm = TRUE)
> >
> >
> >
> > while it works, looking for a more elegant solution, if there is one
> >
> >
> >
> > Jeff
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] <mailto:[hidden email]>  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]]
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: separate and gather functions

reichmaj
In reply to this post by Ista Zahn
That's even easier

-----Original Message-----
From: Ista Zahn <[hidden email]>
Sent: Tuesday, August 13, 2019 8:22 AM
To: [hidden email]
Cc: William Dunlap <[hidden email]>; [hidden email]
Subject: Re: [R] separate and gather functions

How about

> library(tidyr)
> separate_rows(d, Col2)
      Col1      Col2
1 Agency A Function1
2 Agency A Function2
3 Agency A Function3
4 Agency A Function4
5 Agency B Function2
6 Agency B Function4
7 Agency C Function1
8 Agency C Function3
9 Agency C Function4


On Mon, Aug 12, 2019 at 11:06 PM <[hidden email]> wrote:

>
> William
>
>
>
> Yes that works a little better as I don’t have to create notional
> variables. Thank you
>
>
>
> Jeff
>
>
>
> From: William Dunlap <[hidden email]>
> Sent: Monday, August 12, 2019 6:46 PM
> To: Jeff Reichman <[hidden email]>
> Cc: [hidden email]
> Subject: Re: [R] separate and gather functions
>
>
>
> This one uses only core R functions.  Does that count toward "elegance"?
>
>
>
> > # your data, I assume, in a form one can copy and paste into R
>
> > d <- data.frame(stringsAsFactors = FALSE,
>     Col1 = c("Agency A", "Agency B", "Agency C"),
>     Col2 = c("Function1, Function2, Function3, Function4",
>         "Function2, Function4", "Function1, Function3, Function4"))
>
> > # split Col2 by comma following by any number of spaces
>
> > tmp <- strsplit(d$Col2, split=", *")
> > data.frame(Col1 = rep(d$Col1, lengths(tmp)), Col2 = unlist(tmp),
> > stringsAsFactors=FALSE)
>       Col1      Col2
> 1 Agency A Function1
> 2 Agency A Function2
> 3 Agency A Function3
> 4 Agency A Function4
> 5 Agency B Function2
> 6 Agency B Function4
> 7 Agency C Function1
> 8 Agency C Function3
> 9 Agency C Function4
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
>
>
>
>
>
> On Mon, Aug 12, 2019 at 4:23 PM <[hidden email] <mailto:[hidden email]> > wrote:
>
> R-Help Forum
>
>
>
> I have a data set from which I have extracted two columns Column 1 is
> a listing of Federal agencies and Column 2 lists functions like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1, Function2, Function3, Function4
>
> Agency B              Function2, Function4
>
> Agency C              Function1, Function3, Function4
>
>
>
> What I need is a long list like this
>
>
>
> Col1                       Col2
>
> Agency A             Function1
>
> Agency A             Function2
>
> Agency 4              Function3 etc
>
>
>
> Is there a more elegant /efficient way other than what I did which was
> to separate Col2 into separate columns and then gather the data back
> up
>
>
>
> myDat3 <- separate(data= myDat2, col = type, into = c("x1", "x2",
> "x3", "x4", "x5"), sep = ",")
>
> myDat4 <- gather(data=myDat3, key="type", value = "Value",
> "x1","x2","x3","x4","x5", na.rm = TRUE)
>
>
>
> while it works, looking for a more elegant solution, if there is one
>
>
>
> Jeff
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] <mailto:[hidden email]>  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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.