I have the following data frame:
df<- structure(list(Group.1 = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6,
6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
10, 10, 10, 10, 10, 10), species = c("BLC", "BLG", "LMB", "RSF",
"GSF", "CCF", "BLC", "BLG", "LMB", "RSF", "GSF", "CCF", "BLC",
"BLG", "LMB", "RSF", "GSF", "CCF", "BLC", "BLG", "LMB", "RSF",
"GSF", "CCF", "BLC", "BLG", "LMB", "RSF", "GSF", "CCF", "BLC",
"BLG", "LMB", "RSF", "GSF", "CCF", "BLC", "BLG", "LMB", "RSF",
"GSF", "CCF", "BLC", "BLG", "LMB", "RSF", "GSF", "CCF", "BLC",
"BLG", "LMB", "RSF", "GSF", "CCF", "BLC", "BLG", "LMB", "RSF",
"GSF", "CCF"), values = c(72.2343402025878, 1937.03733692026,
623.838638214881, 174.419716477824, 12.3049226613653, 2.895855065589,
37.3321018704224, 935.439181611622, 298.304837910605, 92.7749414559832,
5.49833383606564, 1.70727510450712, 23.6566247165478, 597.966655653905,
220.745420709475, 53.765944830084, 3.7897932631262, 1.05347148538598,
15.8942359026861, 454.165172018186, 148.573525029039, 39.4075327404161,
2.80118066589066, 0.889178275811525, 11.7014228325682, 342.66358744176,
117.532778999925, 29.8761721691445, 1.95309425402833, 0.623954928025089,
10.2405988565389, 267.872477176734, 95.5525373175992, 22.7819723416179,
1.59375454536288, 0.486342316189338, 8.12750074644614, 210.566179254588,
73.7051782364627, 18.750461871616, 1.41145646104497, 0.397612482117296,
7.14018246550655, 191.420750094594, 62.4641473433447, 16.179045656704,
1.08732558991936, 0.314392014172493, 5.71225405584123, 162.986379338076,
56.0946790420074, 13.8012802889254, 0.959386008911135, 0.258095053602248,
4.65575331835703, 129.557048689593, 45.1406344140552, 11.8447584407366,
0.813656130711877, 0.22946385972495)), row.names = c(NA, -60L
), class = c("tbl_df", "tbl", "data.frame"))
I am trying to find the percent change across species, by Group.1. If I understand the use of lead and lag correctly, I should be using lag given that my Group.1 column is in numerical order. The final product should have Na's for all of group 1 because that's the starting group and there's no change between group 1 and itself.
Group 2 should be the difference between group 1 & 2. Group 3= change between group 2 & 3 and so on...
After looking at few different posts I tried a few variations of:
test_new<- df %>%
group_by(Group.1, species) %>%
mutate(percent_change= (values/lag(values)-1)*100)
But this returns NAs for all the values. Anyone know where I'm going wrong?