Difference Between Paste() and Paste0()

Difference between paste() and paste0()

As explained in this blog by Tyler Rinker:

paste has 3 arguments.

paste (..., sep = " ", collapse = NULL) The ... is the stuff you
want to paste together and sep and collapse are the guys to get it
done. There are three basic things I paste together:

  • A bunch of individual character strings.
  • 2 or more strings pasted element for element.
  • One string smushed together.

Here's an example of each, though not with the correct arguments

paste("A", 1, "%") #A bunch of individual character strings.

paste(1:4, letters[1:4]) #2 or more strings pasted element for
element.

paste(1:10) #One string smushed together. Here's the
sep/collapse rule for each:

  • A bunch of individual character strings – You want sep
  • 2 or more strings pasted element for element. – You want sep
  • One string smushed together.- Smushin requires collapse

paste0 is short for: paste(x, sep="") So it allows us to be lazier
and more efficient.

paste0("a", "b") == paste("a", "b", sep="") ## [1] TRUE

What is the difference between paste/paste0 and str_c?

paste0(..., collapse = NULL)is a wrapper for paste(..., sep = "", collapse = NULL), which means there is no separator. In other words, with paste0() you can not apply some sort of separator, while you do have that option with paste(), whereas a single space is the default.

str_c(..., sep = "", collapse = NULL) is equivalent to paste(), which means you do have the option to customize your desired separator. The difference is for str_c() the default is no separator, so it acts just like paste0() as a default.

Paste() and paste0() are both functions from the base package, whereas str_c() comes from the stringr package.

I did not test/microbenchmark it, but from my experience I do agree to Ryan str_c() is generally faster.

What are the differences between concatenating strings with cat() and paste()?

cat and paste are to be used in very different situations.


paste is not print

When you paste something and don't assign it to anything, it becomes a character variable that is print-ed using print.default, the default method for character, hence the quotes, etc. You can look at the help for print.default for understanding how to modify what the output looks like.

  • print.default will not evaluate escape characters such as \n within a character string.

Look at the answers to this question for how to capture the output from cat.


Quoting from the easy to read help for cat (?cat)

Concatenate and Print


Description


Outputs the objects, concatenating the representations. cat performs
much less conversion than print.

...

Details


cat is useful for producing output in user-defined functions. It
converts its arguments to character vectors, concatenates them to a
single character vector, appends the given sep= string(s) to each
element and then outputs them.

Value


None (invisible NULL).

cat will not return anything, it will just output to the console or another connection.

Thus, if you try to run length(cat('x')) or mode(cat('x')), you are running mode(NULL) or length(NULL), which will return NULL.


The help for paste is equally helpful and descriptive

Concatenate Strings


Description


Concatenate vectors after converting to character.

....

Value


A character vector of the concatenated values. This will be of length
zero if all the objects are, unless collapse is non-NULL in which case
it is a single empty string.

sep argument in paste0() function behaves not as expected

The paste0 function does not have a sep= parameter. It was specifically created to not use a separator. So these are all the same

paste0("1st", "2nd", "3rd", sep=", ")
paste0("1st", "2nd", "3rd", ", ")
paste0(a="1st", b="2nd", c="3rd", d=", ")

Check the ?paste0 help page for more info. If you want a separator, don't use paste0. This is expected bahavior.

Difference between `paste`, `str_c`, `str_join`, `stri_join`, `stri_c`, `stri_paste`?

  • stri_join, stri_c, and stri_paste come from package stringi and are pure aliases

  • str_c comes from stringr and is just stringi::stri_join with a parameter ignore_null hardcoded to TRUE while stringi::stri_join has it set to FALSE by default. stringr::str_join is a deprecated alias for str_c

see:

library(stringi)
identical(stri_join, stri_c)
# [1] TRUE
identical(stri_join, stri_paste)
# [1] TRUE

library(stringr)
str_c
# function (..., sep = "", collapse = NULL)
# {
# stri_c(..., sep = sep, collapse = collapse, ignore_null = TRUE)
# }
# <environment: namespace:stringr>

stri_join is very similar to base::paste with a few differences enumerated below:


1. sep = "" by default

So it behaves more like paste0 by default, but paste0 lost its sep argument.

identical(paste0("a","b")        , stri_join("a","b"))
# [1] TRUE
identical(paste("a","b") , stri_join("a","b",sep=" "))
# [1] TRUE
identical(paste("a","b", sep="-"), stri_join("a","b", sep="-"))
# [1] TRUE

str_c will behave just like stri_join here.


2. Behavior with NA

