R - 将相同的脚本应用于多个数据帧

时间:2016-08-18 11:53:31

标签: r function dataframe multiple-columns

我有一个脚本,它读取包含坐标,降雨量和裁剪区域数据的.dat文件,如下所示。

  East  North    rain  Wheat Sbarley Potato OSR fMaize Total  LCA
10000 510000 1498.73 0.0021     0.5 0.0022   0 0.0056  0.01 0.01
10000 510000 1498.73 0.0021   0.034 0.0022   0 0.0056  0.01 0.01
10000 510000 1498.73 0.0021   0.001 0.0022   0 0.0056  0.01 0.01
10000 515000 1518.51 0.0000    0.12 0.0000   0 0.0000  0.00 0.00
10000 515000 1518.51 0.0000  0.0078 0.0125   0 0.0000  0.00 0.00
10000 515000 1518.51 0.0000       0 0.0000   0   0.03  0.00 0.00 

下面的代码在提取数据并生成光栅文件并在ggplot中绘制之前,通过一系列模型计算Wheat的排放量。有read these related queries我对是否需要制作一个包或者遗漏一些非常基本的关于如何重复代码通过每种作物类型。

library(doBy)
library(ggplot2)
library(plyr)
library(raster)
library(RColorBrewer)
library(rgdal)
library(scales)
library(sp)

### Read in the data
crmet <- read.csv("data.dat")
# Remove NA values
crm <- crmet[ ! crmet$rain %in% -119988, ]
crm <- crm[ ! crm$Wheat %in% -9999, ]

### Set model parameters
a <- 0.1474
b <- 0.0005232
g <- -0.00001518
d <- 0.000003662
N <- 182

### Models
crm$logN2O <- a+(b*crm$rain)+(g*N)+(d*crm$rain*N)
crm$eN2O <- exp(crm$logN2O)
crm$whN2O <- crm$eN2O*crm$Wheat

### Prepare data for conversion to raster
crmet.ras <- crm
crmet.ras <- rename(crmet.ras, c("East"="x","North"="y"))

#### Make wheat emissions raster
wn <- crmet.ras[,c(1,2,13)]
spg <- wn

# Set the Eastings and Northings to coordinates
coordinates(spg) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(spg) <- TRUE
# coerce to raster
rasterDF <- raster(spg)
# Add projection to it - in this case OSBG36
proj4string(rasterDF) <- CRS("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs")
rasterDF

writeRaster(rasterDF, 'wn.tif', overwrite=T)

### Plotting the raster:
whplot <-ggplot(wn, aes(x = x, y = y))+
  geom_tile(aes(fill = whN2O))+
  theme_minimal()+
  theme(plot.title = element_text(size=20, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        legend.title = element_text(size=16, face="bold"))+
  scale_fill_gradient(name=expression(paste("", N[2], "O ", Ha^-1, sep="")))+
  xlab ("")+
  ylab ("")+
  labs (title="Nitrous oxide emissions\nfrom Wheat")

whplot

我试图尽可能多地将上述内容转换为函数,但它们比我在此处和?help文件中找到的示例更复杂。非常感谢任何帮助/建议。

1 个答案:

答案 0 :(得分:1)

您需要将代码放在一个以数据文件名作为参数的函数中。然后,您可以使用相关的数据文件名调用您的函数。类似的东西:

library(doBy)
library(ggplot2)
library(plyr)
library(raster)
library(RColorBrewer)
library(rgdal)
library(scales)
library(sp)

#defining the function
my.neat.function <- function(datafname){
### Read in the data
crmet <- read.csv(datafname)
# Remove NA values
crm <- crmet[ ! crmet$rain %in% -119988, ]
crm <- crm[ ! crm$Wheat %in% -9999, ]

### Set model parameters
a <- 0.1474
b <- 0.0005232
g <- -0.00001518
d <- 0.000003662
N <- 182
### Models
crm$logN2O <- a+(b*crm$rain)+(g*N)+(d*crm$rain*N)
crm$eN2O <- exp(crm$logN2O)
crm$whN2O <- crm$eN2O*crm$Wheat

### Prepare data for conversion to raster
crmet.ras <- crm
crmet.ras <- rename(crmet.ras, c("East"="x","North"="y"))

#### Make wheat emissions raster
wn <- crmet.ras[,c(1,2,13)]
spg <- wn

# Set the Eastings and Northings to coordinates
coordinates(spg) <- ~ x + y
# coerce to SpatialPixelsDataFrame
gridded(spg) <- TRUE
# coerce to raster
rasterDF <- raster(spg)
# Add projection to it - in this case OSBG36
proj4string(rasterDF) <- CRS("+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs")
rasterDF

writeRaster(rasterDF, 'wn.tif', overwrite=T)

### Plotting the raster:
whplot <-ggplot(wn, aes(x = x, y = y))+
  geom_tile(aes(fill = whN2O))+
  theme_minimal()+
  theme(plot.title = element_text(size=20, face="bold"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        legend.title = element_text(size=16, face="bold"))+
  scale_fill_gradient(name=expression(paste("", N[2], "O ", Ha^-1, sep="")))+
  xlab ("")+
  ylab ("")+
  labs (title="Nitrous oxide emissions\nfrom Wheat")

whplot
} #end of function definition
my.neat.function("data.dat") #first call to function
my.neat.function("otherdata.dat")#same thing with another dataset

如果不同数据的模型参数,则需要将参数值向量作为参数添加到函数中。