Search code examples
rrangeaggregategrouping

How to group by using aggregate()


The question is below.

Researchers have set five proficiency levels in CIL: Below Level 1, Level 1, Level 2, Level 3, and Level 4. For simplicity, we recode them as two levels combining Level 3 and 4 as "Proficient" and all other lower levels (Level 2 and below) as "Developing". Use PV1CIL and the cut score of 577 (577 included for "Proficient") to create a new dependent variable of CIL_LEVEL (1 for "Proficient" and 0 for "Developing"). Please make sure if you create this new variable correctly by using aggregate with embedded range function.

The data name is CT_KOR.

 > CT_KOR$PV1CIL
   [1] 610.1726 536.0297 486.4944 510.7811 698.7426 489.2825 591.0807 566.0292 527.6944
  [10] 478.1921 582.3216 547.7692 571.0691 532.9718 374.6199 662.7491 514.7650 629.0674
  [19] 627.0129 625.7726 523.9097 485.5613 555.2949 625.6446 625.4544 579.6935 522.2717
  [28] 607.4198 590.7582 567.7674 613.3404 576.6527 628.6818 524.9084 616.9450 608.3879
  [37] 569.3843 589.6912 478.8122 545.1489 668.8271 682.7099 530.7808 608.3936 618.5848
  [46] 683.1247 671.6604 635.3076 719.2977 663.8203 614.3029 585.2294 634.8935 632.1665
  [55] 627.2535 614.2073 546.5247 588.7362 455.0541 630.4287 548.7369 569.1471 591.5804
  [64] 379.3007 655.7059 442.3343 703.0969 573.9475 517.4179 581.0366 608.9786 524.1069
  [73] 533.3098 507.8198 627.8082 631.7757 703.0099 556.6147 533.7735 435.9234 503.0598
  [82] 656.7491 610.1234 483.7502 385.3211 616.0607 539.4333 605.7844 348.6588 567.6139
  [91] 526.7432 452.4807 584.0824 504.4407 685.7394 645.7211 519.3448 577.5231 631.5154
 [100] 460.3575 571.1072 372.8615 418.4094 470.6738 326.9848 376.1307 476.4236 460.7276
 [109] 464.8018 265.9091 384.3027 421.4781 511.8900 392.1852 352.5679 527.5292 573.8367
 [118] 629.0386 520.6578 507.1929 415.9500 658.9268 424.9788 615.9770 603.2422 524.7137
 [127] 580.5351 568.3849 581.4110 432.8819 358.3111 592.2467 557.0001 596.5620 624.7964
 [136] 562.7968 551.2892 596.4786 586.1951 650.4873 521.6422 612.0697 582.7080 553.5536
 [145] 554.6864 712.5223 504.3991 574.3275 540.7888 513.2865 610.6803 683.5272 594.3095
 [154] 607.7757 697.5944 614.9488 537.2764 441.3007 612.6767 648.4753 573.1206 669.6481
 [163] 560.9017 383.6916 607.5759 614.0994 535.2829 498.7650 626.6462 459.6493 599.4698
 [172] 590.0448 499.5877 546.9769 631.6024 586.9019 603.5676 505.8720 539.7610 565.7867
 [181] 541.7483 698.8631 214.0698 407.3421 522.5834 341.9323 464.1518 536.8033 591.1985
 [190] 530.5338 602.5050 559.8515 576.4940 541.8526 486.5006 531.0415 501.7917 687.3182
 [199] 465.2342 365.8037 644.3875 587.3477 531.4102 623.3216 572.6078 592.6715 524.3041
 [208] 636.1258 595.7772 468.2215 309.9742 291.0744 345.1453 432.4905 431.6137 525.7699
 [217] 464.5111 667.1298 523.2919 632.3235 440.8505 586.1794 477.5876 505.8712 600.7812
 [226] 683.9547 462.3136 373.8445 338.3312 497.7788 637.2531 607.5671 512.1143 482.9765
 [235] 653.6729 496.9228 545.8161 599.5307 633.3563 534.6943 511.0465 590.8692 594.5772
 [244] 628.0557 601.7627 554.4089 634.2108 613.2450 594.6948 617.2501 653.5105 626.9157
 [253] 483.7834 543.3944 581.4319 637.2175 483.3355 441.6107 499.1648 337.3638 338.5623
 [262] 647.4014 317.2114 477.7369 547.9270 525.1443 652.5563 467.5695 356.2014 417.2018
 [271] 552.8672 554.2909 486.3138 493.6688 505.0836 597.2277 545.6250 483.8236 606.3112
 [280] 560.8840 620.1240 537.8582 403.7966 558.2255 492.4844 531.1171 344.9439 444.5986
 [289] 503.3509 509.1659 568.5809 642.0548 668.5075 471.4229 670.8706 599.9632 596.7061
 [298] 612.2471 518.5263 370.9307 660.7752 362.3088 646.4000 524.0676 550.4638 581.2960
 [307] 645.8874 558.8220 509.8675 533.9368 571.1732 706.4368 556.0088 622.0528 526.6369
 [316] 612.1288 610.0221 561.8771 655.1419 543.4548 493.0626 472.5344 707.9879 581.7706
 [325] 561.3700 647.6049 575.8710 571.6644 632.3675 556.0260 526.9691 378.8443 590.3157
 [334] 487.4770 739.5540 594.6709 495.2122 425.8457 546.6221 637.6905 504.3273 329.0046
 [343] 529.8330 454.5609 433.6484 517.4695 469.2410 373.3896 654.2946 633.8330 670.7710
 [352] 528.3070 592.0238 714.3752 608.4712 693.9635 517.0967 687.4010 557.9567 629.6972
 [361] 604.3159 629.3352 564.8151 594.7340 485.3810 593.0491 506.9151 510.3312 617.9392
 [370] 464.0122 617.8502 550.3765 491.5968 412.2156 436.4696 519.1878 524.3204 569.1781
 [379] 509.2561 553.4433 495.8182 712.3875 442.0752 551.9771 534.6656 583.4307 517.4090
 [388] 494.6815 488.4654 605.9946 510.6578 494.7884 375.2569 496.8701 508.8740 657.7985
 [397] 540.2819 651.6996 422.3496 536.5095 552.1974 407.3205 530.2077 576.8018 676.3231
 [406] 541.7036 554.8735 540.4270 588.5204 727.2967 628.7897 592.2194 671.3220 707.1966
 [415] 485.3446 673.7472 680.4779 594.3382 568.9618 631.6308 590.1269 455.3148 689.4375
 [424] 625.9905 499.3818 637.8866 527.0268 439.0485 514.6249 745.1192 505.1546 537.4362
 [433] 642.1925 609.7796 602.1587 550.3744 536.7035 527.0910 513.1427 567.9706 704.9052
 [442] 569.8309 531.5959 412.6795 596.6898 595.8808 518.5013 412.6355 497.5280 548.5859
 [451] 505.4673 574.7350 570.8707 603.2317 568.5441 392.7409 546.5554 567.5322 534.8740
 [460] 670.3600 440.2233 623.5253 543.5284 603.7673 523.2946 654.3232 514.2416 542.4837
 [469] 560.2479 530.7274 368.5643 574.2205 489.2742 392.4033 590.2572 585.3987 446.5354
 [478] 735.3303 526.0293 591.1479 437.9814 481.6034 593.6491 561.8535 605.4782 493.5615
 [487] 587.6825 579.8039 442.5309 470.7028 631.6225 326.0924 491.3062 724.5762 658.4295
 [496] 545.1836 583.3657 302.8177 593.7716 629.7934 575.7840 426.5096 570.5089 465.4861
 [505] 530.4368 408.6344 541.5439 529.3011 329.3407 556.3836 408.1099 538.8492 381.8463
 [514] 300.1247 566.9195 628.4352 513.5455 664.2645 545.8093 449.0567 684.6941 624.1112
 [523] 593.3947 684.6439 524.4125 538.4597 532.8545 634.0243 332.5007 466.6511 528.8399
 [532] 535.2424 498.6578 475.0555 400.6366 512.8722 669.4896 424.4301 498.6894 601.7947
 [541] 510.6482 440.2131 354.1841 493.8075 590.6427 641.9373 403.2258 524.5113 385.7828
 [550] 485.8268 498.3487 300.3041 622.8328 621.9232 529.5082 524.2689 498.1017 465.0673
 [559] 434.5204 593.3032 309.8972 524.8695 618.3695 705.6705 547.7182 343.4290 586.4730
 [568] 440.1994 580.7321 483.0616 418.9258 633.6992 681.3593 436.6994 410.5217 558.8730
 [577] 564.4813 352.2621 554.3032 443.4160 300.4667 549.3880 670.7755 670.8112 675.9812
 [586] 565.2126 452.6408 656.3288 460.6566 497.9571 482.4025 288.5071 562.6328 548.2367
 [595] 401.6052 548.0297 550.2048 634.1547 451.4506 205.3479 525.0772 597.5172 433.4256
 [604] 461.8033 373.1299 510.3819 345.9601 474.6677 390.9013 384.2334 409.8042 592.5206
 [613] 527.6902 606.2306 551.3911 574.3444 546.0834 559.1868 666.2265 618.8788 538.4193
 [622] 663.7334 632.0148 583.2458 608.7828 357.5803 683.4967 510.2015 597.3069 460.2933
 [631] 670.4713 656.1282 561.8084 702.9969 681.8683 551.4931 658.2867 651.5589 580.2509
 [640] 512.2999 348.2836 598.9734 533.6019 449.0267 348.7497 591.8666 571.4541 570.0749
 [649] 554.8738 715.5423 616.5714 624.9041 611.8861 471.2481 532.5227 500.9122 599.6075
 [658] 554.3853 447.1114 402.7847 497.3079 430.1130 523.2514 511.9647 230.3549 459.8427
 [667] 543.2000 394.8855 497.5001 350.5940 362.4497 414.5391 623.5342 649.0672 630.8329
 [676] 496.3026 549.7647 429.3879 549.6162 503.7263 403.8911 319.4957 305.3814 536.4746
 [685] 516.2697 691.8133 444.1847 521.6459 303.5204 623.1182 637.4966 481.4494 397.3149
 [694] 568.6503 542.8249 522.5746 573.3647 445.1616 472.2114 542.5459 369.8518 547.2766
 [703] 470.5085 488.2595 395.3229 773.6336 528.0459 422.1607 492.1754 560.4094 518.4545
 [712] 656.3358 512.5697 483.0450 636.7884 407.0311 470.2990 545.9634 685.3336 422.5806
 [721] 394.6296 571.8704 364.2055 586.7419 668.2336 420.7777 460.2786 549.1690 637.8717
 [730] 485.1395 639.4683 499.4613 643.1194 550.4720 479.4685 656.1778 413.9765 605.0390
 [739] 657.0538 607.3078 598.7892 468.7045 609.5553 532.5019 505.6835 567.4638 666.9330
 [748] 636.3258 555.4771 612.5618 536.1278 606.3381 472.0651 608.1485 661.9908 556.9476
 [757] 637.9076 549.9497 505.4512 502.9254 561.2122 726.6784 549.8313 590.5936 552.7611
 [766] 513.1281 581.2105 511.2493 564.8792 602.4473 614.2917 635.8458 573.4984 609.9184
 [775] 512.1127 395.1762 574.3437 493.7187 682.3807 377.1587 509.4152 642.5459 506.7608
 [784] 580.0008 557.5743 625.6982 565.0527 618.1601 643.4908 693.2171 607.3587 491.2058
 [793] 665.6092 591.2632 504.3844 445.2821 547.3844 594.8567 509.3527 549.7404 414.6339
 [802] 544.5781 604.6648 663.4548 639.2557 538.8879 514.4627 495.4890 557.5786 611.0298
 [811] 560.8141 591.2562 422.1747 560.8426 380.6070 499.6450 486.9992 483.9319 509.4972
 [820] 481.6197 560.0077 611.0568 565.0553 585.1861 670.6114 447.3882 660.6743 547.4261
 [829] 486.7865 384.0288 490.9424 483.6112 450.3489 379.9212 616.7521 442.8945 431.3132
 [838] 312.0358 519.4153 380.4219 545.9880 418.1550 590.4711 540.4183 523.6280 280.3552
 [847] 309.2470 432.6751 546.9604 623.0937 595.0230 512.0432 654.8731 614.8597 597.9931
 [856] 575.2531 601.6007 515.7683 598.0579 587.6038 446.3716 513.5403 530.0291 509.6453
 [865] 438.5816 341.2950 369.1405 559.6349 543.0690 547.5638 357.2809 509.6864 564.1927
 [874] 579.7998 608.0862 553.5102 571.6629 648.4707 407.7361 435.5789 515.4500 646.2092
 [883] 534.2466 654.3100 581.9988 576.1276 532.5529 524.6098 479.3209 547.9170 378.8833
 [892] 584.0840 560.9296 552.0179 412.4100 500.4508 604.9939 486.1859 365.4861 545.5607
 [901] 611.9878 553.6224 464.0422 533.8710 679.9812 355.0584 588.0829 554.0489 398.9246
 [910] 427.8454 440.1258 628.0580 422.3995 656.3239 612.0874 532.6908 522.8831 630.2063
 [919] 648.4412 542.8832 472.2348 503.3613 574.1035 647.7719 530.8216 523.8905 362.8608
 [928] 627.5681 497.5527 600.3700 472.2625 382.7398 345.7937 462.1713 596.0888 468.8220
 [937] 525.0912 528.8709 517.9851 583.4719 584.8182 328.7235 520.0649 529.0770 548.2714
 [946] 452.6109 611.7075 490.6138 618.3343 593.8566 521.9175 570.6815 446.6599 724.3289
 [955] 530.6449 553.2758 660.0523 812.6609 536.1668 574.2914 694.0791 583.4676 616.0094
 [964] 466.3206 596.8269 602.9528 618.4076 514.7611 543.0806 487.6603 475.3188 380.5811
 [973] 569.1942 504.6830 499.7571 367.4309 482.0945 515.2384 376.5055 467.3696 662.7557
 [982] 596.5207 427.3464 505.9552 410.7053 642.2937 586.4565 446.4301 363.0911 565.3702
 [991] 308.4431 603.0209 609.4712 607.9397 690.8757 558.4087 428.3958 458.4033 457.1218
[1000] 558.6880
 [ reached getOption("max.print") -- omitted 1875 entries ]
attr(,"value.labels")
named numeric(0)

I found there is no five CIL level variables in CT_KOR. So, I assign "Developing" and " Proficient" to CIL_LEVEL. I don't know what to do after that.

CIL_LEVEL <- c("Developing", "Proficient")
Proficient <- CT_KOR$PV1CIL >= 577
Developing <- CT_KOR$PV1CIL < 577
aggregate(PV1CIL ~ CIL_LEVEL, data = CT_KOR, mean)

As expected, it doesn't work, appearing an error message. Please help me.


Solution

  • As far as I understood, the question is asking to create a single variable called CIL_LEVEL and assign the values based on the condition. I would create the column CIL_LEVEL using the following code:

    CT_KOR$CIL_LEVEL <- ifelse(CT_KOR$PV1CIL >= 577, 1, 0)
    

    Then, if you want to verify that the variable is created correctly you should use:

    aggregate(PV1CIL ~ CIL_LEVEL, data = CT_KOR, FUN = range)