R and Pandas: R equivalent of np.sum and np.reshape











up vote
-1
down vote

favorite












I have two line of code which I want to convert to R from Python:



Can anyone convert the below line of code from python to R:



np.sum([[0, 1], [0, 5]], axis=0)


&



np.reshape(li, [-1, N])


li is the list of elements and N is any variable.










share|improve this question






















  • You can add also input/output data, because sometimes R coder cannot run this code...
    – jezrael
    Nov 19 at 13:11










  • np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
    – apoorv parmar
    Nov 19 at 13:21















up vote
-1
down vote

favorite












I have two line of code which I want to convert to R from Python:



Can anyone convert the below line of code from python to R:



np.sum([[0, 1], [0, 5]], axis=0)


&



np.reshape(li, [-1, N])


li is the list of elements and N is any variable.










share|improve this question






















  • You can add also input/output data, because sometimes R coder cannot run this code...
    – jezrael
    Nov 19 at 13:11










  • np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
    – apoorv parmar
    Nov 19 at 13:21













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have two line of code which I want to convert to R from Python:



Can anyone convert the below line of code from python to R:



np.sum([[0, 1], [0, 5]], axis=0)


&



np.reshape(li, [-1, N])


li is the list of elements and N is any variable.










share|improve this question













I have two line of code which I want to convert to R from Python:



Can anyone convert the below line of code from python to R:



np.sum([[0, 1], [0, 5]], axis=0)


&



np.reshape(li, [-1, N])


li is the list of elements and N is any variable.







python r pandas numpy reshape






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 13:10









apoorv parmar

346




346












  • You can add also input/output data, because sometimes R coder cannot run this code...
    – jezrael
    Nov 19 at 13:11










  • np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
    – apoorv parmar
    Nov 19 at 13:21


















  • You can add also input/output data, because sometimes R coder cannot run this code...
    – jezrael
    Nov 19 at 13:11










  • np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
    – apoorv parmar
    Nov 19 at 13:21
















You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11




You can add also input/output data, because sometimes R coder cannot run this code...
– jezrael
Nov 19 at 13:11












np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21




np.sum([[0, 1], [0, 5]], axis=0) should result in array([0, 6]) For np.reshape(li, [-1, N]), I am not able to understand how this works and what is being done here. I am currently converting the python code to R and not able to understand this. li is ['1',2','3,], single dimensional array
– apoorv parmar
Nov 19 at 13:21












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Example data in R -



mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)


This is what it will look like -



     [,1] [,2]
[1,] 0 0
[2,] 5 1


1st part



rowSums(mat)


Output will look like this -



[1] 0 6


If this doesn't seem to work as per needs try colSums (equivalent to varying axis in numpy)



2nd Part



matrix(mat, 1, length(mat))


This will flatten the array similar to np.reshape()



     [,1] [,2] [,3] [,4]
[1,] 0 5 0 1


This is the part where you want to flatten your matrix. reshape in numpy can be re-written in R by calling the matrix() function to re-cast the existing mat object



Note



This is a pure R implementation (standard libs). There are many other libraries like rehsape2 that will do it in different ways






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375385%2fr-and-pandas-r-equivalent-of-np-sum-and-np-reshape%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Example data in R -



    mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)


    This is what it will look like -



         [,1] [,2]
    [1,] 0 0
    [2,] 5 1


    1st part



    rowSums(mat)


    Output will look like this -



    [1] 0 6


    If this doesn't seem to work as per needs try colSums (equivalent to varying axis in numpy)



    2nd Part



    matrix(mat, 1, length(mat))


    This will flatten the array similar to np.reshape()



         [,1] [,2] [,3] [,4]
    [1,] 0 5 0 1


    This is the part where you want to flatten your matrix. reshape in numpy can be re-written in R by calling the matrix() function to re-cast the existing mat object



    Note



    This is a pure R implementation (standard libs). There are many other libraries like rehsape2 that will do it in different ways






    share|improve this answer

























      up vote
      1
      down vote













      Example data in R -



      mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)


      This is what it will look like -



           [,1] [,2]
      [1,] 0 0
      [2,] 5 1


      1st part



      rowSums(mat)


      Output will look like this -



      [1] 0 6


      If this doesn't seem to work as per needs try colSums (equivalent to varying axis in numpy)



      2nd Part



      matrix(mat, 1, length(mat))


      This will flatten the array similar to np.reshape()



           [,1] [,2] [,3] [,4]
      [1,] 0 5 0 1


      This is the part where you want to flatten your matrix. reshape in numpy can be re-written in R by calling the matrix() function to re-cast the existing mat object



      Note



      This is a pure R implementation (standard libs). There are many other libraries like rehsape2 that will do it in different ways






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Example data in R -



        mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)


        This is what it will look like -



             [,1] [,2]
        [1,] 0 0
        [2,] 5 1


        1st part



        rowSums(mat)


        Output will look like this -



        [1] 0 6


        If this doesn't seem to work as per needs try colSums (equivalent to varying axis in numpy)



        2nd Part



        matrix(mat, 1, length(mat))


        This will flatten the array similar to np.reshape()



             [,1] [,2] [,3] [,4]
        [1,] 0 5 0 1


        This is the part where you want to flatten your matrix. reshape in numpy can be re-written in R by calling the matrix() function to re-cast the existing mat object



        Note



        This is a pure R implementation (standard libs). There are many other libraries like rehsape2 that will do it in different ways






        share|improve this answer












        Example data in R -



        mat<-matrix(c(0,5,0,1), nrow=2, ncol=2)


        This is what it will look like -



             [,1] [,2]
        [1,] 0 0
        [2,] 5 1


        1st part



        rowSums(mat)


        Output will look like this -



        [1] 0 6


        If this doesn't seem to work as per needs try colSums (equivalent to varying axis in numpy)



        2nd Part



        matrix(mat, 1, length(mat))


        This will flatten the array similar to np.reshape()



             [,1] [,2] [,3] [,4]
        [1,] 0 5 0 1


        This is the part where you want to flatten your matrix. reshape in numpy can be re-written in R by calling the matrix() function to re-cast the existing mat object



        Note



        This is a pure R implementation (standard libs). There are many other libraries like rehsape2 that will do it in different ways







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 13:28









        Vivek Kalyanarangan

        4,0521725




        4,0521725






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375385%2fr-and-pandas-r-equivalent-of-np-sum-and-np-reshape%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python