简体   繁体   English

使用上一行信息在R中循环文件

[英]Using the upper row information to loop the file in R

I want to calculate snow storage for the daily time step and add the previous day information and written simple code. 我想计算每日时间段的积雪量,并添加前一天的信息并编写简单的代码。 The data looks like this 数据看起来像这样

elev = seq(550, 1000, 50)
# Elevation in sequence 
D <- read.csv('data.csv', stringsAsFactors=T, header=T)
# Data File 
Z <- 550

#Elevation 
> head(D)
#Table Data 

   Date        T_min T_max  P   J_day   Prain  Psnow  **Snow accum**
1 1995/08/01  -4     -2    0.4   213      0.2   0.2    **0.2**
2 1995/08/02  -12    -6    0.0   214      0.0   0.0    **0.2**
3 1995/08/03  -5      2    4.2   215      2.6   1.2    **1.4**
4 1995/08/04  -2      5    3.2   216      3.0   0.2    **1.6**
5 1995/08/05  -8     -3    0.0   217      0.0   0.0    **0.0**

I distributed T_max and T_min and P snow in each elevation zone using the loop 我使用循环在每个高程区域中分配了T_max和T_min以及P snow

C_Temp = array(dim=c(length(D$T_max), length(elev)))

C_Ppt = array(dim=c(length(D$P),length(elev)))

for (i in 1:length(elev)){
      C_Temp[,i] = D$T_max - Tg*(elev[i]-Z)}

for (i in 1:length(elev)){
      C_Ppt[,i] = D$P*(1+ Pg*(elev[i]-Z))}

Similarly, I want to calculate the Snow accumulation separating snow and rain using the code shown in table above.As in format it is not cumulative addition and snow melt depends on temperature as well. 同样,我想使用上表中的代码来计算将雪和雨分开的积雪量。由于格式不是累加,积雪融化也取决于温度。

SS<-array(dim=c(length(D$P), length(elev)))

for (i in 1:length(elev)){
     SS[i+1]<-PG[i-1]+PG }

Where PG is the Psnow and SS is the snow accumulation (Snow accum). 其中PG是Psnow,SS是积雪(Snow accum)。

I get an error: 我收到一个错误:

dims [product 36530] do not match the length of object [0] 暗淡[产品36530]与物体[0]的长度不匹配

I don't know how to use previous row information, ie 0.2 of Psnow, to calculate the S accumulation for next day using loop and since the file is very big I cannot do it manually. 我不知道如何使用先前的行信息(即Psnow的0.2)来计算使用循环的第二天的S累积,并且由于文件很大,所以我无法手动执行。

Let df be your data frame. df为您的数据帧。 Than this can be achieved as 比这可以实现

df
   structure(list(Date = structure(c(1L, 2L, 3L, 4L, 5L, 5L, 5L), .Label = c("8/1/1995", 
"8/2/1995", "8/3/1995", "8/4/1995", "8/5/1995"), class = "factor"), 
    T_min = c(-4L, -12L, -5L, -2L, -8L, 8L, -7L), T_max = c(-2L, 
    -6L, 2L, 5L, -3L, -3L, -3L), P = c(0.4, 0, 4.2, 3.2, 0, 0, 
    0), J_day = c(213L, 214L, 215L, 216L, 217L, 217L, 217L), 
    Prain = c(0.2, 0, 2.6, 3, 0, 0, 0), Psnow = c(0.2, 0, 1.2, 
    0.2, 0, 5, 10), X..Snow = structure(c(2L, 2L, 3L, 4L, 1L, 
    1L, 1L), .Label = c("**0.0**", "**0.2**", "**1.4**", "**1.6**"
    ), class = "factor")), .Names = c("Date", "T_min", "T_max", 
"P", "J_day", "Prain", "Psnow", "X..Snow"), class = "data.frame", row.names = c(NA, 
-7L))
head(df)
# Get all the rows satisfying your condition
df_snow<-subset(df, df$T_min<0)
df_snow
# Get columnwise cumulative sum
df_snow$AccuSnow<-cumsum(df_snow$Psnow)
df_snow

# If you want, you can merge now
df.final<-merge(df, df_snow, all=T)
df.final

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM