Julie's R tricks - meyermicrobiolab/Meyer_Lab_Resources GitHub Wiki

Things I am always trying to figure out how to do:

I want to create a new dataframe (dis.treat2), keeping only the rows that are "Montastraea cavernosa" in dataframe "dis.treat"

dis.treat2<-dis.treat[which(dis.treat$Coral=='Montastraea cavernosa'),]

OR

dis.treat2<-dis.treat[grepl("Montastraea cavernosa",dis.treat$Coral),]

Subset more than one group at a time

psHighLow<-subset_samples(ps5acer2, treatment %in% c("0.5","6"))

I want to create a new dataframe (dis.treat3), getting rid of the rows with "Orbicella faveolata"

dis.treat3<-dis.treat[!grepl("Orbicella faveolata",dis.treat$Coral),]

How do I change names of samples within a matrix?

For example, dada2 doesn't like amplicon library sample names with capital "R" in them (because it is parsing sequencing reads R1 and R2). However, I really want to use "R" in my sample names after they are in an otu matrix. In the example below "df" is my dataframe/matrix, "Sample" is the column name, and I want to replace the "D" in all sample names in that column with "R" instead.

df$Sample<-sub("D","R",df$Sample)

How do I change the names of my columns in an R matrix?

names(df)<-sub("old_column_name","new_column_name",names(df))
⚠️ **GitHub.com Fallback** ⚠️