How do I get count of number of items in selection?How to sort a dataframe by multiple column(s)Calculating the occurrences of numbers in the subsets of a data.frameHow to make a great R reproducible exampleSelecting a range of rows from R data frameSubsetting rows with logical comparisons when missing observations are presentSelecting Specific Dates in Rcounting number of observations into a dataframeR subset data.frame columns by group to maximize row valuessubset by at least two out of multiple conditionsCounting Duplicates without aggregation or group_by

Are there foods that astronauts are explicitly never allowed to eat?

Quickest way to move a line in a text file before another line in a text file?

is FIND WORDS in P?

Calculating Fibonacci sequence in several different ways

Substitute dried pig's blood for fresh

Why do we need an estimator to be consistent?

Langton's Ant Periodic Behavior

Can't call helper function from helper inside list forEach

Can I make Ubuntu 18.04 switch between multiple windows of the program by just clicking the icon?

On a Gameboy, what happens when attempting to read/write external RAM while RAM is disabled?

Is it better to merge "often" or only after completion do a big merge of feature branches?

What kind of curve (or model) should I fit to my percentage data?

Do I have to mention my main character's age?

Acoustic guitar chords' positions vs those of a Bass guitar

Book in which the "mountain" in the distance was a hole in the flat world

Claiming statutory warranty for a fault that resulted in the loss of the product

Did Don Young threaten John Boehner with a 10 inch blade to the throat?

How can I show that the speed of light in vacuum is the same in all reference frames?

Host telling me to cancel my booking in exchange for a discount?

Why do the top heroes in Boku no Hero Academia only come from Japan?

Connect to client FTP with dynamic client IP address

Why is there an extra "t" in Lemmatization?

"It is what it is"

How do you structure large embedded projects?



How do I get count of number of items in selection?


How to sort a dataframe by multiple column(s)Calculating the occurrences of numbers in the subsets of a data.frameHow to make a great R reproducible exampleSelecting a range of rows from R data frameSubsetting rows with logical comparisons when missing observations are presentSelecting Specific Dates in Rcounting number of observations into a dataframeR subset data.frame columns by group to maximize row valuessubset by at least two out of multiple conditionsCounting Duplicates without aggregation or group_by






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








6















I want to list in array format how many in each Diet group (there are four) have Time > 21.



I have tried to solve this in RStudio.



data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)


In order to find how many observations are in newdata, I used
nrow(newdata),
but I would like to find out how many observations meet the criteria just by making it a part of this expression:



newdata <- subset(ChickWeight, Time >= 21, select=Diet) 


so that when I display newdata the table will also contain the number of observations that meet the criteria in a new column.



Desire output:



Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100


Is there a way to do that?










share|improve this question
























  • and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

    – avid_useR
    Jul 12 at 17:13












  • I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

    – Metsfan
    Jul 12 at 17:19












  • Please post your desired output in the question body itself

    – avid_useR
    Jul 12 at 17:20











  • So basically you want to get the obs count for each Diet group?

    – avid_useR
    Jul 12 at 17:25

















6















I want to list in array format how many in each Diet group (there are four) have Time > 21.



I have tried to solve this in RStudio.



data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)


In order to find how many observations are in newdata, I used
nrow(newdata),
but I would like to find out how many observations meet the criteria just by making it a part of this expression:



newdata <- subset(ChickWeight, Time >= 21, select=Diet) 


so that when I display newdata the table will also contain the number of observations that meet the criteria in a new column.



Desire output:



Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100


Is there a way to do that?










share|improve this question
























  • and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

    – avid_useR
    Jul 12 at 17:13












  • I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

    – Metsfan
    Jul 12 at 17:19












  • Please post your desired output in the question body itself

    – avid_useR
    Jul 12 at 17:20











  • So basically you want to get the obs count for each Diet group?

    – avid_useR
    Jul 12 at 17:25













6












6








6








I want to list in array format how many in each Diet group (there are four) have Time > 21.



I have tried to solve this in RStudio.



data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)


In order to find how many observations are in newdata, I used
nrow(newdata),
but I would like to find out how many observations meet the criteria just by making it a part of this expression:



newdata <- subset(ChickWeight, Time >= 21, select=Diet) 


so that when I display newdata the table will also contain the number of observations that meet the criteria in a new column.



Desire output:



Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100


Is there a way to do that?










share|improve this question
















I want to list in array format how many in each Diet group (there are four) have Time > 21.



I have tried to solve this in RStudio.



data(ChickWeight)
newdata <- subset(ChickWeight, Time >= 21, select=Diet)


In order to find how many observations are in newdata, I used
nrow(newdata),
but I would like to find out how many observations meet the criteria just by making it a part of this expression:



newdata <- subset(ChickWeight, Time >= 21, select=Diet) 


so that when I display newdata the table will also contain the number of observations that meet the criteria in a new column.



Desire output:



Diet Number Observations
1 200 (I just created the numbers for this column as examples)
2 75
3 150
4 100


Is there a way to do that?







r subset frequency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 12 at 17:36









M-M

10.6k6 gold badges27 silver badges50 bronze badges




10.6k6 gold badges27 silver badges50 bronze badges










asked Jul 12 at 17:04









MetsfanMetsfan

535 bronze badges




535 bronze badges












  • and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

    – avid_useR
    Jul 12 at 17:13












  • I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

    – Metsfan
    Jul 12 at 17:19












  • Please post your desired output in the question body itself

    – avid_useR
    Jul 12 at 17:20











  • So basically you want to get the obs count for each Diet group?

    – avid_useR
    Jul 12 at 17:25

















  • and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

    – avid_useR
    Jul 12 at 17:13












  • I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

    – Metsfan
    Jul 12 at 17:19












  • Please post your desired output in the question body itself

    – avid_useR
    Jul 12 at 17:20











  • So basically you want to get the obs count for each Diet group?

    – avid_useR
    Jul 12 at 17:25
















and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

– avid_useR
Jul 12 at 17:13






and the obs count would be a repeating number in a different column of newdata? What about newdata$obs_count <- nrow(newdata)?

– avid_useR
Jul 12 at 17:13














I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

– Metsfan
Jul 12 at 17:19






I would like it displayed this way: Diet Number Observations 1 200 (what # is) 2 300 (what # is) 3 75 (what # is) 4 25 (what # is) avid_useR: When I ran yours, I got NULL.

– Metsfan
Jul 12 at 17:19














Please post your desired output in the question body itself

– avid_useR
Jul 12 at 17:20





Please post your desired output in the question body itself

– avid_useR
Jul 12 at 17:20













So basically you want to get the obs count for each Diet group?

– avid_useR
Jul 12 at 17:25





So basically you want to get the obs count for each Diet group?

– avid_useR
Jul 12 at 17:25












4 Answers
4






active

oldest

votes


















5














It can be done in base:



transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))

#> Diet Freq
#> 1 1 16
#> 2 2 10
#> 3 3 10
#> 4 4 9





share|improve this answer























  • M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

    – Metsfan
    Jul 12 at 17:56












  • @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

    – M-M
    Jul 12 at 18:01











  • When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

    – Metsfan
    Jul 12 at 18:09











  • @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

    – M-M
    Jul 12 at 18:12











  • Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

    – Metsfan
    Jul 12 at 18:17


















1














Consider a straightforward aggregate after the subset call:



newdata <- subset(ChickWeight, Time >= 21, select=Diet)

aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)

# Diet Obs
# 1 1 16
# 2 2 10
# 3 3 10
# 4 4 9





