Adam_G Adam_G - 1 year ago 59
R Question

R: Create Variable and Display Value in Single Command

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)

Answer Source

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( && !is.null(n1 <- names(dn <-[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 :)