I have the following data frame:
> Mice
Blood States Minute
1 0.875 X0 0.8352569
2 0.875 A2 0.7551901
3 0.625 X0 1.4508139
4 0.625 A1 0.7876343
5 0.375 X0 1.1345252
6 0.125 X0 0.8699363
7 0.375 X0 0.9378742
8 1.125 H1 0.9769522
9 0.625 X0 0.4716321
10 0.875 H1 0.9935999
11 0.625 X0 1.0025917
12 0.375 A1 1.0703999
13 0.375 X0 1.3044854
14 0.875 H1 0.6720436
15 0.875 A1 1.0431863
So every mouse has some value of drugs in their "Blood", and their "State" is checked. This is just a piece of my data frame, but the mice can be in 4 different states. "Minute" is whenever something occurs to the mice, does not matter what.
For every value of "Blood", the mice can be in either of the 4 different states, and I want to count how many observations I have in each category.
The count() function with both columns Blood and States did not work because "States" is a factor column
To operate on factor levels, you can use tapply
or by
. If you have discrete scale for Mice$Blood
, convert it to a factor as well:
> by(mice$States, as.factor(mice$Blood), function(x) summary(factor(x)))
as.factor(mice$Blood): 0.125
X0
1
------------------------------------------------------------------------------------------------
as.factor(mice$Blood): 0.375
A1 X0
1 3
------------------------------------------------------------------------------------------------
as.factor(mice$Blood): 0.625
A1 X0
1 3
------------------------------------------------------------------------------------------------
as.factor(mice$Blood): 0.875
A1 A2 H1 X0
1 1 2 1
------------------------------------------------------------------------------------------------
as.factor(mice$Blood): 1.125
H1
1
The returned object is a list, so you may capture it and use for your purposes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.