Can R Paste() Output "\"

Can R paste() output \?

That is the printed representation of a single "\" in R. Clearly the right answer will depend on your end usage, but will something like this do:

> citations <- paste("title", 1:3, sep = "")
> cites <- paste("\\citep{", citations, "}", sep = "")
> writeLines(cites)
\citep{title1}
\citep{title2}
\citep{title3}

Using writeLines() you can output that to a file using something like:

> writeLines(cites, con = file("cites.txt"))

Resulting in the following file:

$ cat cites.txt 
\citep{title1}
\citep{title2}
\citep{title3}

Print string and variable contents on the same line in R

You can use paste with print

print(paste0("Current working dir: ", wd))

or cat

cat("Current working dir: ", wd)

What is the use of 'sep' in paste command of R?

sep is more generally applicable when you have more than two vectors of length greater than 1. If you were looking to get "something_to_paste", then you would be looking for the collapse argument.

Try the following to get a sense of what the sep argument does:

paste(a, 1:3, sep = "_")
# [1] "something_1" "to_2" "paste_3"

and compare it to collapse:

paste(a, collapse = "_")
# [1] "something_to_paste"

R paste() not working - encountering unexpected symbol?

Not sure if this counts as an answer - you missed a comma:

paste(toString(sQuote(mylist)), collapse = "")

Paste outputs of two functions to each other in a loop in R

We can do this with a for loop after capturing the output from CrossTable and using append = TRUE in both capture.output and in the cat by writing the contents to the same file

for(i in 1:2) {
capture.output(CrossTable(z[[i]], z$group, expected = FALSE,
format="SPSS", chisq= T, prop.t = F, dnn = c(names(z)[i], "Group")),
file = "tempnew.txt", append = TRUE)
cat(paste('cramer = ', round(assocstats(table(z[[i]], z$group))[[5]], 3)),
file = 'tempnew.txt', append = TRUE, '\n')
cat('\n\n\n********************************\n\n\n',
file = 'tempnew.txt', append = TRUE, '\n')
}

-contents of 'tempnew.txt'

Sample Image



Related Topics



Leave a reply



Submit