ropensci / taxa

taxonomic classes for R
https://docs.ropensci.org/taxa
Other
48 stars 12 forks source link

When adding new table with `mutate_obs` create `taxon_id` column if inputs named by taxon IDs #157

Open zachary-foster opened 6 years ago

zachary-foster commented 6 years ago

This will often happen when making new tables from the output of functions that return per-taxon values, such as obs_apply, supertaxa, subtaxa, subtaxa_apply, etc.

For example, instead of:

obj <- mutate_obs(obj,
                  "per_taxon",
                  taxon_id = taxon_ids,
                  mean_pid = unlist(obs_apply(obj, "query_data", mean, value = "pid")),
                  n_otus = unlist(obs_apply(obj, "query_data", value = "query", func = function(x) length(unique(x)))))

This would do the same:

obj <- mutate_obs(obj,
                  "per_taxon",
                  mean_pid = unlist(obs_apply(obj, "query_data", mean, value = "pid")),
                  n_otus = unlist(obs_apply(obj, "query_data", value = "query", func = function(x) length(unique(x)))))

since the output of unlist(obs_apply(....)) will be named by taxon IDs.

The taxon_id column would only be added if names of input vectors match and are all valid taxon IDs.