Subsetting specific samples from a phyloseq object

Hi all, I am trying to do something that should be really easy, but I can't figure out how to do it.

I have my phyloseq object and I want to extract only few samples and with them I want to create another phyloseq object (because I want to make some plot with only these specific samples and it seems to me the only way to do it).

So I tried:

new_phy_obj <- prune_samples(sample_names(df_with_all_samples) == c("sampletosubset1", "sampletosubset2", "sampletosubset3"), df_with_all_samples)

But I got this message:
Error in validObject(.Object) : invalid class “otu_table” object:
OTU abundance data must have non-zero dimensions.

I also tried:
samples_to_subset <- c("sampletosubset1", "sampletosubset2", "sampletosubset3")
new_phy_obj <- prune_samples(samples_to_subset, df_with_all_samples)

And I tried many other ways, but I always get the same message.

Could somebody help me?

Thanks a lot,

Try this

new_phy_obj  <- phyloseq %>%
  subset_samples(SampleID %in% c("sample1", "sample2", "sample3"))

Two notes.
In R, == works for two variable, %in% works for c() collections with several variables.

Replace SampleID with one of the columns from your phyloseq object sample_data() data frame. Here's how you can see your your metadata columns.

phyloseq %>%
  sample_data() %>%
  as.data.frame() %>%
  head()
1 Like