Data in R

To run your code, click run. It will let you know if your code is correct or not, and will offer hints on what to fix if needed. If you are stuck, you can see the solution by clicking Solution.

Exercise 1: Using mutate() and lag()

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6ImxpYnJhcnkoZHBseXIpICMgbG9hZHMgZHBseXIgZm9yIGRhdGEgbWFuaXB1bGF0aW9uXG5kZiA8LSBkYXRhLmZyYW1lKGNvbXBhbnk9YyhcIlVPTCBHcm91cFwiLCBcIlVPTCBHcm91cFwiLCBcIlVPTCBHcm91cFwiKSxcbiAgICAgICAgICAgICAgICAgeWVhcj1jKDIwMTAsIDIwMTEsIDIwMTIpLFxuICAgICAgICAgICAgICAgICByZXZlbnVlPWMoMTI5NSwgMTk2MCwgMTE0NiksXG4gICAgICAgICAgICAgICAgIGFzc2V0cz1jKDc5MzksIDg1MjQsIDk1NjQpLFxuICAgICAgICAgICAgICAgICBlYXJuaW5ncz1jKDc0NiwgNjY0LCA4MDgpKVxuZGYgPC0gZGYgJT4lXG4gIG11dGF0ZShyb2E9ZWFybmluZ3MvYXNzZXRzKSIsInNhbXBsZSI6IiMgVGhlIGRhdGFmcmFtZSBiZWxvdyBjb250YWlucyBoaXN0b3JpY2FsIGVhcm5pbmdzLCByZXZlbnVlLCBhc3NldHMsXG4jIGFuZCByb2EgZm9yIDMgeWVhcnMgKDIwMTAsIDIwMTEsIGFuZCAyMDEyKSBhbmQgMyBmaXJtcy5cbmRmXG5cbiMgZHBseXIgaXMgYWxyZWFkeSBsb2FkZWRcblxuIyBDYWxjdWxhdGUgdGhlIHJldHVybiBvbiBhc3NldHMgMSB5ZWFyIGFnbywgdXNpbmcgbXV0YXRlIGFuZCBsYWcuXG4jIFN0b3JlIHRoZSByZXN1bHQgaW4gYSBkYXRhIGZyYW1lIGNvbHVtbiBuYW1lZCByb2FfbGFnLlxuXG5cbiNFTkQiLCJzb2x1dGlvbiI6IiMgVGhlIGRhdGFmcmFtZSBiZWxvdyBjb250YWlucyBoa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