difference in consecutive rows on data frames in a list

Viewed 58

I have a list of data frames called mydata. Each data frame contains information about a student's performance over various years for each subject. one of the data frames looks like this:

   X12.31.2008 X5.85 X15.785 X53293 X774157 X3988 X40992
1   12/31/2009  4.45   9.867  56490  710796  2340  43158
2   12/31/2010  4.55  11.333  62149  725957  3262  47962
3   12/31/2011  6.84  13.496  68405  786719  4026  53815
4   12/31/2012  6.84  11.804  84445  898371  4439  64900
5   12/31/2013  8.20  14.140  85201  962863  5222  64702
6   12/31/2014  9.25  15.601  90945  991414  5573  70239
7   12/31/2015 10.00  16.924  98647 1144604  5899  75785
8   12/31/2016 10.30  17.375 102280 1101023  5835  81604
9   12/31/2017 10.70  16.657 119258 1169595  5882  92110
10  12/31/2018 11.10  16.765 121606 1288744  6282  91278
11  12/31/2019 11.25  17.176 128683 1399175  5772  95395

I want to add a column to each data frame and this column contains the change in the percentage acquired from one year to another for a specific subject (Maths Percentage) say this is the 4th column ( the one starting with. For example, I want the new column to be 2008 value minus 2007 value. the next value being 2009 value minus 2008 value. and do this for each data frame in the list. I used col.names to try to remove the column names since I kept getting an error that I had duplicate names when i had column names

I tried using this code:

col_change<- function(x){
  positions<- length(x)
  the_change = NULL
  the_change[1]=0
  for (i in 1: (positions-1)){
    the_change[i+1]=x[i+1]-x[i]
  }
  return(the_change)
}
#class(the_change) 
for (i in 1: length(mydata)){
  mydata[[i]]= cbind(mydatas[[i]],col_change(mydata[[i]][,4])) 
  
}

but I get an error saying Error in x[i + 1] - x[i] : non-numeric argument to binary operator I don't know what is wrong. I also tried to use mutate and lag but it also gave me errors.

how can I do this differencing?

1 Answers

Update

Seems you want this

> df$D <- c(0, diff(df[[4]])
> df
   X12.31.2008 X5.85 X15.785 X53293 X774157 X3988 X40992     D
1   12/31/2009  4.45   9.867  56490  710796  2340  43158     0
2   12/31/2010  4.55  11.333  62149  725957  3262  47962  5659
3   12/31/2011  6.84  13.496  68405  786719  4026  53815  6256
4   12/31/2012  6.84  11.804  84445  898371  4439  64900 16040
5   12/31/2013  8.20  14.140  85201  962863  5222  64702   756
6   12/31/2014  9.25  15.601  90945  991414  5573  70239  5744
7   12/31/2015 10.00  16.924  98647 1144604  5899  75785  7702
8   12/31/2016 10.30  17.375 102280 1101023  5835  81604  3633
9   12/31/2017 10.70  16.657 119258 1169595  5882  92110 16978
10  12/31/2018 11.10  16.765 121606 1288744  6282  91278  2348
11  12/31/2019 11.25  17.176 128683 1399175  5772  95395  7077

Do you want this?

cbind(
  Date = df[-1, 1],
  df[-1, -1] / df[-nrow(df), -1] - 1
)

where you will get

         Date      X5.85      X15.785      X53293     X774157        X3988
2  12/31/2010 0.02247191  0.148576062 0.100177022  0.02132961  0.394017094
3  12/31/2011 0.50329670  0.190858555 0.100661314  0.08369917  0.234212140
4  12/31/2012 0.00000000 -0.125370480 0.234485783  0.14192107  0.102583209
5  12/31/2013 0.19883041  0.197899017 0.008952573  0.07178771  0.176391079
6  12/31/2014 0.12804878  0.103323904 0.067417049  0.02965219  0.067215626
7  12/31/2015 0.08108108  0.084802256 0.084688548  0.15451668  0.058496322
8  12/31/2016 0.03000000  0.026648546 0.036828287 -0.03807518 -0.010849296
9  12/31/2017 0.03883495 -0.041323741 0.165995307  0.06228026  0.008054841
10 12/31/2018 0.03738318  0.006483761 0.019688407  0.10187202  0.068004080
11 12/31/2019 0.01351351  0.024515359 0.058196142  0.08568886 -0.081184336
         X40992
2   0.111311924
3   0.122034110
4   0.205983462
5  -0.003050847
6   0.085576953
7   0.078958983
8   0.076783005
9   0.128743689
10 -0.009032678
11  0.045103968
Related