0

Thank you jakub and Hack-R! Yes, these are my actual data. The data I am starting from are the following:

 [A] #first, longer dataset 
 CODE_t2 VALUE_t2
 111      3641
 112      1691
 121      1271
 122      185
 123      522
 124      0
 131      0
 132      0
 133      0
 141      626
 142      170
 211      0
 212      0
 213      0
 221      0
 222      0
 223      0
 231      95
 241      0
 242      0
 243      0
 244      0
 311      129
 312      1214
 313      0
 321      0
 322      0
 323      565
 324      0
 331      0
 332      0
 333      0
 334      0
 335      0
 411      0
 412      0
 421      0
 422      0
 423      0
 511      6
 512      0
 521      0
 522      0
 523      87

In the above table, we can see the 44 land use CODES (which I inappropriately named "class" in my first entry) for a certain city. Some values are just 0, meaning that there are no land uses of that type in that city. Starting from this table, which displays all the land use types for t2 and their corresponding values ("VALUE_t2") I have to reconstruct the previous amount of land uses ("VALUE_t1") per each type.

To do so, I have to add and subtract the value per each land use (if not 0) by using the "change land use table" from t2 to t1, which is the following:

 [B] #second, shorter dataset 
 CODE_t2   CODE_t1   VALUE_CHANGE1
 121         112       2
 121         133       12
 121         323       0
 121         511       3
 121         523       2
 123         523       4
 133         123       3
 133         523       4
 141         231       12
 141         511       37

So, in order to get VALUE_t1 from VALUE_t2, I have, for instance, to subtract 2 + 12 + 0 + 3 + 2 hectares (first 5 values of the second, shorter table) from the value of land use type/code 121 of the first, longer table (1271 ha), and add 2 hectares to land type 112, 12 hectares to land type 133, 3 hectares to land type 511 and 2 hectares to land type 523. And I have to do that for all the land use types different than 0, and later also from t1 to t0.

What I have to do is a sort of loop that would both add and subtract, per each land use type/code, the values from VALUE_t2 to VALUE_t1, and from VALUE_t1 to VALUE_t0.

Once I estimated VALUE_t1 and VALUE_t0, I will put the values in a simple table showing the relative variation (here the values are not real):

 CODE    VALUE_t0    VALUE_t2     % VAR t2-t0
 code1      50         100        ((100-50)/50)*100
 code2      70         80         ((80-70)/70)*100
 code3      45         34         ((34-45)/45)*100

What I could do so far is:

 land_code <- names(A)[-1]
 land_code
 A$VALUE_t1 <-  for(code in land_code{
 cbind(A[1], A[land_code] - B[match(A$CODE_t2, B$CODE_t2), land_code])
 }

If I use the loop I get an error, while if I take it away:

 A$VALUE_t1 <-  cbind(A[1], A[land_code] - B[match(A$CODE_t2, B$CODE_t2), land_code])

it works but I don't really get what I want to get... so far I was working on how to get a new column which would contain the new "add & subtract" values, but haven't succeeded yet. So I worked on how to get a new column which would at least match the land use types first, to then include the "add and subtract" formula.

Another problem is that, by using "match", I get a shorter A$VALUE_t1 table (13 rows instead of 44), while I would like to keep all the land use types in dataset A, because I will have then to match it with the table including VALUES_t0 (which I haven't shown here).

Sorry that I cannot do better than this at the moment... and I hope to have explained better what I have to do. I am extremely grateful for any help you can provide to me.

thanks a lot

  • Welcome to SO! Is any of the tables you provided your *actual* data? Because I'm not able to tell which columns you have and which ones you want to calculate. Include a sample of your data, and the code you have used so far - this will make it easier for people to help you. (Also [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610).) – jakub Sep 11 '16 at 12:43
  • Yes, please provide sufficient data to reproduce the problem. By the way, I notice that you referred to values in the first column as "CODE", "land use type", and "class". The word "class" was used repeatedly without context. Since the column is named "CODE" and in R programming "class" typical refers to a data type, I would recommend consistently referring to this as "CODE" or at least explaining how you're using the word "class". – Hack-R Sep 11 '16 at 14:00

0 Answers0