if you paste to NA using stri_join, the result is NA, while paste converts NA to "NA"

paste0(c("a","b"),c("c",NA))
# [1] "ac" "bNA"
stri_join(c("a","b"),c("c",NA))
# [1] "ac" NA

str_c will behave just like stri_join here as well


3. Behavior with length 0 arguments

When a length 0 value is encountered, character(0) is returned, except if ignore_null is set to FALSE, then the value is ignored. It is different from the behavior of paste which would convert the length 0 value to "" and thus contain 2 consecutive separators in the output.

stri_join("a",NULL, "b")  
# [1] character(0)
stri_join("a",character(0), "b")
# [1] character(0)

paste0("a",NULL, "b")
# [1] "ab"
stri_join("a",NULL, "b", ignore_null = TRUE)
# [1] "ab"
str_c("a",NULL, "b")
# [1] "ab"

paste("a",NULL, "b") # produces double space!
# [1] "a b"
stri_join("a",NULL, "b", ignore_null = TRUE, sep = " ")
# [1] "a b"
str_c("a",NULL, "b", sep = " ")
# [1] "a b"

4. stri_join warns more

paste(c("a","b"),c("c","d","e"))
# [1] "a c" "b d" "a e"
paste("a","b", sep = c(" ","-"))
# [1] "a b"

stri_join(c("a","b"),c("c","d","e"), sep = " ")
# [1] "a c" "b d" "a e"
# Warning message:
# In stri_join(c("a", "b"), c("c", "d", "e"), sep = " ") :
# longer object length is not a multiple of shorter object length
stri_join("a","b", sep = c(" ","-"))
# [1] "a b"
# Warning message:
# In stri_join("a", "b", sep = c(" ", "-")) :
# argument `sep` should be one character string; taking the first one

5. stri_join is faster

microbenchmark::microbenchmark(
stringi = stri_join(rep("a",1000000),rep("b",1000),"c",sep=" "),
base = paste(rep("a",1000000),rep("b",1000),"c")
)

# Unit: milliseconds
# expr min lq mean median uq max neval cld
# stringi 88.54199 93.4477 97.31161 95.17157 96.8879 131.9737 100 a
# base 166.01024 169.7189 178.31065 171.30910 176.3055 215.5982 100 b

How to remove space before dot using paste() function in R

Paste0(...) or paste(..., sep = "") work fine, but I find the glue() offers a clean way of concatenating strings, without lots of opening and closing quotes.

In this case:

glue::glue("The values are {a} and {b}.")

What's the difference between the print() and the print(paste())-functions in R?

print

If you are at the R console then the result of any expression you type in is automatically printed so you don't need to specify print so these are the same:

# same when typed into the R console

32
## [1] 32

print(32)
## [1] 32

however, automatic printing is not done in R scripts, in R functions or in any context where it is within the body of some larger expression such as within a for or while loop. Thus, to have 32 print from within a function use print. In these cases nothing would have been printed had we not used print.

f <- function() {
print(32)
}
x <- f()
## [1] 32

for(i in 1:3) print(32)
## [1] 32
## [1] 32
## [1] 32

Note that print prints out a single object. If you want to print out several objects you can either use several print statements or else combine the objects into a single larger object. For example,

# print A and then print B
"A"
## [1] "A"
"B"
## [1] "B"

paste("A", "B", sep = ",") # create single char string and print it
## [1] "A,B"

c("A", "B") # create vector of two elements and print it out
## [1] "A" "B"

There is also cat.

x <- "A"
y <- "B"
cat("x:", x, "y:", y, "\n")
## x: A y: B

paste

paste has nothing to do with printing. Its function is to take its arguments and create a character string out of them so paste("A", "B") creates the character string "A B". Of course if you enter a paste command at the R console since R prints out the value of any expression typed into it, the result of the paste will be printed out. Here are some examples of automatic printing assuming that these expressions are typed into the R console.

# assume these are typed into the R console

paste("A", "B") # R automatically prints result of paste
## [1] "A B"

paste(32) # 32 is converted to character; then R automatically prints it
## [1] "32"

using paste0 in file name in exams2moodle

Because grupo is a string: "gr1". The exams2moodle's first parameter is a string (in your case) and not the list of files (as you want).

If you want use a variable which name is in a string variable, you should use get (get: Return the Value of a Named Object)

Check the sample code:

> x <- 'foo'
> foo <- 'bar'
> x
[1] "foo"
> get(x)
[1] "bar"
>

In your case:

for (i in 1:2){
grupo <- paste0("gr",i)
exams2moodle(get(grupo), name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}


Related Topics



Leave a reply



Submit