0

I have a set of BAM files within the chr16_bam directory and a sgseq_sam.txt file. I want to replace the file_bam column values with the full path where the BAM files are stored.

My code hasn't been able to achieve that.

bamPath = "C:/Users/User/Downloads/chr16_bam/"
samFile <- read.delim("C:/Users/User/Downloads/sgseq_sam.txt", header=T) 

for (i in samFile[,2]) {
  p <- gsub(i, bamPath, samFile)
}

> dput(samFile)
structure(list(sample_name = c("N60", "N11", "T132", "T114"), 
    file_bam = c("60.bam", "11.bam", "132.bam", "114.bam"), paired_end = c(TRUE, 
    TRUE, TRUE, TRUE), read_length = c(75L, 75L, 75L, 75L), frag_length = c(1075L, 
    1466L, 946L, 1154L), lib_size = c(2589976L, 5153522L, 4429912L, 
    3131400L)), class = "data.frame", row.names = c(NA, -4L))

1 Answer 1

1
library(tidyverse)

sam_file <- sam_file %>% 
  mutate(file_bam = paste0(bamPath, file_bam))

Or, alternately, in base R:

data_file$file_bam <- paste0(bamPath, data_file$file_bam)

Sign up to request clarification or add additional context in comments.

2 Comments

The tidyverse code didn't change the column values in the samFile
Did you save the results back to sam_file? I should have specified that and have edited my response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.