How to remove columns in a dataframe in R based on the values from another vector?

user35131

I have a list of values in a vector that i would like to use in deleting the columns found in a dataframe.

For example if my data frame has columns A,B,C,D,E,F,G,H

and my vector has values of C,E,H

i would like my data frame to have columns

A,B,D,F,G,

akrun

There are different options. If we want to remove from the original dataset, assigning to NULL is quick

df1[vecofnames] <- NULL

Another option if we want to subset it to a different object

df2 <- df1[setdiff(names(df1), vecofnames)]

Or with subset

df2 <- subset(df1, select = -vecofnames)

Or in dplyr

library(dplyr)
df2 <- df1 %>%
         select(-vecofnames)     

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to isolate values in a dataframe based on a vector and multiply it by another column in the same dataframe using R?

Subset pandas dataframe on multiple columns based on values from another dataframe

Assign values to columns in Pandas Dataframe based on data from another dataframe

Taking columns from a dataframe based on row values of another dataframe in Python?

Update values in pandas columns based on values from another DataFrame

How to remove rows from a dataframe based on another

How to get another column in a dataframe filled with values from another columns based on multiple conditions?

Fill missing values in a dataframe based on values from another dataframe R

How to fill empty values in a dataframe based on columns in another dataframe?

How to mutate some values of a dataframe based on values from another dataframe column with R

how to create columns from another dataframe rows and populate them based on values from both dataframes

Remove columns from a dataframe based on number of rows with valid values

How to select columns from the dataframe based on variables from another dataframe

Pandas: How to remove rows from a dataframe based on a list of tuples representing values in TWO columns?

Add new columns and add values from another DataFrame based on a filter

How to change values in certain columns based on if the column names contain a string in another column in a dataframe for individual ids in R

How to map columns From one DataFrame onto another based on a column values between the two?

R: Removing multiple columns from data frame based on vector values

How to select columns from matrix in numpy with values from another vector

How to extract values of one dataframe from another dataframe having same columns in r?

Create a vector of all values from all columns in a dataframe in R

Extract values from a DataFrame based on condition on another DataFrame in R

How to subset a dataframe based on columns from another dataframe?

How to create boolean columns based on column values in another dataframe

How to remove rows of a DataFrame based off of data from another DataFrame?

assign values to vector in a dataframe based on conditions from other vectors in R

How to remove all the rows from a matrix that match values in another vector?

How to add values to a dataframe based on conditions in another dataframe in R?

How to split a list of vectors into sublist based on a values of another vector in r