103.2.4 Calculated Fields in R

Making Data Analysis more easier.

Calculated or the derived field is another important concept in data analysis. Sometimes not only the fields or the rows and columns of the raw data are sufficient for the data analysis, we might also have to do some operations and create some new fields. A new field can be added to the existing dataset by using $

Creating Calculated Fields in R

Consider the following syntax, where a new field sum is added(attached) to the dataset, where the content of this variable is the sum of the other two variables x1 and x2 in the dataset.

>dataset$sum <- dataset$x1 + dataset$x2

Average of two existing variables can be stored in a new variable in a dataset, as shown below

>dataset$mean <- (dataset$x1 + dataset$x2)/2

However, assigning this way is very time to consume as the user has to type the name of the dataset again and again.This process can be bypassed by attaching the dataset. Using this, the user need not refer to the dataset again and again.

As we can see, before creating a new variable area in AutoDataset, the number of rows and columns were 205, 26, and after creating the new field, the number of rows and columns became 205, and 27. Hence a new variable ‘area’ has been created . Creating a new variable by reducing the balance by 20%.

[1] "condition fail"
Warning message:
In if (first_element[] > 3) { :
the condition has length > 1 and only the first element will be used

The above code would print “condition fail” in the output as none of the elements in the variable first_element is greater than 9. Else should be written immediately after ‘}’ and not in the next line as it has been written in the above example.

If-then-Else Statement

If then Else, functions the same way as If Else. However, the difference lies in the syntax. The syntax for If then Else is as shown below

Newvar<-ifelse( Condition, True Value, False Value)

In arguments we give the condition, then the value when the condition is true and the value when the condition is false.

Consider the following examples:

We see if there are any missing values in horsepower.

>Sum(is.na(AutoDataset$horsepower))

If there are any missing value then replace it with -1 using ‘If-then-Else’ condition