简体   繁体   中英

Looping through a column in R as variable changes

I am a novice trying to analyze trap catch data in R and am looking for an efficient way to loop through by trap line. The first column is trap ID. The second column is the trap line that each trap is associated with. The remaining columns are values related to target catch and bycatch for each visit to the traps. I want to write code that will evaluate the data during each visit for each trap line. Here is an example of data I am working with:

Sample Data:

Data <- structure(list(Trap_ID = c(1L, 2L, 1L, 1L, 2L, 3L), Trapline = c("Cemetery", 
"Cemetery", "Golf", "Church", "Church", "Church"), Target_Visit_1 = c(0L, 
1L, 5L, 0L, 1L, 1L), Bycatch_Visit_1 = c(3L, 2L, 0L, 2L, 1L, 
4L), Target_Visit_2 = c(1L, 1L, 2L, 0L, 1L, 0L), Bycatch_Visit_2 = c(4L, 
2L, 1L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-6L))

The number of traps per trapline varies. I have a code that I wrote out for each Trapline (there are 14 different traplines), but I was hoping there would be a way to consolidate it into one line of code that would calculate values while the trapline was constant, and then when it changed to the next trapline it would start a new calculation. Here is an example of how I was finding the sum of bycatch found at the Cemetery Trapline for visit 1.

CemetaryBycatch1 <- Data %>% select(Bycatch Visit 1 %>% filter(Data$Trapline == "Cemetery")
sum(CemetaryBycatch1)

As of right now I have code like this written out for each trapline for each visit, but with 14 traplines and 8 total visits, I would like to avoid having to write out so many lines of code and was hoping there was a way to loop through it with one block of code that would calculate value (sum, mean, etc.) for each trap line.

Thanks

Does something like this help you?

You can add a filter for Trapline in between group_by and summarise_all .

Code:

library(dplyr)

Data <- structure(list(Trap_ID = c(1L, 2L, 1L, 1L, 2L, 3L), Trapline = c("Cemetery", 
"Cemetery", "Golf", "Church", "Church", "Church"), Target_Visit_1 = c(0L, 
1L, 5L, 0L, 1L, 1L), Bycatch_Visit_1 = c(3L, 2L, 0L, 2L, 1L, 
4L), Target_Visit_2 = c(1L, 1L, 2L, 0L, 1L, 0L), Bycatch_Visit_2 = c(4L, 
2L, 1L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
-6L))
df

Data %>%
  group_by(Trap_ID, Trapline) %>%
  summarise_all(list(sum))

Output:

#> # A tibble: 6 x 6
#> # Groups:   Trap_ID [3]
#>   Trap_ID Trapline Target_Visit_1 Bycatch_Visit_1 Target_Visit_2 Bycatch_Visit_2
#>     <int> <chr>             <int>           <int>          <int>           <int>
#> 1       1 Cemetery              0               3              1               4
#> 2       1 Church                0               2              0               0
#> 3       1 Golf                  5               0              2               1
#> 4       2 Cemetery              1               2              1               2
#> 5       2 Church                1               1              1               1
#> 6       3 Church                1               4              0               0

Created on 2020-10-16 by the reprex package (v0.3.0)

Adding another row to Data :

Trap_ID Trapline Target_Visit_1 Bycatch_Visit_1 Target_Visit_2 Bycatch_Visit_2
1       Cemetery 100            200              1             4

Will give you:


#> # A tibble: 6 x 6
#> # Groups:   Trap_ID [3]
#>   Trap_ID Trapline Target_Visit_1 Bycatch_Visit_1 Target_Visit_2 Bycatch_Visit_2
#>     <int> <chr>             <int>           <int>          <int>           <int>
#> 1       1 Cemetery            100             203              2               8
#> 2       1 Church                0               2              0               0
#> 3       1 Golf                  5               0              2               1
#> 4       2 Cemetery              1               2              1               2
#> 5       2 Church                1               1              1               1
#> 6       3 Church                1               4              0               0

Created on 2020-10-16 by the reprex package (v0.3.0)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM