Updating Dataframes

To create a new variable in our dataframe, we can use the $ operator.

In the example below, we will add a column called sepal_less_petal_len to the original dataframe.

This column will contain Sepal.Length - Petal.Length

iris$sepal_less_petal_len <- iris$Sepal.Length - iris$Petal.Length

Exercise!

Make your own Data frame

We can also make our very own data frames and lists, by combining vectors.

  • Open up R studio cloud, and 5 create vectors; Monday, Tuesday, Wednesday, Thursday and Friday. Make the content of each vector a shopping list containing 5 items for a restaurant on each day of the week.
  • Monday might look like this:
    • monday <- c("pasta", "bacon", "mushrooms", "milk", "cheese")
  • Do this for all 5 days.
  • Combine all 5 vectors into a data frame using the data.frame() function:
    • dataframe <- data.frame(monday, tuesday, wednesday, thursday, friday)
  • Use some of the subsetting techniques described such as indexing and filtering to explore how data frames are in essence, combinations of vectors that are sub settable.

If Else

We can use ifelse() to create new variables based on conditional statements.

In the example below we will use a vector, however, this applies to dataframe columns too:

Note:

The ifelse() function is an example of a ternary operator which reads as follows: “ A ? B : C“ – If A is true choose B, else choose C.

vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

test <- ifelse(vector > 5, "greater than 5", "less than 5")
print(test)

Returns:

 

But what about the 5th element? 5 is not less than 5.

To add a second layer of conditionals we will re-use the ifelse() function:

test <- ifelse(vector == 5, "five", test)
print(test)

Returns: