I have two tables, ind that has the total number of individuals in each transect, and then Families, which has the number of individuals in each family in each transect.
> ind
# A tibble: 55 × 2
Transect count
<dbl> <int>
1 50 31
2 51 17
3 52 33
4 54 9
5 55 46
6 56 70
7 57 112
8 58 6
9 59 15
10 60 16
# … with 45 more rows
# ℹ Use `print(n = ...)` to see more rows
> Families
# A tibble: 222 × 3
# Groups: Transect [55]
Transect Family count
<dbl> <chr> <int>
1 50 Anatidae 4
2 50 Laridae 25
3 50 Sulidae 1
4 50 Unidentified 1
5 51 Alcidae 3
6 51 Anatidae 3
7 51 Laridae 9
8 51 Sulidae 2
9 52 Alcidae 3
10 52 Anatidae 1
# … with 212 more rows
# ℹ Use `print(n = ...)` to see more rows
I am trying to use an if statement to calculate the proportion of individuals in each family in each transect. So for example if we were looking at that first row in the Families table, I would want it to go "that row is transect 50, ind table says transect 50 has 31 total individuals, we divide 4 by 31" This is the code I have
ind
Families
h.data<-if(ind$Transect==Families$Transect) {data.frame(Families$count/ind$count)}
But I keep getting this error:
> h.data<-if(ind$Transect==Families$Transect) {data.frame(Families$count/ind$count)}
Error in if (ind$Transect == Families$Transect) { :
the condition has length > 1
In addition: Warning message:
In ind$Transect == Families$Transect :
longer object length is not a multiple of shorter object length
I've tried reformatting the if statement to various if_else statements as well, but keep getting the same error. I suppose at this point I either need to use some other code to do this, or figure out a way to make the length of both of the tables multiples of each other, but I'm not sure how to do that. I did look at solutions given for similar situations in other posts, but I couldn't quite figure out how to apply them to my code.