Simultaneous variable assignment and printing in R

2019-06-20 08:53发布

问题:

Sorry if may be a trivial question, but I was wondering if there was a way to assign a value and print the value to the console succinctly.

x <- 1:5; x

Is how I would presently do this, but I was wondering if there was a way to do it in one statement. Thanks!

回答1:

You can try:

(x <- 1:5)

or

print(x <- 1:5)

though that won't work for things like

(names(x) <- letters[1:5])

though for that specific example you can do:

(x <- setNames(x, letters[1:5]))