Table of Contents
To add a suffix to column names in R, you can use the “paste” function along with the “colnames” command. First, create a vector with the desired suffix. Then, use the “paste” function to add the suffix to the existing column names and assign it to the “colnames” command. This will update the column names with the added suffix. Alternatively, you can use the “gsub” function to replace a specific string in column names with the desired suffix. Both of these methods allow for easy and efficient addition of suffixes to column names in R.
Add Suffix to Column Names in R (With Examples)
You can use the following methods to add a suffix to column names in R:
Method 1: Add Suffix to All Column Names
colnames(df) <- paste(colnames(df), 'my_suffix', sep = '_')
Method 2: Add Suffix to Specific Column Names
colnames(df)[c(1, 3)] <- paste(colnames(df)[c(1, 3)], 'my_suffix', sep = '_')
The following examples show how to use each method with the following data frame:
#create data frame df <- data.frame(points=c(99, 90, 86, 88, 95), assists=c(33, 28, 31, 39, 34), rebounds=c(30, 28, 24, 24, 28)) #view data frame df points assists rebounds 1 99 33 30 2 90 28 28 3 86 31 24 4 88 39 24 5 95 34 28
Example 1: Add Suffix to All Column Names
The following code shows how to add the suffix ‘_total‘ to all column names:
#add suffix '_total' to all column names
colnames(df) <- paste(colnames(df), 'total', sep = '_')
#view updated data frame
df
points_total assists_total rebounds_total
1 99 33 30
2 90 28 28
3 86 31 24
4 88 39 24
5 95 34 28
Notice that the suffix ‘_total‘ has been appended to the end of each column name.
Example 2: Add Suffix to Specific Column Names
The following code shows how to add the suffix ‘_total‘ to specific column names:
#add suffix '_total' to column names in index positions 1 and 3
colnames(df)[c(1, 3)] <- paste(colnames(df)[c(1, 3)], 'total', sep = '_')
#view updated data frame
df
points_total assists rebounds_total
1 99 33 30
2 90 28 28
3 86 31 24
4 88 39 24
5 95 34 28
Notice that the suffix ‘_total‘ has only been appended to the columns in index positions 1 and 3.
Cite this article
stats writer (2024). How can I add a suffix to column names in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-a-suffix-to-column-names-in-r/
stats writer. "How can I add a suffix to column names in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-a-suffix-to-column-names-in-r/.
stats writer. "How can I add a suffix to column names in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-a-suffix-to-column-names-in-r/.
stats writer (2024) 'How can I add a suffix to column names in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-a-suffix-to-column-names-in-r/.
[1] stats writer, "How can I add a suffix to column names in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add a suffix to column names in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
