Home > Back-end >  R equivalent of performing operations on an empty list in Python?
R equivalent of performing operations on an empty list in Python?

Time:10-15

I have been trying to replicate code in python to the R equivalent, but am having some troubles. I am trying to add a vector to an empty list, it seems like this can be easily done in python, but finding the correct empty list in R is challenging.

I think the issue with the R equivalent is determining the correct empty list for S1 with the same vector length as P and E, as python seems to do this stress free. I have tried using vector() and rep() functions and specifying the desired length of 731, but R wont let me perform any operations on it and the error message is rightly Error in S1[i - 1] DS : non-numeric argument to binary operator. Was there a simple way to make an empty list numeric in this regard?

Note that the depicted P and E correspond to external rainfall data (from a CSV) with a length of 731.

The python equivalent is shown below:

A1 = float(0.134) 
A2 = float(0.433)     
A3 = float(0.433)
C1 = float(7)
C2 = float(70)  
C3 = float(150)

S1 = []; S2 = []; S3 = []
Total_Excess = []

for i in range(1,dims[0]):
    dS = P[i]-E[i] 
    
    S1_temp = max(S1[i-1] dS,0)
    S1_Excess = max(S1_temp-C1,0)
    S1.append(min(S1_temp,C1))
    
    S2_temp = max(S2[i-1] dS,0)
    S2_Excess = max(S2_temp-C2,0)
    S2.append(min(S2_temp,C2))
    
    S3_temp = max(S3[i-1] dS,0)
    S3_Excess = max(S3_temp-C3,0)
    S3.append(min(S3_temp,C3))
    
    Total_Excess.append((S1_Excess*A1) (S2_Excess*A2) (S3_Excess*A3))

The R code that I had written before I got stuck is shown below:

A1 <- 0.4
A2 <- 0.4
A3 <- 0.2

C1 <- 10
C2 <- 75
C3 <- 200

S1 <- list()
S2 <- list()
S3 <- list()

for(i in 1:nrow(df)){
  DS <- P - E
  
  S1_temp <- max(S1[i-1]  DS, 0)
  S1_excess <- max(S1_temp - C1)
  append(S1, min(S1_temp,C1))

  S2_temp <- max(S2[i-1]  DS, 0)
  S2_excess <- max(S1_temp - C1)
  append(S2, min(S1_temp,C1))
  ...

Once the first portion is run, I call S1_temp in the console and only get a vector of length = 1 back, which should evidently be of length = 731.

Also, how would I go about appending the Total_excess empty list to the resulting vector as seen in the last line of code in the python code example??

Any assistance would be greatly appreciated.

CodePudding user response:

The equivalent of Python S1 = []; S1.append(x) in R is S1 <- list(); S1 <- c(S1, list(x)) in R.

In your example c(S1, x) will work because the numeric value you are trying to append will be automatically wrapped in a list, but it's safer to do it explicitly. If x is already a list, then c(S1, x) will append its elements to S1, while c(S1, list(x)) will append a single entry containing a copy of x to S1.

You could use the append() function in R, but then remember that it's rare for R functions to modify their arguments, so you would write

S1 <- append(S1, list(x))

In this situation it's essentially identical to c().

  • Related