Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 749
Metsfan Avatar asked Jul 12 '19 17:07

Metsfan


People also ask

How do you count the number of items in a list?

Count items in list. Generic formula = COUNTIFS (A:A, A1, B:B, B1) Summary . To create a count of the values that appear in in a list or table, you can use the COUNTIFS function. In the example shown, the formula in D5 is: = COUNTIFS (B:B, B5, C:C, C5) Explanation .

How to count cells in a list or Excel table column?

Count cells in a list or Excel table column by using the SUBTOTAL function. Use the SUBTOTAL function to count the number of values in an Excel table or range of cells.

How do you count the number of values in a range?

You can count the number of values in a range or table by using a simple formula, clicking a button, or by using a worksheet function. Excel can also display the count of the number of selected cells on the Excel status bar.

How do I create a count of the values in Excel?

To create a count of the values that appear in in a list or table, you can use the COUNTIFS function. In the example shown, the formula in D5 is: How this formula works. The COUNTIFS function takes range/criteria pairs, and delivers a count when all criteria match. This example, contains two range/criteria pairs.


3 Answers

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
like image 88
M-- Avatar answered Nov 15 '22 00:11

M--


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
like image 32
Parfait Avatar answered Nov 14 '22 22:11

Parfait


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
like image 25
acylam Avatar answered Nov 15 '22 00:11

acylam