Chapter 6 R objects solutions

6.1 df solution

6.1.1 Step 1

Create vectors for columns and row names:

One <- c(2,4,6)
Three <- c(6,12,18)
Five <- c(10,20,30)
row_names <- c("Two", "Four", "Six")

6.1.2 Step 2a

Create the data frame from vectors:

df <- data.frame(One,Three,Five)

Add row names:

row.names(df) <- row_names

6.1.3 Step 2b

Alternatively you can define the row names in the data.frame() function as an option:

df <- data.frame(One,Three,Five, row.names = row_names)

6.2 beach_df_2 solution

6.2.1 Step 1

Create vectors for columns and row names:

Crab <- c(10,1,1,4)
Oystercatcher <- c(5,6,4,4)
Sandpiper <- c(1,1,2,3)
Starfish <- c(3,3,7,4)
row_names_2 <- c("Formby","West Kirby","Crosby","New Brighton")

6.2.2 Step 2a

Create the data frame from vectors:

beach_df_2 <- data.frame(Crab,Oystercatcher,Sandpiper,Starfish)

Add row names:

row.names(beach_df_2) <- row_names_2

6.2.3 Step 2b

Alternatively you can define the row names in the data.frame() function as an option:

beach_df_2 <- data.frame(Crab,Oystercatcher,Sandpiper,Starfish, row.names = row_names_2)