mardi 24 avril 2018

Loop for with if ... else

I'm having a problem with loop for with if ... else ... and I wish someone could help me. I am using commands from the warbler package to download sounds of bird species into the xeno-canto site database. For package details and examples, go to this site: https://cran.r-project.org/web/packages/warbleR/vignettes/warbleR_workflow_phase1.html

Basically, I run the following script:

    # install warbleR from CRAN
# install.packages("warbleR")

# install warbleR from GitHub repository, which contains the latest updates
# install.packages("devtools") # run this if devtools is not already installed
library(devtools)
install_github("maRce10/warbleR")
library(warbleR) # load warbleR into your global environment
# Create a new directory and set your working directory
dir.create(file.path(getwd(),"warbleR_example"))
setwd(file.path(getwd(),"warbleR_example"))
# Check your location
getwd()

# Query xeno-canto for all recordings of the species Crypturellus obsoletus
Cryp.obso <- querxc(qword = "Crypturellus obsoletus", download = FALSE) 
str(Cryp.obso)
# Check resulting data frame
str(Cryp.obso)

Filter by location
Cryp.obso.SP <- Cryp.tata[grep("SP|São Paulo", Cryp.tata$Locality,
                                  ignore.case = FALSE), ]

# Select highest quality recordings
Cryp.obso.SP <- Cryp.obso.SP [Cryp.obso.SP$Quality == "A", ]

# Download sound files
querxc(X = Cryp.obso.SP)

# Save each data frame object as a .csv file 
write.csv(Cryp.obso.SP, " Cryp.obso.SP", row.names = FALSE)

I need to do this for 400 species. So I thought of doing a loop for and running the script for all of them. I have tried two ways, and both of them give an error. Does anyone know where the error is? 1st way

#Download sound files
List <- c("Crypturellus obsoletus", "Crypturellus parvirostris", "Crypturellus tataupa", "Rhynchotus rufescens", "Odontophorus capueira") #In total, there are more than 400 species, but here I put only five.

for (i in List) {
    Espécie <- querxc(qword = i, download = FALSE)
    Espécie.SP <- Espécie[grep("SP|São Paulo", Espécie$Locality,
                                   ignore.case = FALSE), ]
    Espécie.SP <- Espécie.SP[Espécie.SP$Quality == "A", ]
        # Download sound files
    if (length(Espécie.SP) > 0){querxc(X = Espécie.SP)
        } else {print(Espécie)}
                    }

2nd way

#Download sound files
List <- c("Crypturellus obsoletus", "Crypturellus parvirostris", "Crypturellus tataupa", "Rhynchotus rufescens", "Odontophorus capueira") #In total, there are more than 400 species, but here I put only five.

for (i in List) {
  Espécie <- querxc(qword = i, download = FALSE)
  Espécie.SP <- Espécie[grep("SP|São Paulo", Espécie$Locality,
                             ignore.case = FALSE), ]
  Espécie.SP <- Espécie.SP[Espécie.SP$Quality == "A", ]

  # Download sound files
  ifelse (length(Espécie.SP) > 0, querxc(X = Espécie.SP), print(Espécie))
    }

Thanks

Aucun commentaire:

Enregistrer un commentaire