for (compound in compounds) { compound_info <- kegg_find("compound", compound) if(length(compound_info$ORGANISM) > 0) { # The compound has at least one associated organism organisms[[compound]] <- compound_info$ORGANISM } else { # The compound has no associated organisms organisms[[compound]] <- NA } }
The function loop takes a table as an argument (compounds), and returns a table with the same number of rows as the original table (organisms). The function loops through the compounds table, for each row (compound), it runs the KEGG function (compound_info <- KEGG_find(compound,"compound")), this KEGG function returns a table with several columns. The if statement checks if the compound has more than one organism associated with it, if it does then the organism is retrieved and added to the table, if not it is added as NA.