drop variables in r

To drop variables from a data frame in R, you can use the select() function from the dplyr package. select() is used to filter the columns of a data frame based on their names. You can use the - sign to exclude specific columns from the selection.

Here's an example:

main.r
library(dplyr)

# Create a sample data frame
df <- data.frame(
  a = 1:5,
  b = 6:10,
  c = 11:15
)

# Drop the 'b' column from the data frame
df_new <- select(df, -b)

# View the updated data frame
df_new
206 chars
15 lines

In this example, we use select() to drop the b column from the df data frame. The resulting data frame df_new only contains the a and c columns.

You can also use the select() function to keep only specific columns, by listing their names inside the function call. For example:

main.r
# Keep only the 'a' and 'c' columns
df_new <- select(df, a, c)
63 chars
3 lines

This will create a new data frame with only the a and c columns from the original data frame.

gistlibby LogSnag