问题描述:

If I want to assign a variable only if it is not undefined, is this the most concise way to do it, or is there a shorter syntax to represent the idea?

`if (!exists('t')) t = 1`

For example, in `C#`

, one can write

`int y = x ?? -1;`

To express the idea that `y`

should be assigned to `x`

unless `x`

is null.

You could stitch together your own infix operator:

```
`%<-%` <- function(x,y){
nm <- as.character(substitute(x))
if (!exists(nm,where = parent.frame())){
assign(x=nm,value=y,envir = parent.frame())
return(TRUE)
}else{
return(FALSE)
}
}
> x <- 2
> x %<-% 5
[1] FALSE
> x
[1] 2
> y %<-% 10
[1] TRUE
> y
[1] 10
```

A good way to check whether a vector is null or not is using the length function. This may not be relevant to your specific question though.

```
> t<-NULL
> length(t)
[1] 0
> t<-1
> length(t)
[1] 1
```