Adding Features to a Data Frame
We will look at the code to add new columns to an R data frame. A new column may be a new feature or a copy of an existing column. We'll look at an example in the following exercise. Adding new features can help in improving the efficiency of a model.
Exercise 29: Adding a New Column to an R Data Frame
In this exercise, we will add columns to an existing R data frame. These new columns can be dummy values or copies of other columns.
- Add a new feature to the R data frame, as follows:
#Adding new features to a R datadrame
library(caret)
- Load the GermanCredit dataset:
data(GermanCredit)
- Assign the GermanCredit value to a new field:
#Assign the value to the new field
GermanCredit$NewField1 <- 1
- Print the GermanCredit string:
str(GermanCredit)
The output is as follows:
Figure 3.10: Section showing the added NewField
- Copy an existing column into a new column, as follows:
#Copy an existing column into a new column
GermanCredit$NewField2 <- GermanCredit...