What does this code([,c(8,10)]) mean? Does it mean extracting the eighth and tenth columns from the table?

EC_1990to2021_ASPRchange <- read.csv('EAPC_outcome(Prevalence).csv',header = T)

EC_2021_ASPR_Table1 <- EC_2021_ASPR[,c(8,10)]
EC_1990to2021_ASPRchange_Table1 <- EC_1990to2021_ASPRchange[,c(8,27)]
EC_Prelavence_Table1 <- left_join(EC_2021_Prevalence_number_Table1,EC_2021_ASPR_Table1,by="location")

Yes! As in
mtcars[, c(8,10)]

Just to add detail here; the general syntax is data[rows,columns] -- so, for example,
mtcars[3,4]
is the data point in the 3rd row, 4th column.

You can also put ranges in either the row or column entry. For example,
mtcars[1:3,4]
will give you data from the 4th column, rows 1, 2, and 3.

Since c() is used to make vectors, you can use it to list multiple (nonconsecutive) rows or columns. This code will give you the entry from the 4th column, and only rows 1 and 3.
mtcars[c(1,3),4]

And finally, entries can be excluded from the row section within brackets to print the entire column, or excluded from the column section to print the entire row. Please note that you must keep the comma to make this syntax work. E.g.
mtcars[c(1,3),]
outputs the entirety of rows 1 and 3, and
mtcars[,c(1,3)]
outputs the entirety of columns 1 and 3.

You may find this article helpful: RPubs - Bracket Notation, Subsetting and Extracting

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.