如何按自定义日期(月、日、年)范围分组并跨时间组创建季节并获取摘要?
How to group by a custom date (month, day, year) range and create seasons across the time group and get summary?
我正在寻找一种按特定日期范围(即从特定月、日、年到下一个月、日、年)分组的简单方法。我还需要每年重复相同的日期范围(例如,1980-06-01
到 1981-05-31
然后 1981-06-01
到 1982-05-31
,等等)。此外,我想为每年定义季节,冬季将跨年出现(例如,1980 年 12 月至 1981 年 2 月)。这是一个例子:
数据
df <- structure(list(date = structure(c(3652, 3653, 3654, 3655, 3656,
3657, 3658, 3659, 3660, 3661, 3662, 3663, 3664, 3665, 3666, 3667,
3668, 3669, 3670, 3671, 3672, 3673, 3674, 3675, 3676, 3677, 3678,
3679, 3680, 3681, 3682, 3683, 3684, 3685, 3686, 3687, 3688, 3689,
3690, 3691, 3692, 3693, 3694, 3695, 3696, 3697, 3698, 3699, 3700,
3701, 3702, 3703, 3704, 3705, 3706, 3707, 3708, 3709, 3710, 3711,
3712, 3713, 3714, 3715, 3716, 3717, 3718, 3719, 3720, 3721, 3722,
3723, 3724, 3725, 3726, 3727, 3728, 3729, 3730, 3731, 3732, 3733,
3734, 3735, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3744,
3745, 3746, 3747, 3748, 3749, 3750, 3751, 3752, 3753, 3754, 3755,
3756, 3757, 3758, 3759, 3760, 3761, 3762, 3763, 3764, 3765, 3766,
3767, 3768, 3769, 3770, 3771, 3772, 3773, 3774, 3775, 3776, 3777,
3778, 3779, 3780, 3781, 3782, 3783, 3784, 3785, 3786, 3787, 3788,
3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799,
3800, 3801, 3802, 3803, 3804, 3805, 3806, 3807, 3808, 3809, 3810,
3811, 3812, 3813, 3814, 3815, 3816, 3817, 3818, 3819, 3820, 3821,
3822, 3823, 3824, 3825, 3826, 3827, 3828, 3829, 3830, 3831, 3832,
3833, 3834, 3835, 3836, 3837, 3838, 3839, 3840, 3841, 3842, 3843,
3844, 3845, 3846, 3847, 3848, 3849, 3850, 3851, 3852, 3853, 3854,
3855, 3856, 3857, 3858, 3859, 3860, 3861, 3862, 3863, 3864, 3865,
3866, 3867, 3868, 3869, 3870, 3871, 3872, 3873, 3874, 3875, 3876,
3877, 3878, 3879, 3880, 3881, 3882, 3883, 3884, 3885, 3886, 3887,
3888, 3889, 3890, 3891, 3892, 3893, 3894, 3895, 3896, 3897, 3898,
3899, 3900, 3901, 3902, 3903, 3904, 3905, 3906, 3907, 3908, 3909,
3910, 3911, 3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920,
3921, 3922, 3923, 3924, 3925, 3926, 3927, 3928, 3929, 3930, 3931,
3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942,
3943, 3944, 3945, 3946, 3947, 3948, 3949, 3950, 3951, 3952, 3953,
3954, 3955, 3956, 3957, 3958, 3959, 3960, 3961, 3962, 3963, 3964,
3965, 3966, 3967, 3968, 3969, 3970, 3971, 3972, 3973, 3974, 3975,
3976, 3977, 3978, 3979, 3980, 3981, 3982, 3983, 3984, 3985, 3986,
3987, 3988, 3989, 3990, 3991, 3992, 3993, 3994, 3995, 3996, 3997,
3998, 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008,
4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019,
4020, 4021, 4022, 4023, 4024, 4025, 4026, 4027, 4028, 4029, 4030,
4031, 4032, 4033, 4034, 4035, 4036, 4037, 4038, 4039, 4040, 4041,
4042, 4043, 4044, 4045, 4046, 4047, 4048, 4049, 4050, 4051, 4052,
4053, 4054, 4055, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063,
4064, 4065, 4066, 4067, 4068, 4069, 4070, 4071, 4072, 4073, 4074,
4075, 4076, 4077, 4078, 4079, 4080, 4081, 4082, 4083, 4084, 4085,
4086, 4087, 4088, 4089, 4090, 4091, 4092, 4093, 4094, 4095, 4096,
4097, 4098, 4099, 4100, 4101, 4102, 4103, 4104, 4105, 4106, 4107,
4108, 4109, 4110, 4111, 4112, 4113, 4114, 4115, 4116, 4117, 4118,
4119, 4120, 4121, 4122, 4123, 4124, 4125, 4126, 4127, 4128, 4129,
4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4140,
4141, 4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151,
4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160, 4161, 4162,
4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4172, 4173,
4174, 4175, 4176, 4177, 4178, 4179, 4180, 4181, 4182, 4183, 4184,
4185, 4186, 4187, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195,
4196, 4197, 4198, 4199, 4200, 4201, 4202, 4203, 4204, 4205, 4206,
4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4217,
4218, 4219, 4220, 4221, 4222, 4223, 4224, 4225, 4226, 4227, 4228,
4229, 4230, 4231, 4232, 4233, 4234, 4235, 4236, 4237, 4238, 4239,
4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247, 4248, 4249, 4250,
4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4260, 4261,
4262, 4263, 4264, 4265, 4266, 4267, 4268, 4269, 4270, 4271, 4272,
4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282, 4283,
4284, 4285, 4286, 4287, 4288, 4289, 4290, 4291, 4292, 4293, 4294,
4295, 4296, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4305,
4306, 4307, 4308, 4309, 4310, 4311, 4312, 4313, 4314, 4315, 4316,
4317, 4318, 4319, 4320, 4321, 4322, 4323, 4324, 4325, 4326, 4327,
4328, 4329, 4330, 4331, 4332, 4333, 4334, 4335, 4336, 4337, 4338,
4339, 4340, 4341, 4342, 4343, 4344, 4345, 4346, 4347, 4348, 4349,
4350, 4351, 4352, 4353, 4354, 4355, 4356, 4357, 4358, 4359, 4360,
4361, 4362, 4363, 4364, 4365, 4366, 4367, 4368, 4369, 4370, 4371,
4372, 4373, 4374, 4375, 4376, 4377, 4378, 4379, 4380, 4381, 4382,
4383, 4384, 4385, 4386, 4387, 4388, 4389, 4390, 4391, 4392, 4393,
4394, 4395, 4396, 4397, 4398, 4399, 4400, 4401, 4402, 4403, 4404,
4405, 4406, 4407, 4408, 4409, 4410, 4411, 4412, 4413, 4414, 4415,
4416, 4417, 4418, 4419, 4420, 4421, 4422, 4423, 4424, 4425, 4426,
4427, 4428, 4429, 4430, 4431, 4432, 4433, 4434, 4435, 4436, 4437,
4438, 4439, 4440, 4441, 4442, 4443, 4444, 4445, 4446, 4447, 4448,
4449, 4450, 4451, 4452, 4453, 4454, 4455, 4456, 4457, 4458, 4459,
4460, 4461, 4462, 4463, 4464, 4465, 4466, 4467, 4468, 4469, 4470,
4471, 4472, 4473, 4474, 4475, 4476, 4477, 4478, 4479, 4480, 4481,
4482, 4483, 4484, 4485, 4486, 4487, 4488, 4489, 4490, 4491, 4492,
4493, 4494, 4495, 4496, 4497, 4498, 4499, 4500, 4501, 4502, 4503,
4504, 4505, 4506, 4507, 4508, 4509, 4510, 4511, 4512, 4513, 4514,
4515, 4516, 4517, 4518, 4519, 4520, 4521, 4522, 4523, 4524, 4525,
4526, 4527, 4528, 4529, 4530, 4531, 4532, 4533, 4534, 4535, 4536,
4537, 4538, 4539, 4540, 4541, 4542, 4543, 4544, 4545, 4546, 4547,
4548, 4549, 4550, 4551, 4552, 4553, 4554, 4555, 4556, 4557, 4558,
4559, 4560, 4561, 4562, 4563, 4564, 4565, 4566, 4567, 4568, 4569,
4570, 4571, 4572, 4573, 4574, 4575, 4576, 4577, 4578, 4579, 4580,
4581, 4582, 4583, 4584, 4585, 4586, 4587, 4588, 4589, 4590, 4591,
4592, 4593, 4594, 4595, 4596, 4597, 4598, 4599, 4600, 4601, 4602,
4603, 4604, 4605, 4606, 4607, 4608, 4609, 4610, 4611, 4612, 4613,
4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624,
4625, 4626, 4627, 4628, 4629, 4630, 4631, 4632, 4633, 4634, 4635,
4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4645, 4646,
4647, 4648, 4649, 4650, 4651, 4652, 4653, 4654, 4655, 4656, 4657,
4658, 4659, 4660, 4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668,
4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679,
4680, 4681, 4682, 4683, 4684, 4685, 4686, 4687, 4688, 4689, 4690,
4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700, 4701,
4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712,
4713, 4714, 4715, 4716, 4717, 4718, 4719, 4720, 4721, 4722, 4723,
4724, 4725, 4726, 4727, 4728, 4729, 4730, 4731, 4732, 4733, 4734,
4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 4744, 4745,
4746, 4747), class = "Date"), climate = c(39L, 4L, 7L, 8L, 87L,
8L, 12L, 9L, 26L, 69L, 18L, 89L, 53L, 47L, 70L, 94L, 26L, 51L,
93L, 91L, 71L, 32L, 21L, 35L, 2L, 12L, 97L, 50L, 58L, 55L, 93L,
8L, 99L, 97L, 25L, 44L, 60L, 81L, 3L, 66L, 64L, 26L, 61L, 53L,
97L, 7L, 54L, 78L, 62L, 30L, 28L, 82L, 23L, 81L, 39L, 66L, 62L,
13L, 77L, 8L, 22L, 17L, 89L, 6L, 16L, 6L, 3L, 99L, 57L, 23L,
15L, 14L, 7L, 78L, 24L, 50L, 83L, 86L, 44L, 25L, 92L, 63L, 87L,
68L, 42L, 5L, 24L, 61L, 22L, 79L, 44L, 92L, 74L, 48L, 90L, 1L,
96L, 28L, 97L, 88L, 56L, 83L, 92L, 66L, 76L, 23L, 95L, 26L, 34L,
93L, 8L, 24L, 40L, 90L, 85L, 18L, 55L, 71L, 81L, 12L, 97L, 35L,
69L, 55L, 53L, 94L, 26L, 85L, 22L, 87L, 25L, 31L, 8L, 1L, 7L,
72L, 65L, 62L, 5L, 49L, 75L, 40L, 38L, 22L, 57L, 94L, 13L, 55L,
99L, 64L, 90L, 56L, 65L, 83L, 73L, 69L, 9L, 14L, 28L, 49L, 1L,
38L, 55L, 78L, 58L, 48L, 55L, 65L, 37L, 85L, 75L, 84L, 84L, 59L,
19L, 62L, 37L, 23L, 74L, 94L, 51L, 31L, 64L, 24L, 42L, 24L, 76L,
71L, 95L, 21L, 18L, 67L, 53L, 38L, 60L, 97L, 8L, 80L, 67L, 1L,
28L, 91L, 73L, 99L, 60L, 68L, 12L, 52L, 28L, 32L, 82L, 10L, 58L,
4L, 56L, 49L, 72L, 90L, 16L, 92L, 38L, 32L, 77L, 69L, 18L, 68L,
55L, 78L, 56L, 36L, 21L, 21L, 91L, 63L, 10L, 2L, 97L, 16L, 56L,
89L, 9L, 53L, 23L, 28L, 28L, 41L, 72L, 42L, 10L, 97L, 100L, 79L,
62L, 28L, 64L, 15L, 25L, 42L, 29L, 26L, 70L, 12L, 98L, 93L, 92L,
12L, 38L, 57L, 37L, 51L, 8L, 38L, 71L, 42L, 37L, 76L, 21L, 2L,
3L, 30L, 76L, 78L, 48L, 46L, 22L, 84L, 36L, 9L, 65L, 7L, 62L,
51L, 38L, 31L, 18L, 53L, 1L, 89L, 57L, 19L, 37L, 57L, 66L, 65L,
39L, 46L, 70L, 86L, 50L, 92L, 99L, 10L, 74L, 33L, 52L, 37L, 45L,
85L, 6L, 77L, 54L, 97L, 98L, 98L, 65L, 39L, 3L, 6L, 58L, 13L,
29L, 69L, 32L, 59L, 8L, 12L, 18L, 14L, 39L, 73L, 42L, 62L, 70L,
75L, 57L, 65L, 77L, 88L, 40L, 91L, 6L, 20L, 27L, 67L, 45L, 14L,
57L, 1L, 73L, 40L, 14L, 40L, 95L, 46L, 78L, 11L, 69L, 22L, 41L,
49L, 47L, 99L, 2L, 20L, 8L, 91L, 11L, 85L, 18L, 73L, 22L, 2L,
64L, 12L, 43L, 34L, 8L, 38L, 30L, 55L, 31L, 49L, 1L, 66L, 76L,
49L, 89L, 74L, 62L, 70L, 23L, 26L, 42L, 81L, 49L, 26L, 70L, 75L,
78L, 18L, 51L, 84L, 47L, 39L, 24L, 31L, 51L, 11L, 96L, 46L, 46L,
78L, 53L, 82L, 5L, 77L, 18L, 6L, 45L, 92L, 22L, 72L, 100L, 6L,
41L, 1L, 9L, 84L, 19L, 31L, 8L, 96L, 24L, 98L, 60L, 43L, 50L,
27L, 4L, 75L, 83L, 48L, 10L, 8L, 85L, 39L, 50L, 27L, 43L, 8L,
83L, 92L, 70L, 84L, 78L, 51L, 67L, 26L, 63L, 43L, 90L, 60L, 6L,
27L, 9L, 58L, 63L, 29L, 5L, 77L, 99L, 72L, 17L, 22L, 40L, 92L,
22L, 11L, 70L, 94L, 31L, 93L, 6L, 93L, 62L, 84L, 62L, 21L, 62L,
13L, 35L, 47L, 29L, 12L, 57L, 100L, 6L, 30L, 14L, 23L, 78L, 72L,
39L, 55L, 71L, 56L, 76L, 37L, 54L, 19L, 58L, 94L, 78L, 64L, 100L,
44L, 46L, 40L, 47L, 23L, 88L, 70L, 24L, 38L, 27L, 35L, 53L, 64L,
31L, 50L, 16L, 76L, 94L, 73L, 10L, 73L, 78L, 96L, 42L, 5L, 87L,
64L, 14L, 86L, 9L, 59L, 27L, 48L, 24L, 79L, 77L, 25L, 19L, 43L,
46L, 14L, 39L, 48L, 28L, 29L, 73L, 4L, 79L, 58L, 92L, 30L, 30L,
51L, 35L, 11L, 36L, 33L, 35L, 43L, 66L, 19L, 66L, 48L, 44L, 90L,
75L, 32L, 2L, 64L, 46L, 86L, 55L, 16L, 35L, 59L, 51L, 56L, 9L,
84L, 90L, 23L, 17L, 45L, 20L, 3L, 7L, 43L, 46L, 26L, 52L, 75L,
33L, 22L, 96L, 14L, 45L, 1L, 4L, 92L, 69L, 85L, 21L, 20L, 100L,
28L, 8L, 77L, 29L, 77L, 55L, 72L, 42L, 52L, 79L, 51L, 46L, 21L,
61L, 19L, 21L, 49L, 96L, 33L, 61L, 33L, 69L, 62L, 91L, 58L, 3L,
11L, 66L, 89L, 59L, 22L, 50L, 75L, 3L, 82L, 1L, 26L, 53L, 47L,
94L, 57L, 10L, 81L, 98L, 19L, 38L, 92L, 52L, 91L, 6L, 42L, 76L,
39L, 21L, 89L, 19L, 3L, 37L, 93L, 61L, 35L, 18L, 90L, 25L, 48L,
73L, 24L, 51L, 58L, 1L, 58L, 8L, 8L, 92L, 51L, 85L, 17L, 68L,
98L, 64L, 60L, 64L, 65L, 3L, 23L, 76L, 84L, 24L, 59L, 40L, 58L,
43L, 21L, 3L, 17L, 36L, 90L, 86L, 41L, 65L, 99L, 12L, 11L, 64L,
63L, 24L, 46L, 79L, 97L, 70L, 12L, 1L, 77L, 46L, 17L, 15L, 86L,
4L, 32L, 10L, 67L, 95L, 68L, 67L, 21L, 69L, 2L, 66L, 29L, 80L,
42L, 76L, 85L, 62L, 56L, 4L, 36L, 42L, 73L, 100L, 90L, 99L, 15L,
46L, 37L, 78L, 29L, 35L, 14L, 27L, 23L, 16L, 56L, 57L, 87L, 57L,
80L, 89L, 63L, 63L, 37L, 80L, 71L, 43L, 12L, 39L, 51L, 4L, 66L,
91L, 84L, 8L, 88L, 33L, 24L, 87L, 54L, 65L, 5L, 31L, 61L, 86L,
1L, 2L, 85L, 66L, 82L, 24L, 80L, 78L, 22L, 54L, 72L, 64L, 61L,
91L, 64L, 16L, 38L, 50L, 27L, 47L, 43L, 54L, 79L, 64L, 37L, 6L,
32L, 94L, 32L, 43L, 99L, 13L, 86L, 32L, 85L, 94L, 99L, 26L, 19L,
11L, 15L, 20L, 38L, 58L, 73L, 6L, 7L, 93L, 56L, 48L, 28L, 70L,
8L, 53L, 90L, 36L, 95L, 96L, 98L, 98L, 57L, 40L, 47L, 26L, 50L,
82L, 98L, 69L, 22L, 52L, 86L, 15L, 99L, 76L, 35L, 46L, 87L, 14L,
40L, 68L, 29L, 58L, 32L, 26L, 68L, 68L, 62L, 95L, 6L, 65L, 16L,
64L, 88L, 52L, 89L, 55L, 86L, 86L, 41L, 39L, 62L, 65L, 36L, 40L,
87L, 46L, 66L, 88L, 25L, 49L, 58L, 44L, 1L, 36L, 48L, 56L, 64L,
92L, 94L, 8L, 61L, 84L, 90L, 38L, 42L, 1L, 69L, 98L, 51L, 64L,
80L, 79L, 7L, 33L, 33L, 20L, 35L, 63L, 56L, 96L, 1L, 93L, 30L,
39L, 86L, 78L, 18L, 20L, 59L, 80L, 100L, 100L, 11L, 39L, 72L,
55L, 4L, 42L, 50L, 77L, 64L, 67L, 40L, 100L, 6L, 9L, 67L, 67L,
41L, 57L, 68L, 90L, 38L, 63L, 53L, 85L, 52L, 86L, 49L, 43L, 17L,
35L, 45L, 66L, 5L, 100L, 51L, 30L, 95L, 43L, 94L, 87L, 88L, 23L,
49L, 67L, 5L, 78L, 4L, 8L, 30L, 42L, 18L, 38L, 13L, 1L, 68L,
23L, 27L, 86L, 72L, 39L, 80L, 5L, 87L, 12L, 40L, 43L, 65L, 33L,
42L, 23L, 87L, 19L, 44L, 84L, 58L, 18L, 20L, 49L, 50L, 70L, 99L,
92L, 91L, 25L, 62L, 73L, 43L, 83L, 47L, 38L, 86L, 58L, 7L, 15L,
85L, 38L, 87L, 97L, 69L, 59L, 9L, 9L, 29L, 99L, 35L, 87L, 19L,
2L, 52L, 44L, 81L, 73L, 10L, 83L, 89L, 17L, 58L, 57L, 14L, 42L,
77L, 73L, 47L, 71L, 25L, 96L, 11L, 31L, 6L, 47L, 15L, 71L, 75L
)), row.names = c(NA, -1096L), class = "data.frame")
对于我的示例,如果我正在计算年度摘要,那么我可以使用 group_by
中的 findInterval
(借用自 )来总结每年。不过我一般都有100年左右的数据,所以不想每个区间都写出来。
library(tidyverse)
df %>%
group_by(dr = findInterval(date, as.Date(c("1980-06-01", "1981-06-01", "1982-06-01")))) %>%
summarise(climate = sum(climate))
dr climate
<int> <int>
1 0 7665
2 1 17734
3 2 18172
4 3 11153
此外,我想获取每年每个季节的摘要。所以,我可以这样做:
df %>%
mutate(dr = findInterval(date, as.Date(
c("1980-06-01", "1981-06-01", "1982-06-01")
))) %>%
separate("date", c("year", "month", "day"), sep = "-") %>%
mutate(
month = str_replace(month, "^0+" , ""),
season = case_when(
month %in% c(3:5) ~ "spring",
month %in% c(6:8) ~ "summer",
month %in% c(9:11) ~ "fall",
month %in% c(12, 1:2) ~ "winter"
)
) %>%
group_by(dr, season) %>%
summarise(year = first(year), climate = sum(climate)) %>%
ungroup() %>%
select(dr, year, season, climate)
# A tibble: 13 × 4
# Groups: dr [4]
dr year season climate
<int> <chr> <chr> <int>
1 0 1980 spring 4744
2 0 1980 winter 2921
3 1 1980 fall 4392
4 1 1981 spring 4456
5 1 1980 summer 4687
6 1 1980 winter 4199
7 2 1981 fall 4330
8 2 1982 spring 4870
9 2 1981 summer 4420
10 2 1981 winter 4552
11 3 1982 fall 4622
12 3 1982 summer 4995
13 3 1982 winter 1536
不过,主要的问题还是在于年份的间隔。我还希望输出的顺序与原始数据框中的顺序相同。注:由于冬季是十二月到二月,因此年份应按十二月的年份分配。因此,在我的示例数据中,虽然数据从 1980 年 1 月开始,但我实际上需要将其设置为 1979 年,因为那年冬天开始了。所以,在这里,第一个冬天是唯一有这个问题的。
预期输出
dr year season climate
<int> <chr> <chr> <int>
1 0 1979 winter 2921
2 0 1980 spring 4744
3 1 1980 summer 4687
4 1 1980 fall 4392
5 1 1980 winter 4199
6 1 1981 spring 4456
7 2 1981 summer 4420
8 2 1981 fall 4330
9 2 1981 winter 4552
10 2 1982 spring 4870
11 3 1982 summer 4995
12 3 1982 fall 4622
13 3 1982 winter 1536
一种可能性是创建一个新的 corr.year
列,它确定季节开始的年份。我使用 corr.year
列进行分组而不是使用 findInterval
创建一个组列。但是,我觉得一定有更有效的方法。
library(tidyverse)
library(lubridate)
df %>%
mutate(corr.year=(date - days(59)) %>% floor_date("year") %>% year()) %>%
separate("date", c("year", "month", "day"), sep = "-") %>%
mutate(
month = str_replace(month, "^0+" , ""),
season = case_when(
month %in% c(3:5) ~ "spring",
month %in% c(6:8) ~ "summer",
month %in% c(9:11) ~ "fall",
month %in% c(12, 1:2) ~ "winter"
)
) %>%
group_by(corr.year, season) %>%
summarise(corr.year = first(corr.year), climate = sum(climate)) %>%
ungroup(season) %>%
arrange(corr.year, match(season, c("spring", "summer", "fall", "winter")))
输出
# A tibble: 13 × 3
# Groups: corr.year [4]
corr.year season climate
<dbl> <chr> <int>
1 1979 winter 2913
2 1980 spring 4744
3 1980 summer 4687
4 1980 fall 4392
5 1980 winter 4207
6 1981 spring 4456
7 1981 summer 4420
8 1981 fall 4330
9 1981 winter 4552
10 1982 spring 4870
11 1982 summer 4995
12 1982 fall 4622
13 1982 winter 1536
这首先标识了 2 个月前的那一天,它将所有内容都转移到季节开始的年份和年份的部分。然后它定义了一个有序的因子,其中包含季节名称,从 spring. count(x, wt = y, name = "name")
是 group_by(x) %>% summarize(name = sum(y))
的快捷方式,所以它在这里可以按年份和季节汇总气候数据。
library(lubridate)
df %>%
mutate(date_2mo_ago = date %m-% months(2),
season_num = quarter(date_2mo_ago),
season = c("spring", "summer", "fall", "winter")[season_num] %>%
forcats::fct_reorder(season_num),
year = year(date_2mo_ago)) %>%
count(year, season, wt = climate, name = "climate")
结果
year season climate
1 1979 winter 2921
2 1980 spring 4744
3 1980 summer 4687
4 1980 fall 4392
5 1980 winter 4199
6 1981 spring 4456
7 1981 summer 4420
8 1981 fall 4330
9 1981 winter 4552
10 1982 spring 4870
11 1982 summer 4995
12 1982 fall 4622
13 1982 winter 1536
我正在寻找一种按特定日期范围(即从特定月、日、年到下一个月、日、年)分组的简单方法。我还需要每年重复相同的日期范围(例如,1980-06-01
到 1981-05-31
然后 1981-06-01
到 1982-05-31
,等等)。此外,我想为每年定义季节,冬季将跨年出现(例如,1980 年 12 月至 1981 年 2 月)。这是一个例子:
数据
df <- structure(list(date = structure(c(3652, 3653, 3654, 3655, 3656,
3657, 3658, 3659, 3660, 3661, 3662, 3663, 3664, 3665, 3666, 3667,
3668, 3669, 3670, 3671, 3672, 3673, 3674, 3675, 3676, 3677, 3678,
3679, 3680, 3681, 3682, 3683, 3684, 3685, 3686, 3687, 3688, 3689,
3690, 3691, 3692, 3693, 3694, 3695, 3696, 3697, 3698, 3699, 3700,
3701, 3702, 3703, 3704, 3705, 3706, 3707, 3708, 3709, 3710, 3711,
3712, 3713, 3714, 3715, 3716, 3717, 3718, 3719, 3720, 3721, 3722,
3723, 3724, 3725, 3726, 3727, 3728, 3729, 3730, 3731, 3732, 3733,
3734, 3735, 3736, 3737, 3738, 3739, 3740, 3741, 3742, 3743, 3744,
3745, 3746, 3747, 3748, 3749, 3750, 3751, 3752, 3753, 3754, 3755,
3756, 3757, 3758, 3759, 3760, 3761, 3762, 3763, 3764, 3765, 3766,
3767, 3768, 3769, 3770, 3771, 3772, 3773, 3774, 3775, 3776, 3777,
3778, 3779, 3780, 3781, 3782, 3783, 3784, 3785, 3786, 3787, 3788,
3789, 3790, 3791, 3792, 3793, 3794, 3795, 3796, 3797, 3798, 3799,
3800, 3801, 3802, 3803, 3804, 3805, 3806, 3807, 3808, 3809, 3810,
3811, 3812, 3813, 3814, 3815, 3816, 3817, 3818, 3819, 3820, 3821,
3822, 3823, 3824, 3825, 3826, 3827, 3828, 3829, 3830, 3831, 3832,
3833, 3834, 3835, 3836, 3837, 3838, 3839, 3840, 3841, 3842, 3843,
3844, 3845, 3846, 3847, 3848, 3849, 3850, 3851, 3852, 3853, 3854,
3855, 3856, 3857, 3858, 3859, 3860, 3861, 3862, 3863, 3864, 3865,
3866, 3867, 3868, 3869, 3870, 3871, 3872, 3873, 3874, 3875, 3876,
3877, 3878, 3879, 3880, 3881, 3882, 3883, 3884, 3885, 3886, 3887,
3888, 3889, 3890, 3891, 3892, 3893, 3894, 3895, 3896, 3897, 3898,
3899, 3900, 3901, 3902, 3903, 3904, 3905, 3906, 3907, 3908, 3909,
3910, 3911, 3912, 3913, 3914, 3915, 3916, 3917, 3918, 3919, 3920,
3921, 3922, 3923, 3924, 3925, 3926, 3927, 3928, 3929, 3930, 3931,
3932, 3933, 3934, 3935, 3936, 3937, 3938, 3939, 3940, 3941, 3942,
3943, 3944, 3945, 3946, 3947, 3948, 3949, 3950, 3951, 3952, 3953,
3954, 3955, 3956, 3957, 3958, 3959, 3960, 3961, 3962, 3963, 3964,
3965, 3966, 3967, 3968, 3969, 3970, 3971, 3972, 3973, 3974, 3975,
3976, 3977, 3978, 3979, 3980, 3981, 3982, 3983, 3984, 3985, 3986,
3987, 3988, 3989, 3990, 3991, 3992, 3993, 3994, 3995, 3996, 3997,
3998, 3999, 4000, 4001, 4002, 4003, 4004, 4005, 4006, 4007, 4008,
4009, 4010, 4011, 4012, 4013, 4014, 4015, 4016, 4017, 4018, 4019,
4020, 4021, 4022, 4023, 4024, 4025, 4026, 4027, 4028, 4029, 4030,
4031, 4032, 4033, 4034, 4035, 4036, 4037, 4038, 4039, 4040, 4041,
4042, 4043, 4044, 4045, 4046, 4047, 4048, 4049, 4050, 4051, 4052,
4053, 4054, 4055, 4056, 4057, 4058, 4059, 4060, 4061, 4062, 4063,
4064, 4065, 4066, 4067, 4068, 4069, 4070, 4071, 4072, 4073, 4074,
4075, 4076, 4077, 4078, 4079, 4080, 4081, 4082, 4083, 4084, 4085,
4086, 4087, 4088, 4089, 4090, 4091, 4092, 4093, 4094, 4095, 4096,
4097, 4098, 4099, 4100, 4101, 4102, 4103, 4104, 4105, 4106, 4107,
4108, 4109, 4110, 4111, 4112, 4113, 4114, 4115, 4116, 4117, 4118,
4119, 4120, 4121, 4122, 4123, 4124, 4125, 4126, 4127, 4128, 4129,
4130, 4131, 4132, 4133, 4134, 4135, 4136, 4137, 4138, 4139, 4140,
4141, 4142, 4143, 4144, 4145, 4146, 4147, 4148, 4149, 4150, 4151,
4152, 4153, 4154, 4155, 4156, 4157, 4158, 4159, 4160, 4161, 4162,
4163, 4164, 4165, 4166, 4167, 4168, 4169, 4170, 4171, 4172, 4173,
4174, 4175, 4176, 4177, 4178, 4179, 4180, 4181, 4182, 4183, 4184,
4185, 4186, 4187, 4188, 4189, 4190, 4191, 4192, 4193, 4194, 4195,
4196, 4197, 4198, 4199, 4200, 4201, 4202, 4203, 4204, 4205, 4206,
4207, 4208, 4209, 4210, 4211, 4212, 4213, 4214, 4215, 4216, 4217,
4218, 4219, 4220, 4221, 4222, 4223, 4224, 4225, 4226, 4227, 4228,
4229, 4230, 4231, 4232, 4233, 4234, 4235, 4236, 4237, 4238, 4239,
4240, 4241, 4242, 4243, 4244, 4245, 4246, 4247, 4248, 4249, 4250,
4251, 4252, 4253, 4254, 4255, 4256, 4257, 4258, 4259, 4260, 4261,
4262, 4263, 4264, 4265, 4266, 4267, 4268, 4269, 4270, 4271, 4272,
4273, 4274, 4275, 4276, 4277, 4278, 4279, 4280, 4281, 4282, 4283,
4284, 4285, 4286, 4287, 4288, 4289, 4290, 4291, 4292, 4293, 4294,
4295, 4296, 4297, 4298, 4299, 4300, 4301, 4302, 4303, 4304, 4305,
4306, 4307, 4308, 4309, 4310, 4311, 4312, 4313, 4314, 4315, 4316,
4317, 4318, 4319, 4320, 4321, 4322, 4323, 4324, 4325, 4326, 4327,
4328, 4329, 4330, 4331, 4332, 4333, 4334, 4335, 4336, 4337, 4338,
4339, 4340, 4341, 4342, 4343, 4344, 4345, 4346, 4347, 4348, 4349,
4350, 4351, 4352, 4353, 4354, 4355, 4356, 4357, 4358, 4359, 4360,
4361, 4362, 4363, 4364, 4365, 4366, 4367, 4368, 4369, 4370, 4371,
4372, 4373, 4374, 4375, 4376, 4377, 4378, 4379, 4380, 4381, 4382,
4383, 4384, 4385, 4386, 4387, 4388, 4389, 4390, 4391, 4392, 4393,
4394, 4395, 4396, 4397, 4398, 4399, 4400, 4401, 4402, 4403, 4404,
4405, 4406, 4407, 4408, 4409, 4410, 4411, 4412, 4413, 4414, 4415,
4416, 4417, 4418, 4419, 4420, 4421, 4422, 4423, 4424, 4425, 4426,
4427, 4428, 4429, 4430, 4431, 4432, 4433, 4434, 4435, 4436, 4437,
4438, 4439, 4440, 4441, 4442, 4443, 4444, 4445, 4446, 4447, 4448,
4449, 4450, 4451, 4452, 4453, 4454, 4455, 4456, 4457, 4458, 4459,
4460, 4461, 4462, 4463, 4464, 4465, 4466, 4467, 4468, 4469, 4470,
4471, 4472, 4473, 4474, 4475, 4476, 4477, 4478, 4479, 4480, 4481,
4482, 4483, 4484, 4485, 4486, 4487, 4488, 4489, 4490, 4491, 4492,
4493, 4494, 4495, 4496, 4497, 4498, 4499, 4500, 4501, 4502, 4503,
4504, 4505, 4506, 4507, 4508, 4509, 4510, 4511, 4512, 4513, 4514,
4515, 4516, 4517, 4518, 4519, 4520, 4521, 4522, 4523, 4524, 4525,
4526, 4527, 4528, 4529, 4530, 4531, 4532, 4533, 4534, 4535, 4536,
4537, 4538, 4539, 4540, 4541, 4542, 4543, 4544, 4545, 4546, 4547,
4548, 4549, 4550, 4551, 4552, 4553, 4554, 4555, 4556, 4557, 4558,
4559, 4560, 4561, 4562, 4563, 4564, 4565, 4566, 4567, 4568, 4569,
4570, 4571, 4572, 4573, 4574, 4575, 4576, 4577, 4578, 4579, 4580,
4581, 4582, 4583, 4584, 4585, 4586, 4587, 4588, 4589, 4590, 4591,
4592, 4593, 4594, 4595, 4596, 4597, 4598, 4599, 4600, 4601, 4602,
4603, 4604, 4605, 4606, 4607, 4608, 4609, 4610, 4611, 4612, 4613,
4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621, 4622, 4623, 4624,
4625, 4626, 4627, 4628, 4629, 4630, 4631, 4632, 4633, 4634, 4635,
4636, 4637, 4638, 4639, 4640, 4641, 4642, 4643, 4644, 4645, 4646,
4647, 4648, 4649, 4650, 4651, 4652, 4653, 4654, 4655, 4656, 4657,
4658, 4659, 4660, 4661, 4662, 4663, 4664, 4665, 4666, 4667, 4668,
4669, 4670, 4671, 4672, 4673, 4674, 4675, 4676, 4677, 4678, 4679,
4680, 4681, 4682, 4683, 4684, 4685, 4686, 4687, 4688, 4689, 4690,
4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700, 4701,
4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712,
4713, 4714, 4715, 4716, 4717, 4718, 4719, 4720, 4721, 4722, 4723,
4724, 4725, 4726, 4727, 4728, 4729, 4730, 4731, 4732, 4733, 4734,
4735, 4736, 4737, 4738, 4739, 4740, 4741, 4742, 4743, 4744, 4745,
4746, 4747), class = "Date"), climate = c(39L, 4L, 7L, 8L, 87L,
8L, 12L, 9L, 26L, 69L, 18L, 89L, 53L, 47L, 70L, 94L, 26L, 51L,
93L, 91L, 71L, 32L, 21L, 35L, 2L, 12L, 97L, 50L, 58L, 55L, 93L,
8L, 99L, 97L, 25L, 44L, 60L, 81L, 3L, 66L, 64L, 26L, 61L, 53L,
97L, 7L, 54L, 78L, 62L, 30L, 28L, 82L, 23L, 81L, 39L, 66L, 62L,
13L, 77L, 8L, 22L, 17L, 89L, 6L, 16L, 6L, 3L, 99L, 57L, 23L,
15L, 14L, 7L, 78L, 24L, 50L, 83L, 86L, 44L, 25L, 92L, 63L, 87L,
68L, 42L, 5L, 24L, 61L, 22L, 79L, 44L, 92L, 74L, 48L, 90L, 1L,
96L, 28L, 97L, 88L, 56L, 83L, 92L, 66L, 76L, 23L, 95L, 26L, 34L,
93L, 8L, 24L, 40L, 90L, 85L, 18L, 55L, 71L, 81L, 12L, 97L, 35L,
69L, 55L, 53L, 94L, 26L, 85L, 22L, 87L, 25L, 31L, 8L, 1L, 7L,
72L, 65L, 62L, 5L, 49L, 75L, 40L, 38L, 22L, 57L, 94L, 13L, 55L,
99L, 64L, 90L, 56L, 65L, 83L, 73L, 69L, 9L, 14L, 28L, 49L, 1L,
38L, 55L, 78L, 58L, 48L, 55L, 65L, 37L, 85L, 75L, 84L, 84L, 59L,
19L, 62L, 37L, 23L, 74L, 94L, 51L, 31L, 64L, 24L, 42L, 24L, 76L,
71L, 95L, 21L, 18L, 67L, 53L, 38L, 60L, 97L, 8L, 80L, 67L, 1L,
28L, 91L, 73L, 99L, 60L, 68L, 12L, 52L, 28L, 32L, 82L, 10L, 58L,
4L, 56L, 49L, 72L, 90L, 16L, 92L, 38L, 32L, 77L, 69L, 18L, 68L,
55L, 78L, 56L, 36L, 21L, 21L, 91L, 63L, 10L, 2L, 97L, 16L, 56L,
89L, 9L, 53L, 23L, 28L, 28L, 41L, 72L, 42L, 10L, 97L, 100L, 79L,
62L, 28L, 64L, 15L, 25L, 42L, 29L, 26L, 70L, 12L, 98L, 93L, 92L,
12L, 38L, 57L, 37L, 51L, 8L, 38L, 71L, 42L, 37L, 76L, 21L, 2L,
3L, 30L, 76L, 78L, 48L, 46L, 22L, 84L, 36L, 9L, 65L, 7L, 62L,
51L, 38L, 31L, 18L, 53L, 1L, 89L, 57L, 19L, 37L, 57L, 66L, 65L,
39L, 46L, 70L, 86L, 50L, 92L, 99L, 10L, 74L, 33L, 52L, 37L, 45L,
85L, 6L, 77L, 54L, 97L, 98L, 98L, 65L, 39L, 3L, 6L, 58L, 13L,
29L, 69L, 32L, 59L, 8L, 12L, 18L, 14L, 39L, 73L, 42L, 62L, 70L,
75L, 57L, 65L, 77L, 88L, 40L, 91L, 6L, 20L, 27L, 67L, 45L, 14L,
57L, 1L, 73L, 40L, 14L, 40L, 95L, 46L, 78L, 11L, 69L, 22L, 41L,
49L, 47L, 99L, 2L, 20L, 8L, 91L, 11L, 85L, 18L, 73L, 22L, 2L,
64L, 12L, 43L, 34L, 8L, 38L, 30L, 55L, 31L, 49L, 1L, 66L, 76L,
49L, 89L, 74L, 62L, 70L, 23L, 26L, 42L, 81L, 49L, 26L, 70L, 75L,
78L, 18L, 51L, 84L, 47L, 39L, 24L, 31L, 51L, 11L, 96L, 46L, 46L,
78L, 53L, 82L, 5L, 77L, 18L, 6L, 45L, 92L, 22L, 72L, 100L, 6L,
41L, 1L, 9L, 84L, 19L, 31L, 8L, 96L, 24L, 98L, 60L, 43L, 50L,
27L, 4L, 75L, 83L, 48L, 10L, 8L, 85L, 39L, 50L, 27L, 43L, 8L,
83L, 92L, 70L, 84L, 78L, 51L, 67L, 26L, 63L, 43L, 90L, 60L, 6L,
27L, 9L, 58L, 63L, 29L, 5L, 77L, 99L, 72L, 17L, 22L, 40L, 92L,
22L, 11L, 70L, 94L, 31L, 93L, 6L, 93L, 62L, 84L, 62L, 21L, 62L,
13L, 35L, 47L, 29L, 12L, 57L, 100L, 6L, 30L, 14L, 23L, 78L, 72L,
39L, 55L, 71L, 56L, 76L, 37L, 54L, 19L, 58L, 94L, 78L, 64L, 100L,
44L, 46L, 40L, 47L, 23L, 88L, 70L, 24L, 38L, 27L, 35L, 53L, 64L,
31L, 50L, 16L, 76L, 94L, 73L, 10L, 73L, 78L, 96L, 42L, 5L, 87L,
64L, 14L, 86L, 9L, 59L, 27L, 48L, 24L, 79L, 77L, 25L, 19L, 43L,
46L, 14L, 39L, 48L, 28L, 29L, 73L, 4L, 79L, 58L, 92L, 30L, 30L,
51L, 35L, 11L, 36L, 33L, 35L, 43L, 66L, 19L, 66L, 48L, 44L, 90L,
75L, 32L, 2L, 64L, 46L, 86L, 55L, 16L, 35L, 59L, 51L, 56L, 9L,
84L, 90L, 23L, 17L, 45L, 20L, 3L, 7L, 43L, 46L, 26L, 52L, 75L,
33L, 22L, 96L, 14L, 45L, 1L, 4L, 92L, 69L, 85L, 21L, 20L, 100L,
28L, 8L, 77L, 29L, 77L, 55L, 72L, 42L, 52L, 79L, 51L, 46L, 21L,
61L, 19L, 21L, 49L, 96L, 33L, 61L, 33L, 69L, 62L, 91L, 58L, 3L,
11L, 66L, 89L, 59L, 22L, 50L, 75L, 3L, 82L, 1L, 26L, 53L, 47L,
94L, 57L, 10L, 81L, 98L, 19L, 38L, 92L, 52L, 91L, 6L, 42L, 76L,
39L, 21L, 89L, 19L, 3L, 37L, 93L, 61L, 35L, 18L, 90L, 25L, 48L,
73L, 24L, 51L, 58L, 1L, 58L, 8L, 8L, 92L, 51L, 85L, 17L, 68L,
98L, 64L, 60L, 64L, 65L, 3L, 23L, 76L, 84L, 24L, 59L, 40L, 58L,
43L, 21L, 3L, 17L, 36L, 90L, 86L, 41L, 65L, 99L, 12L, 11L, 64L,
63L, 24L, 46L, 79L, 97L, 70L, 12L, 1L, 77L, 46L, 17L, 15L, 86L,
4L, 32L, 10L, 67L, 95L, 68L, 67L, 21L, 69L, 2L, 66L, 29L, 80L,
42L, 76L, 85L, 62L, 56L, 4L, 36L, 42L, 73L, 100L, 90L, 99L, 15L,
46L, 37L, 78L, 29L, 35L, 14L, 27L, 23L, 16L, 56L, 57L, 87L, 57L,
80L, 89L, 63L, 63L, 37L, 80L, 71L, 43L, 12L, 39L, 51L, 4L, 66L,
91L, 84L, 8L, 88L, 33L, 24L, 87L, 54L, 65L, 5L, 31L, 61L, 86L,
1L, 2L, 85L, 66L, 82L, 24L, 80L, 78L, 22L, 54L, 72L, 64L, 61L,
91L, 64L, 16L, 38L, 50L, 27L, 47L, 43L, 54L, 79L, 64L, 37L, 6L,
32L, 94L, 32L, 43L, 99L, 13L, 86L, 32L, 85L, 94L, 99L, 26L, 19L,
11L, 15L, 20L, 38L, 58L, 73L, 6L, 7L, 93L, 56L, 48L, 28L, 70L,
8L, 53L, 90L, 36L, 95L, 96L, 98L, 98L, 57L, 40L, 47L, 26L, 50L,
82L, 98L, 69L, 22L, 52L, 86L, 15L, 99L, 76L, 35L, 46L, 87L, 14L,
40L, 68L, 29L, 58L, 32L, 26L, 68L, 68L, 62L, 95L, 6L, 65L, 16L,
64L, 88L, 52L, 89L, 55L, 86L, 86L, 41L, 39L, 62L, 65L, 36L, 40L,
87L, 46L, 66L, 88L, 25L, 49L, 58L, 44L, 1L, 36L, 48L, 56L, 64L,
92L, 94L, 8L, 61L, 84L, 90L, 38L, 42L, 1L, 69L, 98L, 51L, 64L,
80L, 79L, 7L, 33L, 33L, 20L, 35L, 63L, 56L, 96L, 1L, 93L, 30L,
39L, 86L, 78L, 18L, 20L, 59L, 80L, 100L, 100L, 11L, 39L, 72L,
55L, 4L, 42L, 50L, 77L, 64L, 67L, 40L, 100L, 6L, 9L, 67L, 67L,
41L, 57L, 68L, 90L, 38L, 63L, 53L, 85L, 52L, 86L, 49L, 43L, 17L,
35L, 45L, 66L, 5L, 100L, 51L, 30L, 95L, 43L, 94L, 87L, 88L, 23L,
49L, 67L, 5L, 78L, 4L, 8L, 30L, 42L, 18L, 38L, 13L, 1L, 68L,
23L, 27L, 86L, 72L, 39L, 80L, 5L, 87L, 12L, 40L, 43L, 65L, 33L,
42L, 23L, 87L, 19L, 44L, 84L, 58L, 18L, 20L, 49L, 50L, 70L, 99L,
92L, 91L, 25L, 62L, 73L, 43L, 83L, 47L, 38L, 86L, 58L, 7L, 15L,
85L, 38L, 87L, 97L, 69L, 59L, 9L, 9L, 29L, 99L, 35L, 87L, 19L,
2L, 52L, 44L, 81L, 73L, 10L, 83L, 89L, 17L, 58L, 57L, 14L, 42L,
77L, 73L, 47L, 71L, 25L, 96L, 11L, 31L, 6L, 47L, 15L, 71L, 75L
)), row.names = c(NA, -1096L), class = "data.frame")
对于我的示例,如果我正在计算年度摘要,那么我可以使用 group_by
中的 findInterval
(借用自
library(tidyverse)
df %>%
group_by(dr = findInterval(date, as.Date(c("1980-06-01", "1981-06-01", "1982-06-01")))) %>%
summarise(climate = sum(climate))
dr climate
<int> <int>
1 0 7665
2 1 17734
3 2 18172
4 3 11153
此外,我想获取每年每个季节的摘要。所以,我可以这样做:
df %>%
mutate(dr = findInterval(date, as.Date(
c("1980-06-01", "1981-06-01", "1982-06-01")
))) %>%
separate("date", c("year", "month", "day"), sep = "-") %>%
mutate(
month = str_replace(month, "^0+" , ""),
season = case_when(
month %in% c(3:5) ~ "spring",
month %in% c(6:8) ~ "summer",
month %in% c(9:11) ~ "fall",
month %in% c(12, 1:2) ~ "winter"
)
) %>%
group_by(dr, season) %>%
summarise(year = first(year), climate = sum(climate)) %>%
ungroup() %>%
select(dr, year, season, climate)
# A tibble: 13 × 4
# Groups: dr [4]
dr year season climate
<int> <chr> <chr> <int>
1 0 1980 spring 4744
2 0 1980 winter 2921
3 1 1980 fall 4392
4 1 1981 spring 4456
5 1 1980 summer 4687
6 1 1980 winter 4199
7 2 1981 fall 4330
8 2 1982 spring 4870
9 2 1981 summer 4420
10 2 1981 winter 4552
11 3 1982 fall 4622
12 3 1982 summer 4995
13 3 1982 winter 1536
不过,主要的问题还是在于年份的间隔。我还希望输出的顺序与原始数据框中的顺序相同。注:由于冬季是十二月到二月,因此年份应按十二月的年份分配。因此,在我的示例数据中,虽然数据从 1980 年 1 月开始,但我实际上需要将其设置为 1979 年,因为那年冬天开始了。所以,在这里,第一个冬天是唯一有这个问题的。
预期输出
dr year season climate
<int> <chr> <chr> <int>
1 0 1979 winter 2921
2 0 1980 spring 4744
3 1 1980 summer 4687
4 1 1980 fall 4392
5 1 1980 winter 4199
6 1 1981 spring 4456
7 2 1981 summer 4420
8 2 1981 fall 4330
9 2 1981 winter 4552
10 2 1982 spring 4870
11 3 1982 summer 4995
12 3 1982 fall 4622
13 3 1982 winter 1536
一种可能性是创建一个新的 corr.year
列,它确定季节开始的年份。我使用 corr.year
列进行分组而不是使用 findInterval
创建一个组列。但是,我觉得一定有更有效的方法。
library(tidyverse)
library(lubridate)
df %>%
mutate(corr.year=(date - days(59)) %>% floor_date("year") %>% year()) %>%
separate("date", c("year", "month", "day"), sep = "-") %>%
mutate(
month = str_replace(month, "^0+" , ""),
season = case_when(
month %in% c(3:5) ~ "spring",
month %in% c(6:8) ~ "summer",
month %in% c(9:11) ~ "fall",
month %in% c(12, 1:2) ~ "winter"
)
) %>%
group_by(corr.year, season) %>%
summarise(corr.year = first(corr.year), climate = sum(climate)) %>%
ungroup(season) %>%
arrange(corr.year, match(season, c("spring", "summer", "fall", "winter")))
输出
# A tibble: 13 × 3
# Groups: corr.year [4]
corr.year season climate
<dbl> <chr> <int>
1 1979 winter 2913
2 1980 spring 4744
3 1980 summer 4687
4 1980 fall 4392
5 1980 winter 4207
6 1981 spring 4456
7 1981 summer 4420
8 1981 fall 4330
9 1981 winter 4552
10 1982 spring 4870
11 1982 summer 4995
12 1982 fall 4622
13 1982 winter 1536
这首先标识了 2 个月前的那一天,它将所有内容都转移到季节开始的年份和年份的部分。然后它定义了一个有序的因子,其中包含季节名称,从 spring. count(x, wt = y, name = "name")
是 group_by(x) %>% summarize(name = sum(y))
的快捷方式,所以它在这里可以按年份和季节汇总气候数据。
library(lubridate)
df %>%
mutate(date_2mo_ago = date %m-% months(2),
season_num = quarter(date_2mo_ago),
season = c("spring", "summer", "fall", "winter")[season_num] %>%
forcats::fct_reorder(season_num),
year = year(date_2mo_ago)) %>%
count(year, season, wt = climate, name = "climate")
结果
year season climate
1 1979 winter 2921
2 1980 spring 4744
3 1980 summer 4687
4 1980 fall 4392
5 1980 winter 4199
6 1981 spring 4456
7 1981 summer 4420
8 1981 fall 4330
9 1981 winter 4552
10 1982 spring 4870
11 1982 summer 4995
12 1982 fall 4622
13 1982 winter 1536