Remove Quotes from a Character Vector in R

Remove quotes from a character vector in R

as.name(char[1]) will work, although I'm not sure why you'd ever really want to do this -- the quotes won't get carried over in a paste for example:

> paste("I am counting to", char[1], char[2], char[3])
[1] "I am counting to one two three"

Remove quotes from a character object in R

Assuming s is as shown below and you want to iterate over three character strings:

s <- "c('r1', 'r2', 'r3')"

rr <- eval(parse(text = s)) ; rr
## [1] "r1" "r2" "r3"

for(r in rr) print(r) # can use it in a for loop like this
## [1] "r1"
## [1] "r2"
## [1] "r3"

or if what you meant is that r1, r2 and r3 are R variables then using rr from above:

r1 <- 1; r2 <- 2; r3 <- 3
L <- mget(rr) # list with elements 1, 2 and 3
for(r in L) print(r)
## [1] 1
## [1] 2
## [1] 3

There may be some earlier step you can take to create an object easier to deal with in the first place but that can't be determined from the question as stated.

Remove double quote \ symbol from string

You can try this. Note that what you actually want is to remove \", not "\ (as proposed in the unedited version of your question). The first " you need to represent each element in the character.

gsub('[\"]', '', data)

Remove quotes from a character vector in R?

You can use stringi for this. We can use the ICU metacharacter \\P to negate the matched values and -- to subtract the negation of the comma.

library(stringi)
mcv <- c("version", "of", "mackinnon’s", "“dominance", "approach,”")
stri_replace_all_regex(mcv, "[\\P{Ll}--,]", "")
# [1] "version" "of" "mackinnons" "dominance" "approach,"

I'm just learning ICU, but I think that's the right expression to use.

Remove quotes () from a data.frame in R

The print() method for data frames has an option quote=, which you can set to FALSE:

print.data.frame(data.frame(x=c("Hello", "World")), 
quote=FALSE)
# x
# 1 Hello
# 2 World

See also ?print.data.frame(= help)

Edit:

With regards to the dputed data in the comment below:

as.data.frame(sapply(df, function(x) gsub("\"", "", x)))

Use regular expressions to remove quotation marks in R data frame

grep only searches for matches within each element of a character vector, if you want to replace the first occurrence of a specific pattern, use sub or gsub to replace all the occurrences.

Example:

> h <- data.frame(age = c(20,21), country = c('AU"', 'AU"'))
> h$country <- gsub('"', '', h$country)
> h

# age country
# 1 20 AU
# 2 21 AU

How to remove quotes in string values in displaying a matrix?

(Solved by docendo discimus)

To employ docendo's trick, I added "ADFtable <- as.data.frame(ADFtable)" line in the code:

ADFtable <- cbind(LevelADFtable, FirstDiffADFtable, Result) # 18 x 18
ADFtable <- as.data.frame(ADFtable)

It resulted with no quotes then. Thx a lot, docendo.

remove all quotation marks from a data frame

As indicated by @Roland, you have a matrix, not a data.frame, and these have different default print methods. Sticking with a matrix, you can set quote = FALSE explicitly in print or you can use noquote.

Here is a basic example:

## Sample data
x <- matrix(c(17, "chr1", 0, "miRNA", 18, "chr1", 0, "miRNA"), nrow = 2,
byrow = TRUE, dimnames = list(
NULL, c("position", "chrom", "value", "label")))

## Default printing
x
# position chrom value label
# [1,] "17" "chr1" "0" "miRNA"
# [2,] "18" "chr1" "0" "miRNA"

## Two options to make the quotes disappear
print(x, quote = FALSE)
# position chrom value label
# [1,] 17 chr1 0 miRNA
# [2,] 18 chr1 0 miRNA
noquote(x)
# position chrom value label
# [1,] 17 chr1 0 miRNA
# [2,] 18 chr1 0 miRNA

Also, as you figured out on your own, converting your matrix to a data.frame makes the quotes disappear. A data.frame is a more appropriate structure to hold your data if each column is a different type of data (numeric, character, factor, and so on). However, converting a matrix to a data.frame does not take care of the conversion of columns for you automatically. Instead, you can make use of type.convert (which is also used when creating a data.frame using read.table and family):

y <- data.frame(x, stringsAsFactors = FALSE)
str(y)
# 'data.frame': 2 obs. of 4 variables:
# $ position: chr "17" "18"
# $ chrom : chr "chr1" "chr1"
# $ value : chr "0" "0"
# $ label : chr "miRNA" "miRNA"
y[] <- lapply(y, type.convert)
str(y)
# 'data.frame': 2 obs. of 4 variables:
# $ position: int 17 18
# $ chrom : Factor w/ 1 level "chr1": 1 1
# $ value : int 0 0
# $ label : Factor w/ 1 level "miRNA": 1 1
y
# position chrom value label
# 1 17 chr1 0 miRNA
# 2 18 chr1 0 miRNA

remove quotes from string on only numeric values in r

Try the following regex:

s <- "('9','','','','','','','','31.23','testing7'),
('10','','','','','','','','31.23','testing10')"

gsub("'(-?\\d+(?:[\\.,]\\d+)?)'", x = s, replacement = "\\1")

Regex explanation:

  • ' match literal single quote character
  • () capture group
  • ? match between 0-1 times
  • \\d+ match digit 1 and unlimited times
  • \\1 group 1

This should allow for negative numbers and decimals.

Output

"(9,'','','','','','','',31.23,'testing7'),(10,'','','','','','','',31.23,'testing10')"


Related Topics



Leave a reply



Submit