Clarification regarding Scatter Plots (R code)

Screen Link:
https://app.dataquest.io/m/276/scatter-plots-for-exploratory-analysis/5/write-a-function-to-create-multiple-scatter-plots

My Code:

x_var<- names(reviews_2)[5]
y_var <- names(reviews_2)[2:4]

What I expected to happen:
Actually, I am just curious if there’s a difference between:
x_var<- names(reviews_2)[5]
y_var <- names(reviews_2)[2:4]

Vs.

x_var<- names(reviews_2[5])
y_var <- names(reviews_2[2:4])

The latter comes to me more naturally.

Thank you!

What actually happened:

Replace this line with the output/error
2 Likes

Hi @lauratangwt,

There is no difference between the results. The only difference is in the way it retrieves that result.

In the case of names(reviews_2)[5], the column names names(reviews_2) are returned first and then we select a specific column name using [5]

On the other hand, in names(reviews_2[5]), we are returning the 5th column using reviews_2[5] and finding it’s column name using names(),

Best,
Sahil

3 Likes