Elements of Sets as dataframe column names

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Elements of Sets as dataframe column names

Neha Aggarwal
Hello all,

I have a set B and a dataframe df. I want to name the columns of the
dataframe after the elements of the set B.
For example, for set B with elements {{"P1"}, {"P2"}, {"P3", "P4"}} I want
to create a new dataframe with 3  columns named {"P1"} and {"P2"} and
{"P3","P4"}.

I tried colnames(df)<-(B). But it shows the elements as list, see below:
> colnames(df)
[1] "list(\"P1\")"         "list(\"P2\")"         "list(\"P3\", \"P4\")"


Second part of my question is what is the bests command to extract the
elements of a set?


Thanks,
Neha

        [[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: Elements of Sets as dataframe column names

Bert Gunter-2
You have failed to tell us that you are using a package, presumably the
sets package.

What I believe you don't understand is that the underlying data structures
that represent sets are not what you think they are, but behave as you
expect through the package API. So unless the API gets you the names in the
form you want, you will have to write code to do this yourself.  Based on
what you show above, various string handling functionality -- see, e.g.
?regexp and ?gsub or ?strsplit in base R or the stringr or stringi packages
which are likely easier to learn and use --  are how you could go about
this. Effort would be required to climb the larning curve, but I suspect
you would find it time well spent.

Cheers,
Bert



Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Mon, Mar 19, 2018 at 8:24 PM, Neha Aggarwal <[hidden email]>
wrote:

> Hello all,
>
> I have a set B and a dataframe df. I want to name the columns of the
> dataframe after the elements of the set B.
> For example, for set B with elements {{"P1"}, {"P2"}, {"P3", "P4"}} I want
> to create a new dataframe with 3  columns named {"P1"} and {"P2"} and
> {"P3","P4"}.
>
> I tried colnames(df)<-(B). But it shows the elements as list, see below:
> > colnames(df)
> [1] "list(\"P1\")"         "list(\"P2\")"         "list(\"P3\", \"P4\")"
>
>
> Second part of my question is what is the bests command to extract the
> elements of a set?
>
>
> Thanks,
> Neha
>
>         [[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: Elements of Sets as dataframe column names

Jim Lemon-4
In reply to this post by Neha Aggarwal
Hi Neha,
From your message I think that you might get what you want with:

names(df)<-unlist(B)

However, the "sets" package might handle lists differently.

Jim

On Tue, Mar 20, 2018 at 2:24 PM, Neha Aggarwal
<[hidden email]> wrote:

> Hello all,
>
> I have a set B and a dataframe df. I want to name the columns of the
> dataframe after the elements of the set B.
> For example, for set B with elements {{"P1"}, {"P2"}, {"P3", "P4"}} I want
> to create a new dataframe with 3  columns named {"P1"} and {"P2"} and
> {"P3","P4"}.
>
> I tried colnames(df)<-(B). But it shows the elements as list, see below:
>> colnames(df)
> [1] "list(\"P1\")"         "list(\"P2\")"         "list(\"P3\", \"P4\")"
>
>
> Second part of my question is what is the bests command to extract the
> elements of a set?
>
>
> Thanks,
> Neha
>
>         [[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.