0

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.

Olivia
  • 1
  • 1
  • 1
    You need to join the data before your calculation, `left_join(Families, rename(ind, transect_count = count), by = "Transect")`. – Gregor Thomas Feb 02 '23 at 18:23
  • Your title/subject says `ifelse` but your code never uses it. Perhaps there's some confusion here? – r2evans Feb 02 '23 at 18:29
  • I'm guessing though that `nrow(Families)` and `nrow(ind)` are different, which leads to Gregor's comment about needing to join them before attempting to do that division. For example, what should happen if I try `c(1,2,3) / c(1,2,3,4,5,6,7)`? At best it errors, at worst it recycles and gives us incorrect results (see `1:2 / 1:4`). – r2evans Feb 02 '23 at 18:30
  • 1
    @akrun the join solved it! You guys are awesome! – Olivia Feb 02 '23 at 18:36
  • I thought it was @GregorThomas who suggested the join. (At least, Gregor is the only one who didn't delete their comments.) – r2evans Feb 02 '23 at 18:38
  • @r2evans Yeah, Gregor also suggested it, but akrun had the exact code I tried. Must've deleted the comment right after I posted – Olivia Feb 02 '23 at 18:41
  • 1
    Akrun's edit to include the join and my comment went through about the same time. – Gregor Thomas Feb 02 '23 at 18:44
  • There are _so many_ confusing chains of comments because of that, some users are certainly averse to leave comments around for more than a minute or two (often seems counter-productive to me \*shrug\*). – r2evans Feb 02 '23 at 18:44

0 Answers0