x <- NULL creates a NULL variable isnull(x) tests whether a variable is NULL. |

A NULL variable is a special type of variable. Unlike REAL, LOGICAL or CHARACTER variables, a NULL variable contains no data. You might think of it as an completely empty variable. Many functions and commands such as anova(), regress() and print() that are primarily executed for their "side effects" return a NULL variable as value. You can explicitly create a NULL variable by Cmd> nullvar <- NULL or Cmd> nullvar <- print("Hello!") # value of print() is NULL You can use isnull() to test whether a variable is NULL. Cmd> isnull(NULL, PI, T, "hello", nullvar) (1) T F F F T See isnull() for details. For obvious reasons, you can't do arithmetic or comparisons with NULL variables and most commands and functions do not accept NULL variables as arguments. A few functions such as vector(), hconcat(), vconcat(), sum(), prod(), min() and max() do accept NULL arguments. For example, vector(NULL,a,b) and min(NULL,a,b) are equivalent to vector(a,b) and min(a,b), respectively. Here is an example where this might be useful. Cmd> x <- run(10); fstats <- NULL # or fstats <- vector(NULL) Cmd> for(i,run(1000)){regress("{.1*x+rnorm(10)}=x",silent:T) fstats <- vector(fstats,SS[2]/(SS[3]/DF[3]));;} This creates a random sample of F-statistics based on a regression of y on x where y = .1*x + rnorm(10) (see regress(), 'models', rnorm()) Although fstats starts out NULL, by the end of the first trip through the loop, it contains the first F-statistic. Without NULL variables, the loop would have to be something like the following: Cmd> for(i,run(1000)){ regress("{x+rnorm(10)}=x",silent:T); fstats <- \ if(i==1){SS[2]/(SS[3]/DF[3])}else{vector(f,SS[2]/(SS[3]/DF[3]))}} A better way to implement this example would probably be initialize by fstats <- rep(0,1000), and save each value by fstats[i] <- SS[2]/(SS[3]/DF[3]). See also topics 'for' and 'if'.

Gary Oehlert 2003-01-15