Referring to columns in a dataset uses different syntax in each software. Most data languages, such as SAS, SQL and R provide you ways of referring to columns (or variables or fields) in your dataset using variable names. If you do a lot of public health data analysis like I do, referring to columns by their names is easiest for communication among people working on the dataset.
Referring to Columns by Names in R (and in General)
In fact, I had not really thought of referring to columns using column numbers instead of names until I started using R a lot. I thought it was a very bad idea at first, and that is mainly because I often remake my datasets, so the column numbers change. This means that referring to a column number in coding might be risky, because if I remake the dataset, then suddenly, this will be pointing to a different variable. And if there is any variable in that position, the code will execute and not error out.
Also, I tend to use popular datasets a lot that other people use, and so when I talk about a variable by name with them, then this actually means something to my audience. No one talks about “variable 100” unless you are using a fixed width dataset, and if you are, I’m feeling very sorry for you.
I’ll give an example of how attached to the actual names of variables I am. If I’m using the BRFSS dataset in R, I am probably using the variable X_AGE_G in my programming. This is because BRFSS is a health survey, and X_AGE_G is an age group variable that has no missing values (because it is used in the sampling approach, so it has to be filled in completely). Of course, who doesn’t love a variable like that? Pretty much everyone who uses the BRFSS uses this variable.
And I know that no matter where I put that variable in the dataframe, if I run code against it by specifying X_AGE_G, R will refer to that variable only. If it does not find a variable with that exact name, it will error out. If I’m using a dataframe called BRFSS_a, and I tell R to do something to BRFSS_a$X_AGE_G, I know that no matter what number the variable BRFSS_a$X_AGE_G has in that iteration of the dataframe, the code will either execute on the correct variable, or error out. There is no risk of a mistake. So why do I need the column numbers?
Referring to Columns by Number in R Enables Easy Automation
So, I basically had to learn the hard way that it is very difficult to automate R code against dataframes if you are using the actual names of the fields in the code. If you are a SAS user, then you will likely be as surprised as I was to learn this. SAS likes automating against named variables and arrays. But in R, it’s just easier to refer to columns by numbers if you are automating them – and that is because of a fundamental difference in the way R runs compared to SAS.
Numerical Syntax in R
Those of you who are big Excel fans are comfortable of thinking of an Excel table as not just a collection of rows (numbered) and columns (assigned letter names). You can think about an Excel spreadsheet as a collection of cells – for example, A1 is the upper leftmost cell in an Excel sheet. So while R lets you refer to columns in a dataframe by using the dataframe name followed by a $ and the variable name (e.g., BRFSS_a$X_AGE_G), R also has a way for you to specify the same column using a number, as I show in this table.
As you can see in the table, R has syntax that is kind of like Excel that allows you to specify a particular row and column. In the table above, I give the example of using a dataframe called BRFSS_a and specifying a cell that is in the 4th row (first position within brackets) and the 23rd column (second position, after the comma). Referring to that exact cell in the dataframe would be done numerically with the syntax BRFSS_a[4,23].
As we all realize, spreadsheets in Excel are totally different from dataframes in R (or datasets that are residing in the memory of a data software program in general). We can see the Excel spreadsheet and edit it directly; with R (and SAS and SQL), we need to use commands to edit the data. So if you do a lot of analysis with R dataframes, you probably don’t encounter the situation where you need to refer to a particular cell very often. Therefore, you probably don’t find yourself using this kind of syntax where you are specifying both a row number and a column number in R.
Referring to Columns Using Numerical Syntax in R
But as shown in the table, this numerical syntax allows you to specify entire rows and entire columns using numbers. Imagine that you knew that the 4th row of your data contained a survey that should be removed due to incorrect data. When you create code to remove the row, you could do so by referring to it as BRFSS_a[4,]. By only filling in the first position before the comma within the brackets, we are telling R that we are referring to the entire row – basically, all the columns in row 4.
Referring to columns is done a similar way. Imagine that we knew that variable X_AGE_G was in the 23rd column, and we wanted to use it in processing. A typical maneuver I talk about in my book on building data warehouses in SAS is creating a new variable where I collapse categories from a native categorical variable. Since X_AGE_G is a set of age groups, let’s say I want to create X_AGE_G2 as a new grouping variable where I collapse some categories.
If I was coding the new field directly, I could just use the field names, like BRFSS_a$X_AGE_G, when referring to this native variable in the code. But if I was automating code to do it, it is easier to use the column number when trying to communicate with R. And if X_AGE_G was in the 23rd column, referring to it as BRFSS_a[,23] would be easier to do in automated code.
Referring to Columns Numerically for Automation
The table above shows an example of storing both the row number and the column number in variables (named RN and CN, respectively), and then using those variables in code. So if the column number of X_AGE_G was 23, we could just store 23 in a variable called CN (for column number), and then refer to the column from now on as BRFSS_a[,CN].
Alright, you have probably figured out there is a practical issue with this approach. The BRFSS has over 100 variables in it. How will I easily be able to tell that X_AGE_G is variable 23? The answer is that you can use the which command to help with this maneuver, as I demonstrate in my video.
In our hypothetical, we have a dataframe called BRFSS_a, and we have a column called X_AGE_G. We want to put the number of the X_AGE_G column into a variable called CN so we can use it in processing. Actually, in case we need to analyze other variables, let’s create a naming convention, and name the column number variable X_AGE_G_CN. This would be the code.
X_AGE_G_CN <- which(colnames(BRFSS_a)=="X_AGE_G")
From then on, as long as the column number to X_AGE_G did not change in processing, you could refer to this column in programming as BRFSS_a[,X_AGE_G_CN]. Typically, you would set this variable right before an edit, and then establish a rollback by versioning the dataframe. It is imagined that if there was further processing involving this X_AGE_G that the current value of X_AGE_G_CN would be set each time preceding the processing step.
Added banners and reformatted June 16, 2023.
Referring to columns in R can be done using both number and field name syntax. Although field name syntax is easier to use in programming, my blog demonstrates how you can use column numbers to make automation easier.