running loop with my own function

remove_animal <- c("Ant", "Ann", "Jim", "Rita")

animal_function <- function(select_animal) {
  
 animal_df <- all_animals %>%
  filter((!animal_name %in% remove_animal & animal_name != select_animal),
         animal_type != "cow")
 
 return(animal_df)
 }


animal_data_loop <- lapply(select_animal = remove_animal, animal_function)

I want to run the function I created through a loop for each element within the remove animal list I created. each element should be a different dataframe. any help would be so so appreciated.

here is the data sample

structure(list(animal_type = c("cow", "dog", "dog", "dog", "cow", 
"monkey", "giraffe", "giraffe", "cow"), animal_name = c("Ant", 
"Sophia", "Ann", "Jill", "Jim", "Jim", "Rita", "Anna", "Rita"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-9L))

Hello @help ,

I have tried to address your question. If I have understood correctly, you need 4 different data frames after looping through the function you have written. Please see the below code and output, it may be of some use.

> library(tidyverse)
> 
> # data from post
> 
> structure(list(animal_type = c("cow", "dog", "dog", "dog", "cow", 
+                                "monkey", "giraffe", "giraffe", "cow"), 
+                animal_name = c("Ant","Sophia", "Ann", "Jill", "Jim", "Jim", 
+                                "Rita", "Anna", "Rita"
+                                )), 
+           class = c("tbl_df", "tbl", "data.frame"), 
+           row.names = c(NA, -9L)) ->  all_animals
> 
> 
> remove_animal <- c("Ant", "Ann", "Jim", "Rita")
> 
> 
> animal_function <- function(select_animal) {
+   
+   animal_df <- all_animals %>%
+     filter((!animal_name %in% remove_animal & animal_name != select_animal), # this condition is written in a manner that gegnerates the samem out put no matter what is provided as an input for select_animal
+            animal_type != "cow")
+   
+   return(animal_df)
+ }
> 
> 
> ## Possible solutions/approaches
> 
> lapply(X = remove_animal,FUN = animal_function) # returns a list of data, you were super close 
[[1]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[2]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[3]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[4]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

> 
> purrr::map(remove_animal,animal_function) # does the same using purrr
[[1]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[2]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[3]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[4]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

> 
> ## using a loop
> 
> list_of_df <-  vector(mode='list', length=length(remove_animal)) # create an empty list 
> 
> for (i in seq_along(remove_animal)) {
+   
+   list_of_df[[i]] <- animal_function(remove_animal[i])
+   
+ }
> 
> list_of_df
[[1]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[2]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[3]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

[[4]]
# A tibble: 3 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 giraffe     Anna       

> 
> ## maybe this is the function that works better
> 
> 
> animal_function2 <- function(select_animal) {
+   
+   animal_df <- all_animals %>%
+     filter((animal_name != select_animal), # edited condition
+            animal_type != "cow")
+   
+   return(animal_df)
+ }
> 
> lapply(X = remove_animal,FUN = animal_function2) 
[[1]]
# A tibble: 6 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Rita       
6 giraffe     Anna       

[[2]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 monkey      Jim        
4 giraffe     Rita       
5 giraffe     Anna       

[[3]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 giraffe     Rita       
5 giraffe     Anna       

[[4]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Anna       

> 
> purrr::map(remove_animal,animal_function2) # does the same using purrr
[[1]]
# A tibble: 6 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Rita       
6 giraffe     Anna       

[[2]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 monkey      Jim        
4 giraffe     Rita       
5 giraffe     Anna       

[[3]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 giraffe     Rita       
5 giraffe     Anna       

[[4]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Anna       

> 
> ## using a loop
> 
> list_of_df <-  vector(mode='list', length=length(remove_animal)) # create an empty list 
> 
> for (i in seq_along(remove_animal)) {
+   
+   list_of_df[[i]] <- animal_function2(remove_animal[i])
+   
+ }
> 
> list_of_df
[[1]]
# A tibble: 6 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Rita       
6 giraffe     Anna       

[[2]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Jill       
3 monkey      Jim        
4 giraffe     Rita       
5 giraffe     Anna       

[[3]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 giraffe     Rita       
5 giraffe     Anna       

[[4]]
# A tibble: 5 × 2
  animal_type animal_name
  <chr>       <chr>      
1 dog         Sophia     
2 dog         Ann        
3 dog         Jill       
4 monkey      Jim        
5 giraffe     Anna       


Hope this helps,
Ayush

this is so helpful. but im not getting the desired output with my filter which is upsetting. :frowning: I essentially want to remove observations with animal_name as "Ann", "Jim", or "Rita" in the first dataframe. in the second dataframe, I want to remove observations with animal_name as "Ant", "Jim", or "Rita"... so on and so forth. ur assistance on editing the filter would be so helpful.

The problem appears still to be underspecified. Here is one interpretation

d <- data.frame(animal_type = c(
  "cow", "dog", "dog", "dog", "cow",
  "monkey", "giraffe", "giraffe", "cow"
), animal_name = c(
  "Ant",
  "Sophia", "Ann", "Jill", "Jim", "Jim", "Rita", "Anna", "Rita"
))

d
#>   animal_type animal_name
#> 1         cow         Ant
#> 2         dog      Sophia
#> 3         dog         Ann
#> 4         dog        Jill
#> 5         cow         Jim
#> 6      monkey         Jim
#> 7     giraffe        Rita
#> 8     giraffe        Anna
#> 9         cow        Rita
drops <- c("Ant", "Ann", "Jim", "Rita")
kine <- "cow"

# no cows anywhere, which also knocks out Ant
tip_cow <- function(x) x[which(x[1] != kine),]
tip_cow(d)
#>   animal_type animal_name
#> 2         dog      Sophia
#> 3         dog         Ann
#> 4         dog        Jill
#> 6      monkey         Jim
#> 7     giraffe        Rita
#> 8     giraffe        Anna
# no names not in drop list (i.e., Sophie)
keepers <- function(x) x[which(x[2][[1]] %in% drops),]
keepers(d)
#>   animal_type animal_name
#> 1         cow         Ant
#> 3         dog         Ann
#> 5         cow         Jim
#> 6      monkey         Jim
#> 7     giraffe        Rita
#> 9         cow        Rita
# no cows and only names in the drop list
tip_cow(d) |> keepers(x = _)# separate into dataframes
#>   animal_type animal_name
#> 3         dog         Ann
#> 6      monkey         Jim
#> 7     giraffe        Rita
# splits, too lazy to parameterize d
herd <- function(x) (tip_cow(d) |> keepers(x = _))[x,]
herd(1)
#>   animal_type animal_name
#> 3         dog         Ann
# list of three data frames; to lazy to parameterize 1:3
lapply(1:3,herd)
#> [[1]]
#>   animal_type animal_name
#> 3         dog         Ann
#> 
#> [[2]]
#>   animal_type animal_name
#> 6      monkey         Jim
#> 
#> [[3]]
#>   animal_type animal_name
#> 7     giraffe        Rita

Created on 2023-01-04 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.