-1

I have categorical data of behaviours observed for male and female foxes, and I want to count the number of times confident/cautious behaviour was observed if the fox was male and how many times those behaviours were observed if the fox was female.

I tried using an if/then statement but I'm relatively new to R and can't figure out how to make the then statement a count function

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 1
    The most important thing to get adequate help is a minimal reproducible example: See here – TarJae Apr 22 '23 at 22:53

2 Answers2

0

I edited my answer. I don't know if that is your question, but I hope it helps.

First an alternative using data.frame.

foxes = data.frame(
  age = c(5,2,4,6,1,2,4,5,6),
  sex = c("M","M","F","M","F","F","F","F","M"),
  Species = c("S1","S2","S2","S1","S3","S1","S2","S3","S3")
)
foxes
foxes_M = foxes[foxes$sex=="M",]
foxes_M
foxes_M_S1 = foxes[foxes$sex=="M" & foxes$Species=="S1",]
foxes_M_S1

foxes_F = foxes[foxes$sex=="F",]
foxes_F
foxes_F_l3 = foxes[foxes$sex=="F" & foxes$age < 3,]
foxes_F_l3

Now with tidyverse.

library(tidyverse)
foxes = as_tibble(foxes)
foxes_M = foxes |> filter(foxes$sex=="M") 
foxes_M_S1 = foxes |> filter(foxes$sex=="M",foxes$Species=="S1")
  • Thanks! I'm also trying to make an object with all the rows where male is true; there are also several other conditions i want to use to create the object like what the behaviour was. – Andrew McCarthy Apr 22 '23 at 23:39
  • 1
    While possibly correct, a code-only answer helps the person who asked the question, it doesn't do them or future visitors any good. See [Is there any benefit in code-only answers?](https://meta.stackexchange.com/a/148274/183937) – Rohit Gupta Apr 23 '23 at 03:56
0

There are a few ways to achieve your goal using a reproducible example.

# randomly generate data and make a dataframe called df
set.seed(1234)
behavior <- sample(x = c("confident", "cautious"), size = 50, replace = TRUE)
sex <- sample(x = c("male", "female"), size = 50, replace = TRUE)
df <- data.frame(sex, behavior)

You can use Jessica's approach to simply count the number of times two conditions are met using sum and the & operator.

sum(df$sex == "female" & df$behavior == "confident")
#> [1] 7
sum(df$sex == "male" & df$behavior == "cautious")
#> [1] 13

You can also use the table function to very quickly summarize data like this, but be aware that you have to transform it into a dataframe if you want to use it in analyses.

mytable <- table(df)
print(mytable)
#>         behavior
#> sex      cautious confident
#>   female       19         7
#>   male         13        11
class(mytable)
#> [1] "table"

You can also use a dplyr approach to create a new dataframe that summarizes the data based on these two variables, which allows it to keep its dataframe status.

library(dplyr)
mytable2 <- df %>%
  group_by(sex, behavior) %>%
  count()

print(mytable2)
#> # A tibble: 4 × 3
#> # Groups:   sex, behavior [4]
#>   sex    behavior      n
#>   <chr>  <chr>     <int>
#> 1 female cautious     19
#> 2 female confident     7
#> 3 male   cautious     13
#> 4 male   confident    11
class(mytable2)
#> [1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30