Home > Blockchain >  R: replace column values with string
R: replace column values with string

Time:02-16

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

CodePudding user response:

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)

  •  Tags:  
  • r
  • Related