When using the function By in R, how do I create a vector of the factors for each group?












1















I am using the function "by" in R and at the end, I would like a vector with the factors in it as well because I want to create a data frame that has what I computed and the factor next to it that the function used to subset that particular group. How do I extract the factors from the output? The setup is as followed.



id <- rep(14:18,each=5)
x <- rep(5:9,each =5)
y <- rep(5:9, each = 5)
t <- data.frame(id = id,x=x,y=y)
s <- by(t,id,function(h){ (h$x%*%h$y)/sum(h$y)})


In the final output I would like something like this.



sum id 
5 14
6 15
7 16
8 17
9 18


Thanks










share|improve this question

























  • Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

    – Parfait
    Nov 22 '18 at 1:56











  • Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

    – I Wright
    Nov 23 '18 at 16:33
















1















I am using the function "by" in R and at the end, I would like a vector with the factors in it as well because I want to create a data frame that has what I computed and the factor next to it that the function used to subset that particular group. How do I extract the factors from the output? The setup is as followed.



id <- rep(14:18,each=5)
x <- rep(5:9,each =5)
y <- rep(5:9, each = 5)
t <- data.frame(id = id,x=x,y=y)
s <- by(t,id,function(h){ (h$x%*%h$y)/sum(h$y)})


In the final output I would like something like this.



sum id 
5 14
6 15
7 16
8 17
9 18


Thanks










share|improve this question

























  • Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

    – Parfait
    Nov 22 '18 at 1:56











  • Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

    – I Wright
    Nov 23 '18 at 16:33














1












1








1








I am using the function "by" in R and at the end, I would like a vector with the factors in it as well because I want to create a data frame that has what I computed and the factor next to it that the function used to subset that particular group. How do I extract the factors from the output? The setup is as followed.



id <- rep(14:18,each=5)
x <- rep(5:9,each =5)
y <- rep(5:9, each = 5)
t <- data.frame(id = id,x=x,y=y)
s <- by(t,id,function(h){ (h$x%*%h$y)/sum(h$y)})


In the final output I would like something like this.



sum id 
5 14
6 15
7 16
8 17
9 18


Thanks










share|improve this question
















I am using the function "by" in R and at the end, I would like a vector with the factors in it as well because I want to create a data frame that has what I computed and the factor next to it that the function used to subset that particular group. How do I extract the factors from the output? The setup is as followed.



id <- rep(14:18,each=5)
x <- rep(5:9,each =5)
y <- rep(5:9, each = 5)
t <- data.frame(id = id,x=x,y=y)
s <- by(t,id,function(h){ (h$x%*%h$y)/sum(h$y)})


In the final output I would like something like this.



sum id 
5 14
6 15
7 16
8 17
9 18


Thanks







r subset tapply






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 16:31







I Wright

















asked Nov 21 '18 at 23:26









I WrightI Wright

154




154













  • Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

    – Parfait
    Nov 22 '18 at 1:56











  • Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

    – I Wright
    Nov 23 '18 at 16:33



















  • Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

    – Parfait
    Nov 22 '18 at 1:56











  • Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

    – I Wright
    Nov 23 '18 at 16:33

















Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

– Parfait
Nov 22 '18 at 1:56





Your attempted output of a matrix multiplication, %*%, does not align with expected output which can be done with aggregate(). Please advise.

– Parfait
Nov 22 '18 at 1:56













Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

– I Wright
Nov 23 '18 at 16:33





Sorry about that.I have updated the code. It was suppose to divide by the sum of one of the columns which I left out.

– I Wright
Nov 23 '18 at 16:33












1 Answer
1






active

oldest

votes


















1














Consider returning a data frame within your by call and not single-value vector where your calculation is rendered as a new column and x and y columns are removed all using transform(), the function to manipulate columns and then return adjusted data frame.



Then, to avoid duplicate data (since you are running an aggregation), wrap result in unique(). Finally, since by will return a list of one-row data frames, run a do.call(rbind, ...) outside to row bind all elements together.



