As usual, create sample data first
1 2 3 4 5 6 7 8 9 |
##### Creating Sample Data ##### Name <- as.character(c("Abbey", "Brian", "Connie", "Dan", "Ethan", "Frida")) GPA <- c(3.0, 2.8, 2.1, 4.0, NA, 3.0) Grade <- c(3, 4, NA, NA, 6, 9) State <- c("AL", NA, NA, NA, "CA", "VA") Credit_Score <- sample(550:750,6) ID <- sample(100:200,6) data <- data.frame(Name, GPA, Grade, State, Credit_Score, ID, stringsAsFactors = FALSE) |
We can extract by creating odd/even sequences. The data frame is 6 x 6. Therefore, we will create up to 6 only.
1 2 3 |
##### Create Sequence ##### odd <- seq(1,6,2) even <- seq(2,6,2) |
We then apply the sequence to the data frame.
1 2 3 4 5 6 7 8 9 10 |
#### Row Extraction ##### data_odd_row <- data[odd,] data_even_row <- data[even,] ##### Column Extraction ##### data_odd_col <- data[,odd] data_even_col <- data[,even] data_odd_row <- data[odd,] data_even_col <- data[,even] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
> data_odd_row Name GPA Grade State Credit_Score ID 1 Abbey 3.0 3 AL 555 153 3 Connie 2.1 NA <NA> 607 181 5 Ethan NA 6 CA 675 117 > data_even_col GPA State ID 1 3.0 AL 153 2 2.8 <NA> 145 3 2.1 <NA> 181 4 4.0 <NA> 170 5 NA CA 117 6 3.0 VA 158 |