2

I have many variables having many observations. I have one standard variable. Now, I want to calculate the difference between the standard and observed variables only when the observation is bigger than the standard and summing up all the differences as a separate variables. In addition, the names of the variables that are bigger than the standard in a separate column.

Names   Standard    Das Dss Tri Tet
Aa  32  42  21  45  34
Ab  23  25  43  43  32
Ac  43  34  23  32  23
Ad  23  24  33  12  23
Ae  14  24  12  20  24
Af  43  42  13  12  43
Ag  12  13  22  13  22
Ah  32  32  42  42  23

OUTPUT:

Names   Standard    Das Dss Tri Tet Difference  No_Difference   Names_Difference
Aa  32  42  21  45  34  15  3   Das, Tri, Tet
Ab  23  25  43  43  32  52  4   Das,Dss,Tri,Tet
Ac  43  34  23  32  23  0   0   NA
Ad  23  24  33  12  23  10  2   Das,Dss
Ae  14  24  12  20  24  26  4   Das,Tri,Tet
Af  43  42  13  12  43  0   0   NA
Ag  12  13  22  13  22  22  4   Das,Dss,Tri,Tet
Ah  32  32  42  42  23  20  2   Dss,Tri
2
  • Your output is incomplete, isn't? It is not clear if you sum only the positive difference?
    – agstudy
    Dec 15, 2012 at 20:00
  • @agstudy, Yes its something like that, row-wise, Observation-Standard=+ve, than sum all the +ve values. I hope now it is clear. In addition the names of the columns that have bigger observation than the respective standard values. So, that I could see at which site the observation is positive. Thanks for the help
    – Gongon
    Dec 15, 2012 at 20:23

2 Answers 2

3

Let's say your data from above is stored in the object df. Then this should work

df2 <- do.call(rbind, apply(df[, -1], 1, function(z) {
        ind <- z[2:5] > z[1]
        return(cbind.data.frame(
                        Difference = sum(z[2:5][ind] - z[1]), 
                        No_Difference = sum(ind), 
                        Names_Difference = paste(colnames(df[3:6])[ind], 
                                collapse = ", ")
                ))
    }))

df <- cbind(df, df2)
df

  Names Standard Das Dss Tri Tet Difference No_Difference   Names_Difference
1    Aa       32  42  21  45  34         25             3      Das, Tri, Tet
2    Ab       23  25  43  43  32         51             4 Das, Dss, Tri, Tet
3    Ac       43  34  23  32  23          0             0                   
4    Ad       23  24  33  12  23         11             2           Das, Dss
5    Ae       14  24  12  20  24         26             3      Das, Tri, Tet
6    Af       43  42  13  12  43          0             0                   
7    Ag       12  13  22  13  22         22             4 Das, Dss, Tri, Tet
8    Ah       32  32  42  42  23         20             2           Dss, Tri

Altough this is not really elegant and it's not very robust to use integers to index columns, in case the ordering of your variables changes at some point

9
  • @ adibender thanks for your reply but it is not giving me the right output. Probably my question was not in right way. I need to calculate the difference row-wise. For each value of standard column compare to the observed columns (i.e. Das, Dss, Tri, Tet) and if observed value is bigger calculate the difference and sum all the differences of each row separately and make a new column with name "Difference" and in addition the names of those columns as well. Thanks
    – Gongon
    Dec 15, 2012 at 21:58
  • @maria raz: can u give an example for one row to calculate the difference, for example the first one?
    – adibender
    Dec 15, 2012 at 22:20
  • I thought it would be (42 - 32) + (45 - 32) + (34 - 32) for the first row
    – adibender
    Dec 15, 2012 at 22:28
  • @ adibender you are right for the first row it should be like that.. and difference should be 25. I just realized that in my example there is a mistake and the result is given as 15, that should have been 25. But yes you get the idea right. Sorry and Thanks for your help..
    – Gongon
    Dec 15, 2012 at 22:42
  • @mariariaz what exactly is wrong with my solution then? which row is wrong? I think you might have some mistakes in other rows too in your example...
    – adibender
    Dec 15, 2012 at 23:19
2

To add only those values where standard val is less than obs, here's an easy way. My example is for a single row, so I'll use a vector.

> foo<- sample(10,10,replace=TRUE)
> foo
 [1]  7  5 10  8  8  7  4  1  8  2
> sum((foo[-1]-foo[1])*(foo[-1]>foo[1]))
 [1] 6

Someone else can do the name-collecting :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.