df_list <- by(t,id, function(sub)
unique(transform(sub,
sum_output = (x %*% y) / sum(y),
x = NULL,
y = NULL)
)
)

final_df <- do.call(rbind, df_list)
row.names(final_df) <- NULL

final_df

# id sum
# 1 14 5
# 2 15 6
# 3 16 7
# 4 17 8
# 5 18 9





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',
    autoActivateHeartbeat: false,
    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%2f53421859%2fwhen-using-the-function-by-in-r-how-do-i-create-a-vector-of-the-factors-for-eac%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









    1














    Consider returning a data frame within your by call and not single-value vector where your calculation is rendered as a new column and x and y columns are removed all using transform(), the function to manipulate columns and then return adjusted data frame.



    Then, to avoid duplicate data (since you are running an aggregation), wrap result in unique(). Finally, since by will return a list of one-row data frames, run a do.call(rbind, ...) outside to row bind all elements together.



    df_list <- by(t,id, function(sub)
    unique(transform(sub,
    sum_output = (x %*% y) / sum(y),
    x = NULL,
    y = NULL)
    )
    )

    final_df <- do.call(rbind, df_list)
    row.names(final_df) <- NULL

    final_df

    # id sum
    # 1 14 5
    # 2 15 6
    # 3 16 7
    # 4 17 8
    # 5 18 9





    share|improve this answer




























      1














      Consider returning a data frame within your by call and not single-value vector where your calculation is rendered as a new column and x and y columns are removed all using transform(), the function to manipulate columns and then return adjusted data frame.



      Then, to avoid duplicate data (since you are running an aggregation), wrap result in unique(). Finally, since by will return a list of one-row data frames, run a do.call(rbind, ...) outside to row bind all elements together.



      df_list <- by(t,id, function(sub)
      unique(transform(sub,
      sum_output = (x %*% y) / sum(y),
      x = NULL,
      y = NULL)
      )
      )

      final_df <- do.call(rbind, df_list)
      row.names(final_df) <- NULL

      final_df

      # id sum
      # 1 14 5
      # 2 15 6
      # 3 16 7
      # 4 17 8
      # 5 18 9





      share|improve this answer


























        1












        1








        1







        Consider returning a data frame within your by call and not single-value vector where your calculation is rendered as a new column and x and y columns are removed all using transform(), the function to manipulate columns and then return adjusted data frame.



        Then, to avoid duplicate data (since you are running an aggregation), wrap result in unique(). Finally, since by will return a list of one-row data frames, run a do.call(rbind, ...) outside to row bind all elements together.



        df_list <- by(t,id, function(sub)
        unique(transform(sub,
        sum_output = (x %*% y) / sum(y),
        x = NULL,
        y = NULL)
        )
        )

        final_df <- do.call(rbind, df_list)
        row.names(final_df) <- NULL

        final_df

        # id sum
        # 1 14 5
        # 2 15 6
        # 3 16 7
        # 4 17 8
        # 5 18 9





        share|improve this answer













        Consider returning a data frame within your by call and not single-value vector where your calculation is rendered as a new column and x and y columns are removed all using transform(), the function to manipulate columns and then return adjusted data frame.



        Then, to avoid duplicate data (since you are running an aggregation), wrap result in unique(). Finally, since by will return a list of one-row data frames, run a do.call(rbind, ...) outside to row bind all elements together.



        df_list <- by(t,id, function(sub)
        unique(transform(sub,
        sum_output = (x %*% y) / sum(y),
        x = NULL,
        y = NULL)
        )
        )

        final_df <- do.call(rbind, df_list)
        row.names(final_df) <- NULL

        final_df

        # id sum
        # 1 14 5
        # 2 15 6
        # 3 16 7
        # 4 17 8
        # 5 18 9






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 17:04









        ParfaitParfait

        52k84470




        52k84470
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421859%2fwhen-using-the-function-by-in-r-how-do-i-create-a-vector-of-the-factors-for-eac%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

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            Npm cannot find a required file even through it is in the searched directory