I simply need to save df each time it goes through the loop and save it as a csv or something similar!
#Q are matrices and trees are phyllo objects
library(ape)
library(phytools)
for(l in 1:length(Q.all)){
for(k in 1:length(trees)){
tree<- trees[[k]]
Q<- Q.all[[l]]
file1<- sim.history (tree, Q, nsim=10)
df<- getStates(file1, type="tips")
df2 <- as.data.frame(matrix(nrow=nrow(df),ncol=length(file1)))
for(i in 1:length(file1)){  
  for(j in 1:nrow(df)){ 
    x <- df[j,i]
    num <- sum(df[,i] == x) - 1 
    rarity <- 1 - num/(nrow(df)-1)             
    df2[j,i] <- rarity
  }
}
Thanks
