remove all quotation marks from a data frame

biohazard picture biohazard · Feb 9, 2014 · Viewed 21k times · Source

I have a data frame rep that looks like this:

> head(rep)
     position chrom  value label  
[1,] "17408"  "chr1" "0"   "miRNA"
[2,] "17409"  "chr1" "0"   "miRNA"
[3,] "17410"  "chr1" "0"   "miRNA"
[4,] "17411"  "chr1" "0"   "miRNA"
[5,] "17412"  "chr1" "0"   "miRNA"
[6,] "17413"  "chr1" "0"   "miRNA"

How can I remove the quotation marks from all elements?

Note: rep$position and rep$value should be numeric type, rep$chrom and rep$label should be character type.

Answer

Mark Heckmann picture Mark Heckmann · Feb 9, 2014

Two steps: 1) get rid of quotation marks, 2) convert columns accordingly:

The data

x <- read.table(text='
position chrom  value label  
"\\"17408\\""  "\\"chr1\\"" "\\"0\\""   "\\"miRNA\\""
"\\"17409\\""  "\\"chr1\\"" "\\"0\\""   "\\"miRNA\\""'
, header=T)

1) get rid of quotation marks

library(stringr)
library(plyr)

del <- colwise(function(x) str_replace_all(x, '\"', ""))
x <- del(x)

2) convert columns accordingly

num <- colwise(as.numeric)    
x[c(1,3)] <- num(x[c(1,3)])
x

  position chrom value label
1    17408  chr1     0 miRNA
2    17409  chr1     0 miRNA