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?
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 .
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.
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.
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.
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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With