0

Is there a way to both create a variable and output its value in a single command? In other words, concatenating both lines below into a single command?

x <- mean(1:7)
x
3
  • 1
    Wrap in parantheses (x<- mean(1:7)) Commented Nov 16, 2016 at 1:56
  • or just dart your eyes over to the Environment pane in RStudio Commented Nov 16, 2016 at 1:58
  • @Hack-R - This is for an R Notebook Commented Nov 16, 2016 at 2:14

1 Answer 1

1

Wrap the command in parantheses.

(x <- mean(1:7))

Knowing this you can do a few cool R tricks like creating an object and using it in the same expression. The development team does this all over the R source code. This is a double nest from apply:

if (length(dn.call) && !is.null(n1 <- names(dn <- dn.call[1])) && 
        nzchar(n1) && length(ans.names) == length(dn[[1]])) 

Be careful with these newfound powers, your co-workers will never be able to read your code again :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.