Moving 2nd column into 1st column using R

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

Moving 2nd column into 1st column using R

R help mailing list-2
Hi R-user,
I have a problem regarding R.  How can I move my 2nd column into 1st column.  For example I have data as below:
 mydf <- data.frame(matrix(1:6, ncol = 2))
 mydf
  X1 X2   1   4   2   5   3   6
I want move my 2nd column become like this:
  X1    1      2    3    4   5   6
Hope anyone can help me. Many thanks.

Zuhri
 
        [[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: Moving 2nd column into 1st column using R

David Winsemius

On 9/9/19 7:19 PM, smart hendsome via R-help wrote:

> Hi R-user,
> I have a problem regarding R.  How can I move my 2nd column into 1st column.  For example I have data as below:
>   mydf <- data.frame(matrix(1:6, ncol = 2))
>   mydf
>    X1 X2   1   4   2   5   3   6
> I want move my 2nd column become like this:
>    X1    1      2    3    4   5   6
> Hope anyone can help me. Many thanks.
>
> Zuhri
>  
> [[alternative HTML version deleted]]


I doubt that anyone can tell what your intentions are. You ahve posted
in html and the mailserver does not interpret html code. So repost in
plain text .... as the posting guide advises.


--

David.

>
> ______________________________________________
> [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: Moving 2nd column into 1st column using R

Jim Lemon-4
In reply to this post by R help mailing list-2
Hi Zuhri,
Try:

mydf<-mydf[,c(2,1)]

You might be surprised.

Jim

On Tue, Sep 10, 2019 at 12:20 PM smart hendsome via R-help
<[hidden email]> wrote:

>
> Hi R-user,
> I have a problem regarding R.  How can I move my 2nd column into 1st column.  For example I have data as below:
>  mydf <- data.frame(matrix(1:6, ncol = 2))
>  mydf
>   X1 X2   1   4   2   5   3   6
> I want move my 2nd column become like this:
>   X1    1      2    3    4   5   6
> Hope anyone can help me. Many thanks.
>
> Zuhri
>
>         [[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: Moving 2nd column into 1st column using R

PIKAL Petr
Or maybe use stack

stack(mydf)

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Tuesday, September 10, 2019 6:34 AM
> To: smart hendsome <[hidden email]>; r-help mailing list <r-
> [hidden email]>
> Subject: Re: [R] Moving 2nd column into 1st column using R
>
> Hi Zuhri,
> Try:
>
> mydf<-mydf[,c(2,1)]
>
> You might be surprised.
>
> Jim
>
> On Tue, Sep 10, 2019 at 12:20 PM smart hendsome via R-help <r-help@r-
> project.org> wrote:
> >
> > Hi R-user,
> > I have a problem regarding R.  How can I move my 2nd column into 1st
> column.  For example I have data as below:
> >  mydf <- data.frame(matrix(1:6, ncol = 2))  mydf
> >   X1 X2   1   4   2   5   3   6
> > I want move my 2nd column become like this:
> >   X1    1      2    3    4   5   6
> > Hope anyone can help me. Many thanks.
> >
> > Zuhri
> >
> >         [[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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[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: Moving 2nd column into 1st column using R

Martin Møller Skarbiniks Pedersen
In reply to this post by R help mailing list-2
On Tue, 10 Sep 2019 at 04:20, smart hendsome via R-help <
[hidden email]> wrote:
>
> Hi R-user,
> I have a problem regarding R.  How can I move my 2nd column into 1st
column.  For example I have data as below:
>  mydf <- data.frame(matrix(1:6, ncol = 2))
>  mydf
>   X1 X2   1   4   2   5   3   6
> I want move my 2nd column become like this:
>   X1    1      2    3    4   5   6
> Hope anyone can help me. Many thanks.


I am not sure I understand your problem, but try:

mydf <- data.frame(matrix(1:6, ncol = 2, byrow = TRUE))

Regards
Martin

        [[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.