Table of Contents
In R, columns can be selected by their index number using the bracket notation. The index number represents the position of the column in the data frame. To select columns by index, the data frame name is followed by a comma, then the index numbers of the desired columns within brackets. Multiple columns can be selected by separating the index numbers with a comma. For example, df[, c(1,3)] would select the first and third columns of the data frame named “df”. This method can also be used to select columns by their names or logical expressions. Some examples of selecting columns by index in R are shown below.
Select Columns by Index in R (With Examples)
You can use the following basic syntax to select columns by index in R:
#select specific columns by index df[ , c(1, 4)] #select specific columns in index range df[ , 1:3] #exclude specific columns by index df[ , -c(2, 5)]
The following examples show how to use this syntax in practice with the following data frame:
#create data frame df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'), points=c(99, 90, 86, 88, 95), assists=c(33, 28, 31, 39, 34), rebounds=c(30, 28, 24, 24, 28), blocks=c(7, 7, 5, 9, 13)) #view data frame df team points assists rebounds blocks 1 A 99 33 30 7 2 B 90 28 28 7 3 C 86 31 24 5 4 D 88 39 24 9 5 E 95 34 28 13
Example 1: Select Columns by Index
The following code shows how to select specific columns by index:
#select columns in 1st and 4th position
df[ , c(1, 4)]
team rebounds
1 A 30
2 B 28
3 C 24
4 D 24
5 E 28
Example 2: Select Columns in Index Range
The following code shows how to select specific columns in an index range:
#select columns in positions 1 through 3df[ , 1:3]
team points assists
1 A 99 33
2 B 90 28
3 C 86 31
4 D 88 39
5 E 95 34Example 3: Exclude Columns by Index
The following code shows how to exclude specific columns by index:
#select all columns except columns in positions 2 and 5
df[ , -c(2, 5)]
team assists rebounds
1 A 33 30
2 B 28 28
3 C 31 24
4 D 39 24
5 E 34 28
Notice that this returns all of the columns in the data frame except for the columns in index positions 2 and 5.
The following tutorials explain how to perform other common operations on data frame columns in R:
Cite this article
stats writer (2024). How can I select columns by index in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-select-columns-by-index-in-r-can-you-provide-some-examples/
stats writer. "How can I select columns by index in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 4 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-select-columns-by-index-in-r-can-you-provide-some-examples/.
stats writer. "How can I select columns by index in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-select-columns-by-index-in-r-can-you-provide-some-examples/.
stats writer (2024) 'How can I select columns by index in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-select-columns-by-index-in-r-can-you-provide-some-examples/.
[1] stats writer, "How can I select columns by index in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I select columns by index in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
