Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: replace column values with string

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))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading