It is not surprising that the R language is powerful and the best fit for statistical analysis. If you think of learning R, it is very easy to learn R programming. To make you comfortable with R programming, in this article we are briefing about a couple of R programming examples. Let’s Roll!!!

This article will include some R programming examples which cover the basics aspects of programming. These aspects include loops, input and output statements, finding the sum, mean, and median of a number, and more.

## 1. Print statements in R

How can one start programming without ‘Hello World’ program. Personally I love this culture and will start with this program.

Hello world program is the best program to describe print statements in simple terms. Let’s print a hello world statement in R and it takes only one line…

```#Prints the 'Hello World!"
print("Hello World!")```
``"Hello World!"``

WOW!!!, you just printed the hello world statement in R. Kudos!!!

```#Prints the hello world without quotes
print("Hello World!",quote = F)```
``Hello World!``

The above code will remove the quotes in the statement and returns the text in this case as shown above.

```#Concateates the strings and returns the whole sequence as output
print(paste("Howdy","Buddy,","Welcome","To","R","Programming"))```
``"Howdy Buddy, Welcome To R Programming"``

Well, you have printed the hello world statements. But what if you have multiple strings in your input?

Then you should make use of the paste() function in R programming. The paste() function allows you to concatenate multiple strings and returns a sequenced output as shown in the above R programming example.

## 2. Loops in R programming

Our next R programming aspect is looping. There are 3 loops in R. For loop, while loop, and Repeat loop. I will illustrate all of these with an example to make sure things are clear at your end.

For Loop:

We are going to compute a simple program to know how for loop works in R programming.

```#iterates though the range and returns the values
for (val in 1:5)
print(val)```
``````1
2
3
4
5``````

While Loop:

The while loops run the condition until the statement becomes false. In the while loop, the test condition is executed first and then the body will follow that. If your test statement is false, then the body loop is not executed!.

```#creating a  value
value = 1
#while loop
while (value <= 5 )
{
#input statement
print(valie)
value = value + 1```
``````1
2
3
4
5``````

In the above section, we are initialising the value, then the while checks the condition and adds 1 to the value until it satisfies the condition.

Repeat Loop:

The repeat loop in the R programming will take the input statement or the group of statements and repeats it until it satisfies the end condition. Let’s see how it works.

``` val = 1
# using repeat loop
repeat
{
# input statements
print('I love Hackanons')
val = val + 1
#stop condition
if(val > 5)
{
# using break statement
break
}
} ```
``````"I love Hackanons"
"I love Hackanons"
"I love Hackanons"
"I love Hackanons"
"I love Hackanons"``````

## 3. Input prompts from the user

R offers the input statements which you can use to take the user input on many occasions. Suppose, if you want the user to enter his salary, age, or anything else, you can use the input prompt statements in R.

```#prompts the user for the input
``Enter your name:  Mark ``
```#prints the welcome statement
print(paste('Howdy',name,'!','Welcome to Hackaons'))```
``"Howdy Mark ! Welcome to Hackaons"``

## 4. Finding the Sum, Mean and Median

R is popular for its computational speed and tools. So, I will end up the R programming examples with some statistical computations such as sum, mean and median of the values.

Let’s see how it works!

```#input vector having values
df<-c(23,54,67,8.908,NA,-45,3.0098,NA)```
```#Computes the sum of values
sum(df,na.rm = T)```
``110.9178``
```#Computes the mean of values
mean(df,na.rm = T)```
``18.4863``
```#Computes the median of values
median(df,na.rm = T)```
``15.954``

In the above computations, we are using the na.rm = T parameter. Because our input data contains the NaN values. Hence it is important to remove those values to get the output.

## Wrapping Up – R programming Examples

R is too good for the statistical computation, data analysis and model building. You can use R with ease and it has tons of functions which assists you in your work.