subset columns in r code example
Example 1: subset row r
# df[row,column]
df[1, ] # select first row and all columns from df
df[ ,8] # select all rows for column 8 from df
df[2,5] # select row 2, column 5 from df
df[1:5, ] # select rows 1 to 5 and all columns from df
Example 2: reorder columns in r
col_order <- c("Species", "Petal.Width", "Sepal.Length",
"Sepal.Width", "Petal.Length")
my_data2 <- my_data[, col_order]
my_data2
# or
my_data2 <- my_data[, c(5, 4, 1, 2, 3)]