[R-sig-Geo] Small problem in extraction of a table of range, sill and nugget in loop

Tim Appelhans tim.appelhans at gmail.com
Fri Sep 11 14:31:13 CEST 2015


Uzzal,
if you wrap a do.call("rbind", ... around you lapply statement, I think 
you get what you want:

table<-do.call("rbind", lapply(seq(seoul1to7_splt), function(i)
{
   dat<-seoul1to7_splt[[i]]
   coordinates(dat)<-~LON+LAT
   proj4string(dat) <- "+proj=longlat +datum=WGS84"
   dat <- spTransform(dat, CRS("+proj=utm +north +zone=52 +datum=WGS84"))
   variogram<-autofitVariogram(log(PM10)~1,dat)

   nug=variogram$var_model$psill[1]
   sill = variogram$var_model$psill[2] + nug
   range = variogram$var_model$range[2]
   table<-data.frame(sill=sill,range=range,nugget=nug)

   return(table)
}))

after this, you can simply add your datetimes in a further column and 
rearrange the columns as you wish

HTH
Tim

On 11.09.2015 14:15, Uzzal wrote:
>
> Hello, I have a csv file named "seoul1to7" contains hourly pm10 
> concentration for around 105 sites collected for 7 days. Please 
> download from here 
> <https://drive.google.com/file/d/0ByY3OAw62EShdEdqcUVHVTNabnM/view?usp=sharing>
>
>  By using this dataset, I have plotted variogram for every hour of 7 
> days (total 161 variogram) using automap package in R. From every 
> variogram, I can see that the range, sill and nugget value is written 
> on the variogram plot. But I want to extract all the psill,range and 
> nugget value in a table for every individual variogram (hour). Is it 
> possible in R?
>
>  [Actually, in future I want to make some graph using those range, 
> sill and nugget to compare the changes]. I wrote the following code to 
> plot 161 variogram :
>
> library(sp)
> library(gstat)
> library(rgdal)
> library(automap)
> library(latticeExtra)
> library(ggplot2)
>
> seoul1to7<-read.csv("seoul1to7.csv")
> seoul1to7[seoul1to7==0] <-NA
> seoul1to7 <- na.omit(seoul1to7)
> seoul1to7_splt<-split(seoul1to7,seoul1to7$time) #a list contain 161 
> dataframe
> seq(seoul1to7_splt)
> names(seoul1to7_splt)
>
> hours<-as.POSIXct(names(seoul1to7_splt), format="%Y%m%d%H")
> hours
>
> vars<-lapply(seq(seoul1to7_splt), function(i)
> {
>   dat<-seoul1to7_splt[[i]]
>   coordinates(dat)<-~LON+LAT
>   proj4string(dat) <- "+proj=longlat +datum=WGS84"
>   dat <- spTransform(dat, CRS("+proj=utm +north +zone=52 +datum=WGS84"))
>   variogram<-autofitVariogram(log(PM10)~1,dat, model="Sph")
>   plt<- plot(variogram,plotit=F,asp=1)
>   plt<- update(plt, main=paste("Variogram for", as.character(hours)[i]))
>
>   return(plt)
> })
> vars[[1]]
>
> .
>
> .
>
>
> vars[[161]]
>
> *my problem starts from here: *Now I want to extract the table of 
> sill, range and Nugget.So I wrote this code
>
> table<-lapply(seq(seoul1to7_splt), function(i)
> {
>   dat<-seoul1to7_splt[[i]]
>   coordinates(dat)<-~LON+LAT
>   proj4string(dat) <- "+proj=longlat +datum=WGS84"
>   dat <- spTransform(dat, CRS("+proj=utm +north +zone=52 +datum=WGS84"))
>   variogram<-autofitVariogram(log(PM10)~1,dat)
>
>   nug=variogram$var_model$psill[1]
>   sill = variogram$var_model$psill[2] + nug
>   range = variogram$var_model$range[2]
>   table<-data.frame(sill=sill,range=range,nugget=nug)
>
>   return(table)
> })
> table<-as.data.frame (table))
>
> But as output here I got this type of table below
>
> sill
>
> 	
>
> range
>
> 	
>
> nugget
>
> 	
>
> Sill.1
>
> 	
>
> Range.1
>
> 	
>
> Nugget.1
>
> 	
>
> Sill.2
>
> 	
>
> Range.2
>
> 	
>
> Nugget.2
>
> 	
>
> ???
>
> 	
>
> 	
>
> 	
>
> 	
>
> 	
>
> 	
>
> 	
>
> 	
>
> 	
>
> But I want this type of table below.
>
> hours
>
> 	
>
> Psill
>
> 	
>
> Range
>
> 	
>
> Nugget
>
> 2012030101
>
> 	
>
> 	
>
> 	
>
> 2012030102
>
> 	
>
> 	
>
> 	
>
> .
>
> 	
>
> 	
>
> 	
>
> .
>
> 	
>
> 	
>
> 	
>
> .
>
> 	
>
> 	
>
> 	
>
> 2012030731
>
> 	
>
> 	
>
> 	
>
> What correction I should do in my loop?
>
> If you have any further query please let me know. Thanks in advance.
>
> Orpheus
>
>
>
>
>
>

-- 
#####################################
Tim Appelhans
Department of Geography
Environmental Informatics
Philipps Universität Marburg
Deutschhausstraße 12
35032 Marburg (Paketpost: 35037 Marburg)
Germany

Tel +49 (0) 6421 28-25957

http://environmentalinformatics-marburg.de/


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list