Menu

Let’s say we want to add whitespace to name feature; we can use for loop()  to do just that.

We assign the length of observation to the length variable. Then we use str_pad()  to add whitespace. The first str_pad()  will add leading whitespace, while the second str_pad()  will add trailing whitespace.

For example,  data[[1]][[1]] is ‘Abbey’. Length’s value will be 5. Whatever in Width’s ” ” in str_pad()  controls how many more width will be.

Pad controls what to put in the string, in this case, it is whitespace. Width controls how many more character in Pad will be inserted. In this case, it will be only one as we want the new length to be 6. Then side refers to leading insertion.

For the second str_pad() , the function works the same way except that the side has to be ‘right’ as we now will add trailing whitespace. Width also has to be ‘length+2’ as data[[1]][[1]]  has length six due to leading whitespace.

The for loop()  can be used to add any character by just changing the pad parameter.