使用R如何编写一个循环以使用x个时间步长在数据帧中使用绿色和nir计算ndwi?

时间:2018-11-19 08:12:29

标签: r

我有6个NetCDF文件中31年的Landsat数据。

每个文件约有400万行数据。

每个文件中每个文件的时间步数尚未确定

除了我用来编写脚本的第一个文件外,它有60个时间步长,并将用于处理所有文件。

在进行这项小运动之前,我从未使用过R。

我的任务是为统计人员创建一组仅土地数据。

如何遍历计算ndwi的时间步长?

我有此代码工作: 但这需要大量查找/替换,复制/粘贴才能完成60个时间步,然后还有5个文件需要处理。

###############################################################
# Calculate ndwi for for the each timestep
###############################################################
#convert the timestep_1 for green to numeric value
green_nir_df02$t_1 <- as.numeric(as.character(green_nir_df02$t_1))
head (na.omit(green_nir_df02$t_1, 20))

#convert the timestep_1 for nir to numeric value
green_nir_df02$t_1.1 <- as.numeric(as.character(green_nir_df02$t_1.1))
head (na.omit(green_nir_df02$t_1.1, 20))

# calculate green minus nir for timestep_1
grnMinusNir_t_1 <- green_nir_df02$t_1 - green_nir_df02$t_1.1
head(grnMinusNir_t_1, 20)

# calculate green plus nir for timestep_1
grnPlusNir_t_1 <- green_nir_df02$t_1 + green_nir_df02$t_1.1
head(grnPlusNir_t_1, 20)

# calculate ndwi from greenMinusNir divided by greenPlusNir for timestep_1
ndwi_t_1 <- grnMinusNir_t_1 / grnPlusNir_t_1
head(ndwi_t_1, 20)

# write ndwi to the green_nir_df02 dataframe for timestep_1
green_nir_df02$ndwi_t_1 <- ndwi_t_1

####################################################
#convert the timestep_2 for green to numeric value
green_nir_df02$t_2 <- as.numeric(as.character(green_nir_df02$t_2))
head (na.omit(green_nir_df02$t_2, 20))

#convert the timestep_2 for nir to numeric value
green_nir_df02$t_2.1 <- as.numeric(as.character(green_nir_df02$t_2.1))
head (na.omit(green_nir_df02$t_2.1, 20))

# calculate green minus nir for timestep_2
grnMinusNir_t_2 <- green_nir_df02$t_2 - green_nir_df02$t_2.1
head(grnMinusNir_t_2, 20)

# calculate green plus nir for timestep_2
grnPlusNir_t_2 <- green_nir_df02$t_2 + green_nir_df02$t_2.1
head(grnPlusNir_t_2, 20)

# calculate ndwi from greenMinusNir divided by greenPlusNir for timestep_2
ndwi_t_2 <- grnMinusNir_t_2 / grnPlusNir_t_2
head(ndwi_t_2, 20)

# write ndwi to the green_nir_df02 dataframe for timestep_2
green_nir_df02$ndwi_t_2 <- ndwi_t_2

...等到t_60

###############################################################
# END: Calculate ndwi for for the each timestep
###############################################################

下一个文件从t_61开始,依此类推

我尝试使用此循环,该循环不起作用,因为我无法指向数据帧中t_1列中的值,而不是列名数组中的值。

# initialize the "for" loop to calculate ndwi columns
seq <- 1:nt
i <- 0
green_t <- array(1:nt)
nir_t <- array(1:nt)
ndwi_t <- array(1:nt)
greenMinusNir <- array(1:nt)
greenPlusNir <- array(1:nt)
ndwi <- array(1:nt)
# set the value of the count
# count = nt from previous file #nt = num timesteps#THIS IS THE VALUE TO USE
count <- 0 ################################## EDIT THIS VALUE BEFORE RUNNING
# START: Loop for each timestep
# Step 1: create variable names for green, nir, ndwi
# Step 2: convert t_[i] and t_[i].1 to numeric
# Step 3: calculate green minus nir
# Step 4: calculate green plus nir
# Step 5: calculate ndwi = g-nir/g+nir
# Step 6: write ndwi to the green_nir_df02 dataframe
for(i in seq){
    # Step 1: create variable names for green, nir, ndwi
    green_t[i] <- paste("green_nir_df02$t_",count+i,sep="")
    nir_t[i] <- paste("green_nir_df02$t_",count+i,".1",sep="")
    ndwi_t[i] <- paste("green_nir_df02$ndwi_t_",count+i,sep="")
}
# initialize the "for" loop to calculate ndwi columns
seq <- 1:nt
i <- 0
for(i in seq){
    # Step 2: convert green (i.e., t_[i]) and nir (i.e., t_[i].1) to numeric
    green <- as.numeric(as.character(green_t[i]))
    ##    
    #ERROR when i=1 > green <-as.numeric(as.character("green_nir_df02$t_1"))
    ## 
    nir_t <- as.numeric(as.character(nir_t))

    # Step 3: calculate green minus nir
    greenMinusNir[i] <- green_t - nir_t

    # Step 4: calculate green plus nir
    greenPlusNir[i] <- green_t[i] + nir_t[i]

    # Step 5: calculate ndwi = g-nir/g+nir
    ndwi[i] <- greenMinusNir[i] / greenPlusNir[i]

    # Step 6: write ndwi to the green_nir_df02 dataframe ndwi timestep
    ndwi_t[i] <- ndwi[i]
    # i <- i+1
}

样本数据

            lon_lat t_1 t_2 t_60 t_1.1 t_2.1  t_60.1  ndwi_t_1   ndwi_t_2

1 -1609787.5_-2275087.5 180 216 247 80 197 192 0.3846154 0.04600484

2 -1609762.5_-2275087.5 102252281 80197227 0.1208791 0.12249443

3 -1609737.5_-2275087.5 102216281 80156227 0.1208791 0.16129032

4 -1609712.5_-2275087.5 141216281 80156227 0.2760181 0.16129032

5 -1609687.5_-2275087.5 180 181 281 80 156 227 0.3846154 0.07418398

6 -1609662.5_-2275087.5 180216281 80197227 0.3846154 0.04600484

0 个答案:

没有答案