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?