how to print out the contents of a data.frame during for loop

45 views Asked by At

I want to print out the content of a dataframe adj_expression variable. This loops through 1:449 which is the number of genes.

for (i in 1:n_genes) {
        cat(i, "/", n_genes, "\n")
        gene <- genes[i]
        gene_name <- gene_annot$gene_name[gene_annot$gene_id == gene]
        gene_type <- get_gene_type(gene_annot, gene)
        coords <- get_gene_coords(gene_annot, gene)
        cis_gt <- get_cis_genotype(gt_df, snp_annot, coords, cis_window)
        if (all(is.na(cis_gt))) {
          # No snps within window for gene.
          model_summary <- c(gene, gene_name, gene_type, alpha, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
          write(model_summary, file = model_summary_file, append = TRUE, ncol = 24, sep = '\t')
          next
        }
        model_summary <- c(gene, gene_name, gene_type, alpha, ncol(cis_gt), 0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)
        if (ncol(cis_gt) >= 2) {
          expression_vec <- expr_df[,i]
          adj_expression <- adjust_for_covariates(expression_vec, covariates_df)
          adj_expression <- as.matrix(adj_expression[(rownames(adj_expression) %in% rownames(cis_gt)),])
}

I used

write.table(adj_expression,file="s_expr.txt",header=T,sep="\t")

But it only print the last content of i rather than the entire 1:449 length.

0

There are 0 answers