Table of Contents
In R, there are various methods for keeping certain columns of data when working with a dataset. One way is to use the “subset” function, which allows users to specify the columns they want to keep. Another method is to use the “select” function from the “dplyr” package, which allows for more advanced selection of columns based on specific criteria. Additionally, the “keep” function from the “purrr” package can be used to keep columns that meet a certain condition. Overall, these methods provide flexibility and control in keeping desired columns for further analysis and manipulation in R.
Keep Certain Columns in R (With Examples)
You can use the following methods to only keep certain columns in a data frame in R:
Method 1: Specify Columns to Keep
#only keep columns 'col1' and 'col2'
new_df = subset(df, select = c(col1, col2))
Method 2: Specify Columns to Drop
#drop columns 'col3' and 'col4'
new_df = subset(df, select = c(col3, col4))
The following examples show how to use each method with the following data frame in R:
#create data frame df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B'), points=c(19, 14, 14, 29, 25, 30), assists=c(4, 5, 5, 4, 12, 10), rebounds=c(9, 7, 7, 6, 10, 11)) #view data frame df team points assists rebounds 1 A 19 4 9 2 A 14 5 7 3 A 14 5 7 4 B 29 4 6 5 B 25 12 10 6 B 30 10 11
Method 1: Specify Columns to Keep
The following code shows how to define a new data frame that only keeps the “team” and “assists” columns:
#keep 'team' and 'assists' columns
new_df = subset(df, select = c(team, assists))
#view new data frame
new_df
team assists
1 A 4
2 A 5
3 A 5
4 B 4
5 B 12
6 B 10
The resulting data frame only keeps the two columns that we specified.
Method 2: Specify Columns to Drop
The following code shows how to define a new data frame that drops the “team” and “assists” columns from the original data frame:
#drop 'team' and 'assists' columns
new_df = subset(df, select = -c(team, assists))
#view new data frame
new_df
points rebounds
1 19 9
2 14 7
3 14 7
4 29 6
5 25 10
6 30 11
The resulting data frame drops the “team” and “assists” columns from the original data frame and keeps the remaining columns.
Cite this article
stats writer (2024). How can I keep certain columns in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-keep-certain-columns-in-r-and-what-are-some-examples-of-how-to-do-so/
stats writer. "How can I keep certain columns in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-keep-certain-columns-in-r-and-what-are-some-examples-of-how-to-do-so/.
stats writer. "How can I keep certain columns in R, and what are some examples of how to do so?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-keep-certain-columns-in-r-and-what-are-some-examples-of-how-to-do-so/.
stats writer (2024) 'How can I keep certain columns in R, and what are some examples of how to do so?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-keep-certain-columns-in-r-and-what-are-some-examples-of-how-to-do-so/.
[1] stats writer, "How can I keep certain columns in R, and what are some examples of how to do so?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I keep certain columns in R, and what are some examples of how to do so?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
