Under certain conditions, users of the assisted sense of data analysis generally require a standard method. The non-standard method requires users to be able to write their own events and functions that they need. R makes it easy for users to be able to write their own programs they need. The easy-to-learn R event syntax makes R one of the most powerful programming languages for performing analysis based on simple to complex.
In this article will be explained the basic arguments on R programming to be the foundation on creating custom programs that might be expected. This article uses RStudio as an environment to facilitate the process of creating events. Understanding and installation stage of RStudio can be reviewed in the following article: Tutorial R : RStudio becomes an IDE that Facilitates A. If … Else
The use of If … Else is done when we want a function / program to be run if an exclusive condition occurs. Here is the model of using If… Else. Copy-Paste the following code into RStudio-Console or RStudio-Script and run.year = 2012if (year %% 4 == 0) paint(“year”,year,”is leap year”)else print(“year”,year,”is not a leap year”)
Results:2012 is a leap year
In the example above, suppose you want to find out if the year inputted is a leap year (the number of years that are divisible by 4).The logic in the argument above is:If the year variable is divided (modulo) 4, then it is written that year is a leap year.If anything else, then written that year is not a leap year.
Here’s an explanation of each line:year = 2012 (input year to know)if (year %% 4 == 0) paint(“year”,year,”is leap year”) (condition when fulfilled)else print(“year”,year,”is not a leap year”) (the condition when the non-life is fulfilled)B. Looping For
The use of For looping is carried out when the process of looping a condition is carried out until in the replay that has been affected. Here’s the model of using Loop For. Copy-Paste the following code into the RStudio-Console or RStudio-Script then Run.s=c()for(i in 1:100)s[i]=i*10 s
Result[1] 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150[16] 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300[31] 310 320 330 340 350 360 370 380 390 400 410 420 430 440 450[46] 460 470 480 490 500 510 520 530 540 550 560 570 580 590 600[61] 610 620 630 640 650 660 670 680 690 700 710 720 730 740 750[76] 760 770 780 790 800 810 820 830 840 850 860 870 880 890 900[91] 910 920 930 940 950 960 970 980 990 1000
In the example above, suppose you want to loop 100 times (make numbers 1 to 100). With each iteration number multiplied by 10. Then this process will make a number of tens of 100 starting based on 10 to 1000.
Here’s an explanation of each line:s=c() (provides an empty variable s to be filled with iteration output)for(i in 1:100) (entering looping commands 100 times)s[i]=i*10 (states variable s is multiplication of number 10 by 1-100)s (calling the variable s that already contains iteration output & multiplication)C. Temporary Loop
The use of While iterations is carried out when the looping process is carried out until an exclusive condition is met. Here’s the model of using Temporary Loops. Copy-Paste the following code into the RStudio-Console or RStudio-Script then Run.z=0while(z < 100)z=z+10print(z)
Result:[1] 10[1] 20[1] 30[1] 40[1] 50[1] 60[1] 70[1] 80[1] 90[1] 100
In the example above, suppose you want to repeat on the condition that the replay will permanently last until the value of z does not exceed 100. With eachz number on each loop plus using 10, where the first z value is defined as programming r sema with 0 (zero).Here’s an explanation of each line:z=0 (defining the initial z value of 0 (zero))while(z < 100) (selecting the terms of the loop)z=z+10 (states that each new z variable that appears plus 10)print(z)(displays the number z each repeat)
Thus, the three basic arguments in Tutorial R: Basic Arguments in R Programming. Still poly other basic arguments that can facilitate the process of preparing functions / events for data analysis in R. On other occasions will be discussed about other arguments in R Programming. May this article be useful and do not hesitate to leave comments in the comments column.