16  Global Assignment

16.1 Global assigment of an object

Code
fn_xmean = function(x, y, ...) {

    fn_n = function(x, na.rm = FALSE) ifelse(na.rm, length(na.omit(x)), length(x))
    xn = fn_n(x, ...)
    xsum = sum(x, ...)
    xmean = xsum/xn

    z = y^2
    y <<- z

    return(list(y = z, xmean = xmean))

}

16.2 Data

Code
A = c(NA, 12, 15, 14, 18)

16.3 Function call

Code
y = 5

fn_xmean(A, y = y, na.rm = TRUE)
$y
[1] 25

$xmean
[1] 14.75
Code
y
[1] 25

16.4 Global assignment of an object

Code
def fn_mean(x, **kwargs):
    
    global y
    y = y**2
    
    na_rm = kwargs.get('na_rm', None)
    
    if na_rm:
        x = x[~np.isnan(x)]
        
    xn = len(x)
    xsum = np.sum(x)
    xmean = xsum / xn
    return xmean
  
# Example usage:
A = np.array([11, 12, np.nan, 14, 15])
y = 5
fn_mean(A, na_rm=True)
print(y)