Paste Quotation Marks into Character String, Within a Loop

paste quotation marks into character string, within a loop

Simply escape the quotation marks with backslashes:

paste("modelCheck(var\"",i,"_d.bug\")",sep="")

An alternative is to use single quotes to enclose the string:

paste('modelCheck(var"',i,'_d.bug")',sep="")

Concatenate quotes with a string in a loop in R

The problem is that your column is a factor:

items <- structure(list(V1 = structure(c(4L, 3L, 1L, 2L, 5L, 6L),
.Label = c("Am f?cut multe din lucrurile pe care mi le-am dorit.",
"Am sentimente calde fa?? de ceilal?i.", "Nu sunt interesat de al?i oameni. (I) ",
"Nu sunt mul?umit de felul în care sunt. (I)", "Rareori m? trezesc odihnit. (I)",
"Sunt pesimist în leg?tur? cu viitorul. (I)"),
class = "factor")),
.Names = "V1",
row.names = c(NA, -6L),
class = "data.frame")
x <- structure(list(SSB1 = c(6L, 6L, 6L, 6L, 6L, 3L),
SSB2 = c(5L, 6L, 5L, 6L, 6L, 1L),
SSB3 = c(5L, 4L, 4L, 6L, 6L, 5L),
SSB4 = c(6L, 4L, 5L, 6L, 6L, 6L),
SSB5 = c(5L, 1L, 4L, 5L, 3L, 4L),
SSB6 = c(6L, 3L, 6L, 6L, 2L, 2L),
SSB7 = c(4L, 1L, 2L, 4L, 3L, 2L),
SSB8 = c(5L, 4L, 4L, 6L, 5L, 6L),
SSB9 = c(6L, 4L, 6L, 6L, 4L, 4L),
SSB10 = c(6L, 5L, 6L, 1L, 4L, 4L)),
.Names = c("SSB1", "SSB2", "SSB3", "SSB4", "SSB5",
"SSB6", "SSB7", "SSB8", "SSB9", "SSB10"),
class = "data.frame",
row.names = c("1", "2", "3", "4", "5", "6"))

class(items[,1])
## [1] "factor"
paste("\"", items[1,], "\"", sep = "")
## [1] "\"Nu sunt mul?umit de felul în care sunt. (I)\""

The simple paste function works fine, but when you try to assign it back to the same row (you should be more explicit, btw, by using item[i,1] instead of just item[i,], especially if you are intending to collapse strings):

items[1,] <- paste("\"", items[1,], "\"", sep = "")
## Warning in `[<-.factor`(`*tmp*`, iseq, value = "\"Nu sunt mul?umit de felul în care sunt. (I)\"") :
## invalid factor level, NA generated

NB: we will need to reload items since we just corrupted the first row.

The problem is that the string resulting from the paste command is not recognized as one of the levels of the factor.

levels(items[,1])
## [1] "Am f?cut multe din lucrurile pe care mi le-am dorit."
## [2] "Am sentimente calde fa?? de ceilal?i."
## [3] "Nu sunt interesat de al?i oameni. (I) "
## [4] "Nu sunt mul?umit de felul în care sunt. (I)"
## [5] "Rareori m? trezesc odihnit. (I)"
## [6] "Sunt pesimist în leg?tur? cu viitorul. (I)"

paste("\"", items[1,], "\"", sep = "")
## [1] "\"Nu sunt mul?umit de felul în care sunt. (I)\""

In order to assign an arbitrary string into this factor-ized column, you must either (a) ensure the string you are assigning is the same as one of the defined levels, (b) add levels to the column to account for your new string (probably not efficient nor what you are wanting to do), or (c) just convert the whole column to character before you even begin this. The first two are inefficient and (IMO) unnecessary, but let me know if I'm misinterpreting your needs.

For (c):

items[,1] <- as.character(items[,1])
items[1,] <- paste("\"", items[1,], "\"", sep = "")
items
## V1
## 1 "Nu sunt mul?umit de felul în care sunt. (I)"
## 2 Nu sunt interesat de al?i oameni. (I)
## 3 Am f?cut multe din lucrurile pe care mi le-am dorit.
## 4 Am sentimente calde fa?? de ceilal?i.
## 5 Rareori m? trezesc odihnit. (I)
## 6 Sunt pesimist în leg?tur? cu viitorul. (I)

TL;DR;

You have to convert (your column) to a string first before you use paste.

How to add quotes around each word in a string in R?

We can split the words by , to get a list output. We loop through sapply , dQuote the elements and then paste it together with toString which is a wrapper for paste(..., collapse=', ').

sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x)))
#[1] "“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”"

If we need to change the fancy quotes, add FALSE in dQuote

sapply(strsplit(words, '[, ]+'), function(x) toString(dQuote(x, FALSE)))

Creating a string in R with in it

In the string, there is already a double quote. So, we can wrap it with single quotes

Input <- '?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA'
cat(Input, "\n")
#?jql=filter%20=%20"Plan%20Standup%20-%20Mutual-SA

multiple quotation marks in for loop (batch file)

Well, you actually answered your own question without looking closely.
Let me show you why:

set command="C:\Program Files\7-Zip\7z.exe" l "FolderName\archive.zip" "file.cmd"
for /F "delims=" %%a in ('"%command%"') do ...

Note that your code translates the value of the set command to:

"C:\Program Files\7-Zip\7z.exe" l "FolderName\archive.zip" "file.cmd"

where you then pass that to the loop in double quotes again, as:

"%command%"

which ends up being:

""C:\Program Files\7-Zip\7z.exe" l "FolderName\archive.zip" "file.cmd""

Which will work exactly in a for loop:

for /f "delims=" %%i in ('""C:\Program Files\7-Zip\7z.exe" l "FolderName\archive.zip" "file.cmd""') do ...

Though that will work as expected in this case, it will fail if your quoted strings contain special characters like & as shown in this example:

for /f "delims=" %%i in ('""C:\Program Files\7-Zip\7z.exe" l "FolderName\archives & backups.zip" "file.cmd""') do ...

So you need to simply escape the outer double quotes to overcome this:

for /f "delims=" %%i in ('^""C:\Program Files\7-Zip\7z.exe" l "FolderName\archives & backups.zip" "file.cmd"^"') do ...

Point 2. from cmd /? seemingly states this behaviour, as per the below screenshot:
Sample Image

How can you print a variable in quotation marks?

In Python 2.6 or above, you can use string.format:

print('"{}" does not even sound like a word anymore.'.format(favoriteword))

In lower versions, Ketzak's method will work.


To print multiple times on a single line, you want to prevent print from appending a newline.

In Python 3, use the end argument:

for i in range(12):
print(favoriteword, end='')
print('') # for newline

or in lower versions:

import sys

for i in range(12):
sys.stdout.write(favoriteword)
print('')

Insert variable within character string

It seems this is what you are after.

for (i in 1 : length(file.list)) {
file.name.variable <- file.list[i]

url <- paste0("https://accounts.profunds.com/etfdata/ByFund/",
file.name.variable, "-historical_nav.csv")

destfile <- paste0("C:/R Projects/Data/etf_aum/",
file.name.variable, "csv")

download.file(url, destfile, mode="wb")
}


Related Topics



Leave a reply



Submit