I'd like to run Moran's I test on a data frame with 12044 columns (and 3400 rows) and store the results in a df or a list of sorts. The first three columns are ID, Lat, and Long respectively. The rest are variables that I am interested in.
I understand that lapply is designed to do what I want, but I am not very good at storing the results in a separate df. The result of the test has four variable: observed, expected, sd, and p.value.
Here is a sample of the df and the function itself.
set.seed(1)
df <- data.frame(
ID = 1:15,
LATITUDE = c(42.6, 42.5, 42.3, 42.8, 42.4, 42.4, 42.4, 42.3, 42.4, 42.4, 41.4, 41.6, 41.8, 43.7, 47.3),
LONGITUDE = c(-71.5, -71.6, -71.9, -71.0, -71.1, -71.1, -71.1, -71.1, -71.2, -71.2, -70.5, -70.3, -71.2, -70.3, -68.3),
x1 = runif(15, min=0, max=1000),
x2 = runif(15, min=0, max=1000),
x3 = runif(15, min=0, max=1000),
x4 = runif(15, min=0, max=1000),
x5 = runif(15, min=0, max=1000),
x6 = runif(15, min=0, max=1000),
x7 = runif(15, min=0, max=1000),
x8 = runif(15, min=0, max=1000) )
require(ape)
dists <- as.matrix(dist(cbind(df$LONGITUDE, df$LATITUDE)))
dists.inv <- 1/dists
diag(dists.inv) <- 0
#check
dists.inv[1:5, 1:5]
#deal with the infinite values in the matrix
dists.inv[is.infinite(dists.inv)] <- 0
#calculate Moran's I
Moran.I(df$x1, dists.inv)
Thank you all