Running a function that outputs a dataframe iteratively and storing the data in a new data frame

Hello, i'm trying to do a batch run of variogram calculations using the gstat package and the variogram function. The main issue is that "variogram" outputs a data frame, and i'm having trouble figuring out how to store each iteration of all the data frame components in a new data frame, organized by the subsetting ID. My code is below, note i had to replace any data references with dummy names. I give an example of the dummy data. Any help or hints with this would be greatly appreciated. Thankyou.

Dummydata.csv:
|Location_ID |Sample_ID|x|y|Value|
|Dummy1|Dummy1-1|0|462.725|3.4204|
|Dummy1|Dummy1-2|0|461.725|3.474|
|Dummy1|Dummy1-3|0|460.475|3.3534|
|Dummy1|Dummy1-4|0|458.475|3.4539|
|Dummy1|Dummy1-5|0|456.475|3.3601|
|Dummy1|Dummy1-6|0|454.475|3.14235|
|Dummy1|Dummy1-7|0|452.475|2.80534|
|Dummy1|Dummy1-8|0|450.475|2.93197|
|Dummy1|Dummy1-9|0|462.651|3.13632|
|Dummy1|Dummy1-10|0|461.151|3.5477|
|Dummy1|Dummy1-11|0|459.901|3.7085|
|Dummy1|Dummy1-12|0|457.901|3.28506|
|Dummy2|Dummy2-1|0|452.77|3.47534|
|Dummy2|Dummy2-2|0|447.77|3.39896|
|Dummy2|Dummy2-3|0|442.77|3.24419|
|Dummy2|Dummy2-4|0|437.77|3.5276|
|Dummy2|Dummy2-5|0|437.77|3.6951|
|Dummy3|Dummy3-1|0|456.274|3.5678|
|Dummy3|Dummy3-2|0|455.274|3.6214|
|Dummy3|Dummy3-3|0|454.524|3.47132|
|Dummy3|Dummy3-4|0|453.024|2.7906|
|Dummy3|Dummy3-5|0|451.024|3.4338|
|Dummy3|Dummy3-6|0|448.024|2.48441|
|Dummy3|Dummy3-7|0|445.024|2.76849|
|Dummy3|Dummy3-8|0|443.024|2.55744|
|Dummy3|Dummy3-9|0|440.024|2.53399|
|Dummy3|Dummy3-10|0|436.024|2.7169|

library(gstat)
library(sp)

my_data <- read.csv("Dummydata.csv")

boring_list <- c(unique(my_data[ , c("Location_ID")]))
length(boring_list)

coordinates(my_data)=~x+y

Computation of the variogram

data.frame(matrix(NA, nrow = 1680, ncol = 6))
for(i in 3){

Boring <- subset(my_data, Location_ID==boring_list[i])

data.frame[i]=variogram(value~1, data=Boring,cutoff=7, width=0.7)

}

Is this what you want to do?

library(sp)
library(gstat)
library(tidyverse)

# Sample data in a copy/paste friendly format
my_data <- data.frame(
    stringsAsFactors = FALSE,
    Location_ID = c("Dummy1","Dummy1","Dummy1",
                    "Dummy1","Dummy1","Dummy1","Dummy1","Dummy1","Dummy1",
                    "Dummy1","Dummy1","Dummy1","Dummy2","Dummy2",
                    "Dummy2","Dummy2","Dummy2","Dummy3","Dummy3","Dummy3",
                    "Dummy3","Dummy3","Dummy3","Dummy3","Dummy3","Dummy3",
                    "Dummy3"),
    Sample_ID = c("Dummy1-1","Dummy1-2",
                  "Dummy1-3","Dummy1-4","Dummy1-5","Dummy1-6","Dummy1-7",
                  "Dummy1-8","Dummy1-9","Dummy1-10","Dummy1-11","Dummy1-12",
                  "Dummy2-1","Dummy2-2","Dummy2-3","Dummy2-4",
                  "Dummy2-5","Dummy3-1","Dummy3-2","Dummy3-3","Dummy3-4",
                  "Dummy3-5","Dummy3-6","Dummy3-7","Dummy3-8","Dummy3-9",
                  "Dummy3-10"),
    x = c(0,0,0,0,0,0,0,0,0,0,0,
          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
    y = c(462.725,461.725,460.475,
          458.475,456.475,454.475,452.475,450.475,462.651,461.151,
          459.901,457.901,452.77,447.77,442.77,437.77,
          437.77,456.274,455.274,454.524,453.024,451.024,448.024,
          445.024,443.024,440.024,436.024),
    Value = c(3.4204,3.474,3.3534,3.4539,
              3.3601,3.14235,2.80534,2.93197,3.13632,3.5477,
              3.7085,3.28506,3.47534,3.39896,3.24419,3.5276,3.6951,
              3.5678,3.6214,3.47132,2.7906,3.4338,2.48441,2.76849,
              2.55744,2.53399,2.7169)
)

# Relevant code
my_data %>% 
    group_nest(Location_ID) %>% 
    mutate(data = set_names(data, Location_ID)) %>% 
    pull(data) %>% 
    map_dfr(.f = ~ {
        coordinates(.x)=~x+y
        variogram(Value~1, data=.x,cutoff=7, width=0.7)
    }, .id = "Location_ID")
#>    Location_ID np     dist       gamma dir.hor dir.ver   id
#> 1       Dummy1  5 0.494400 0.027848858       0       0 var1
#> 2       Dummy1  4 1.106500 0.019662718       0       0 var1
#> 3       Dummy1 11 1.795455 0.031186695       0       0 var1
#> 4       Dummy1  5 2.485200 0.039247147       0       0 var1
#> 5       Dummy1  5 3.235200 0.029413409       0       0 var1
#> 6       Dummy1  6 4.000000 0.048806840       0       0 var1
#> 7       Dummy1  4 4.625000 0.009594564       0       0 var1
#> 8       Dummy1  3 5.367333 0.093938385       0       0 var1
#> 9       Dummy1  5 6.085200 0.070218105       0       0 var1
#> 10      Dummy1  1 6.676000 0.082154311       0       0 var1
#> 11      Dummy2  1 0.000000 0.014028125       0       0 var1
#> 12      Dummy2  4 5.000000 0.039178589       0       0 var1
#> 13      Dummy3  2 0.875000 0.006349242       0       0 var1
#> 14      Dummy3  4 1.812500 0.116367056       0       0 var1
#> 15      Dummy3  1 2.250000 0.345114320       0       0 var1
#> 16      Dummy3  5 3.150000 0.158804031       0       0 var1
#> 17      Dummy3  1 4.000000 0.016728034       0       0 var1
#> 18      Dummy3  1 4.250000 0.017596880       0       0 var1
#> 19      Dummy3  4 5.062500 0.021503993       0       0 var1
#> 20      Dummy3  1 6.000000 0.221318698       0       0 var1
#> 21      Dummy3  2 6.750000 0.249854710       0       0 var1

Created on 2021-09-27 by the reprex package (v2.0.1)

Yes that is an elegant solution, thank you so much! I was so stuck on doing it with a for loop.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.