share|improve this answer






























    1














    We can do this with summarize from dplyr:



    library(dplyr)

    newdata %>%
    group_by(Diet) %>%
    summarize(Num_Obs = n())


    We can even combine the subset to a single dplyr workflow:



    ChickWeight %>%
    filter(Time >= 21) %>%
    group_by(Diet) %>%
    summarize(Num_Obs = n())


    Output:



    # A tibble: 4 x 2
    Diet Num_Obs
    <fct> <int>
    1 1 16
    2 2 10
    3 3 10
    4 4 9





    share|improve this answer
































      0














      Here is a data table approach



      library(data.table)
      df <- as.data.table(ChickWeight)

      df[Time >= 21, .(Number = .N), by = Diet]
      # Diet Number
      # 1: 1 16
      # 2: 2 10
      # 3: 3 10
      # 4: 4 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%2f57011287%2fhow-do-i-get-count-of-number-of-items-in-selection%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        5














        It can be done in base:



        transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))

        #> Diet Freq
        #> 1 1 16
        #> 2 2 10
        #> 3 3 10
        #> 4 4 9





        share|improve this answer























        • M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

          – Metsfan
          Jul 12 at 17:56












        • @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

          – M-M
          Jul 12 at 18:01











        • When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

          – Metsfan
          Jul 12 at 18:09











        • @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

          – M-M
          Jul 12 at 18:12











        • Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

          – Metsfan
          Jul 12 at 18:17















        5














        It can be done in base:



        transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))

        #> Diet Freq
        #> 1 1 16
        #> 2 2 10
        #> 3 3 10
        #> 4 4 9





        share|improve this answer























        • M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

          – Metsfan
          Jul 12 at 17:56












        • @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

          – M-M
          Jul 12 at 18:01











        • When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

          – Metsfan
          Jul 12 at 18:09











        • @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

          – M-M
          Jul 12 at 18:12











        • Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

          – Metsfan
          Jul 12 at 18:17













        5












        5








        5







        It can be done in base:



        transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))

        #> Diet Freq
        #> 1 1 16
        #> 2 2 10
        #> 3 3 10
        #> 4 4 9





        share|improve this answer













        It can be done in base:



        transform(table(Diet=subset(ChickWeight, Time >= 21, select=Diet)))

        #> Diet Freq
        #> 1 1 16
        #> 2 2 10
        #> 3 3 10
        #> 4 4 9






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 12 at 17:29









        M-MM-M

        10.6k6 gold badges27 silver badges50 bronze badges




        10.6k6 gold badges27 silver badges50 bronze badges












        • M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

          – Metsfan
          Jul 12 at 17:56












        • @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

          – M-M
          Jul 12 at 18:01











        • When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

          – Metsfan
          Jul 12 at 18:09











        • @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

          – M-M
          Jul 12 at 18:12











        • Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

          – Metsfan
          Jul 12 at 18:17

















        • M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

          – Metsfan
          Jul 12 at 17:56












        • @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

          – M-M
          Jul 12 at 18:01











        • When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

          – Metsfan
          Jul 12 at 18:09











        • @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

          – M-M
          Jul 12 at 18:12











        • Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

          – Metsfan
          Jul 12 at 18:17
















        M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

        – Metsfan
        Jul 12 at 17:56






        M-M, thanks. It works. What is the purpose of "table"? Why is it needed?

        – Metsfan
        Jul 12 at 17:56














        @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

        – M-M
        Jul 12 at 18:01





        @Metsfan You can read about it by running ?table(). In short, table gives a cross tab with frequencies. I am just transforming it later to change the direction of output (run the code without transform to see). You should run table on couple more dataframes that you have to know what it does better.

        – M-M
        Jul 12 at 18:01













        When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

        – Metsfan
        Jul 12 at 18:09





        When I ran it without transform I got this error: "Error in subset.data.frame(ChickWeight, select = Diet, weight) : 'subset' must be logical"

        – Metsfan
        Jul 12 at 18:09













        @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

        – M-M
        Jul 12 at 18:12





        @Metsfan Are you assigning that to a column or something? For testing, just run that line without anything else before or after: table(Diet=subset(ChickWeight, Time >= 21, select=Diet))

        – M-M
        Jul 12 at 18:12













        Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

        – Metsfan
        Jul 12 at 18:17





        Okay, now it worked. I noticed that it converted the rows into columns. Interesting. Thanks again.

        – Metsfan
        Jul 12 at 18:17













        1














        Consider a straightforward aggregate after the subset call:



        newdata <- subset(ChickWeight, Time >= 21, select=Diet)

        aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)

        # Diet Obs
        # 1 1 16
        # 2 2 10
        # 3 3 10
        # 4 4 9





        share|improve this answer



























          1














          Consider a straightforward aggregate after the subset call:



          newdata <- subset(ChickWeight, Time >= 21, select=Diet)

          aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)

          # Diet Obs
          # 1 1 16
          # 2 2 10
          # 3 3 10
          # 4 4 9





          share|improve this answer

























            1












            1








            1







            Consider a straightforward aggregate after the subset call:



            newdata <- subset(ChickWeight, Time >= 21, select=Diet)

            aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)

            # Diet Obs
            # 1 1 16
            # 2 2 10
            # 3 3 10
            # 4 4 9





            share|improve this answer













            Consider a straightforward aggregate after the subset call:



            newdata <- subset(ChickWeight, Time >= 21, select=Diet)

            aggregate(cbind(Obs=Diet) ~ Diet, newdata, FUN=length)

            # Diet Obs
            # 1 1 16
            # 2 2 10
            # 3 3 10
            # 4 4 9






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 12 at 17:29









            ParfaitParfait

            59k10 gold badges55 silver badges76 bronze badges




            59k10 gold badges55 silver badges76 bronze badges





















                1














                We can do this with summarize from dplyr:



                library(dplyr)

                newdata %>%
                group_by(Diet) %>%
                summarize(Num_Obs = n())


                We can even combine the subset to a single dplyr workflow:



                ChickWeight %>%
                filter(Time >= 21) %>%
                group_by(Diet) %>%
                summarize(Num_Obs = n())


                Output:



                # A tibble: 4 x 2
                Diet Num_Obs
                <fct> <int>
                1 1 16
                2 2 10
                3 3 10
                4 4 9





                share|improve this answer





























                  1














                  We can do this with summarize from dplyr:



                  library(dplyr)

                  newdata %>%
                  group_by(Diet) %>%
                  summarize(Num_Obs = n())


                  We can even combine the subset to a single dplyr workflow:



                  ChickWeight %>%
                  filter(Time >= 21) %>%
                  group_by(Diet) %>%
                  summarize(Num_Obs = n())


                  Output:



                  # A tibble: 4 x 2
                  Diet Num_Obs
                  <fct> <int>
                  1 1 16
                  2 2 10
                  3 3 10
                  4 4 9





                  share|improve this answer



























                    1












                    1








                    1







                    We can do this with summarize from dplyr:



                    library(dplyr)

                    newdata %>%
                    group_by(Diet) %>%
                    summarize(Num_Obs = n())


                    We can even combine the subset to a single dplyr workflow:



                    ChickWeight %>%
                    filter(Time >= 21) %>%
                    group_by(Diet) %>%
                    summarize(Num_Obs = n())


                    Output:



                    # A tibble: 4 x 2
                    Diet Num_Obs
                    <fct> <int>
                    1 1 16
                    2 2 10
                    3 3 10
                    4 4 9





                    share|improve this answer















                    We can do this with summarize from dplyr:



                    library(dplyr)

                    newdata %>%
                    group_by(Diet) %>%
                    summarize(Num_Obs = n())


                    We can even combine the subset to a single dplyr workflow:



                    ChickWeight %>%
                    filter(Time >= 21) %>%
                    group_by(Diet) %>%
                    summarize(Num_Obs = n())


                    Output:



                    # A tibble: 4 x 2
                    Diet Num_Obs
                    <fct> <int>
                    1 1 16
                    2 2 10
                    3 3 10
                    4 4 9






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jul 12 at 19:46

























                    answered Jul 12 at 17:27









                    avid_useRavid_useR

                    14.2k4 gold badges20 silver badges33 bronze badges




                    14.2k4 gold badges20 silver badges33 bronze badges





















                        0














                        Here is a data table approach



                        library(data.table)
                        df <- as.data.table(ChickWeight)

                        df[Time >= 21, .(Number = .N), by = Diet]
                        # Diet Number
                        # 1: 1 16
                        # 2: 2 10
                        # 3: 3 10
                        # 4: 4 9





                        share|improve this answer



























                          0














                          Here is a data table approach



                          library(data.table)
                          df <- as.data.table(ChickWeight)

                          df[Time >= 21, .(Number = .N), by = Diet]
                          # Diet Number
                          # 1: 1 16
                          # 2: 2 10
                          # 3: 3 10
                          # 4: 4 9





                          share|improve this answer

























                            0












                            0








                            0







                            Here is a data table approach



                            library(data.table)
                            df <- as.data.table(ChickWeight)

                            df[Time >= 21, .(Number = .N), by = Diet]
                            # Diet Number
                            # 1: 1 16
                            # 2: 2 10
                            # 3: 3 10
                            # 4: 4 9





                            share|improve this answer













                            Here is a data table approach



                            library(data.table)
                            df <- as.data.table(ChickWeight)

                            df[Time >= 21, .(Number = .N), by = Diet]
                            # Diet Number
                            # 1: 1 16
                            # 2: 2 10
                            # 3: 3 10
                            # 4: 4 9






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jul 12 at 19:37









                            IceCreamToucanIceCreamToucan

                            13.3k1 gold badge8 silver badges19 bronze badges




                            13.3k1 gold badge8 silver badges19 bronze badges



























                                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%2f57011287%2fhow-do-i-get-count-of-number-of-items-in-selection%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