Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all quotation marks from a data frame

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.

like image 796
biohazard Avatar asked Dec 02 '22 20:12

biohazard


1 Answers

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
like image 191
Mark Heckmann Avatar answered Dec 05 '22 08:12

Mark Heckmann