0
0
R Programmingprogramming~10 mins

Row and column indexing in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Row and column indexing
Start with data frame
Choose row index
Choose column index
Extract value or subset
Use or display result
END
This flow shows how to pick rows and columns from a data frame by their positions or names to get specific values or smaller tables.
Execution Sample
R Programming
df <- data.frame(A=1:3, B=4:6)
value <- df[2, 1]
subset <- df[1:2, "B"]
print(value)
print(subset)
This code picks a single value and a subset of a data frame by row and column indexes and prints them.
Execution Table
StepCodeRow IndexColumn IndexExtracted ValueOutput/Action
1df <- data.frame(A=1:3, B=4:6)--Data frame createddf = A B 1 1 4 2 2 5 3 3 6
2value <- df[2, 1]212value = 2
3subset <- df[1:2, "B"]1 and 2"B"4, 5subset = c(4, 5)
4print(value)--2Prints 2
5print(subset)--4 5Prints 4 and 5
6----Execution ends
💡 All indexing done; program ends after printing values.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dfNULLdata frame with 3 rows and 2 columnssamesamesame
valueNULLNULL222
subsetNULLNULLNULLc(4,5)c(4,5)
Key Moments - 2 Insights
Why do we use df[2, 1] instead of df[1, 2] to get the value 2?
Because df[row, column] means row first, then column. Step 2 in the execution_table shows row=2 and column=1 gives value 2.
How does using "B" as a column index work in df[1:2, "B"]?
Using the column name "B" selects that column. Step 3 shows rows 1 and 2 of column "B" extracted as 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2. What is the value of 'value' after this step?
A2
B4
C5
D1
💡 Hint
Check the 'Extracted Value' column at Step 2 in the execution_table.
At which step is the subset c(4, 5) created?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'subset' variable changes in variable_tracker and the 'Extracted Value' in execution_table.
If we change df[2, 1] to df[3, 2], what value will 'value' hold?
A3
B5
C6
D4
💡 Hint
Look at the data frame 'df' in Step 1 and find row 3, column 2.
Concept Snapshot
Row and column indexing in R:
Use df[row, column] to pick data.
Rows and columns can be numbers or names.
Example: df[2,1] gets row 2, column 1.
Example: df[1:2, "B"] gets rows 1-2 of column B.
Result can be single value or subset.
Full Transcript
This lesson shows how to get data from a data frame in R by picking rows and columns. We start with a data frame named df with columns A and B. Using df[2, 1] picks the value in row 2, column 1, which is 2. Using df[1:2, "B"] picks rows 1 and 2 of column B, giving values 4 and 5. We print these values to see the results. The key is remembering the order: row first, then column. Columns can be selected by number or name. This helps get single values or smaller parts of the data frame.