How can I filter into a new data frame conditional on the columns existing in another data frame?

eBopBob

So I have two dataframes.

df1:

   Date Bond2 Bond3 Bond5 Bond6 Bond7 Bond8 Bond10 Bond11 Bond12 Bond14 Bond15 Bond16 Bond17
1 41275    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
2 41276    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
3 41277    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
4 41278    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
5 41279    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA

df1:

   Date Bond2 Bond3 Bond4 Bond5 Bond6 Bond7 Bond8 Bond10 Bond11 Bond12 Bond14 Bond16 Bond17 Bond19
1 41275    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
2 41276    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
3 41277    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA
4 41278    NA    NA    NA    NA    NA    NA    NA     NA     NA     NA     NA     NA     NA     NA

I want to create a new df3 which is df1 where all the columns are contained in df2, and then a df4 which is df2 where all the columns are contained within df1.

I was thinking along the lines of filter(df1, names() %in% names(df2)) or select(names(df1) %in% names(df2) but neither works.

Thanks

broti

If you want to stick to tidyr, one way would be:

df3 <- df1 %>% select(all_of(names(df2))
df4 <- df2 %>% select(all_of(names(df1))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I create a new data frame based on the existing columns?

How can I rename all columns of a data frame based on another data frame in R?

How can I add columns in a data frame?

How to create new columns in a new data frame from information in an existing data frame in R

Adding a vector as new columns in existing data frame

How can I add additional columns to an existing data.frame, that are aligned on one specific column already in the data.frame?

How I can calculate the variance across all columns in a data frame in R according to the values of another data frame using Dplyr?

How can I add a new column and use an existing column in a data frame in R?

How can I make a data frame of the unique values in an existing column?

How can I create a data frame with all existing variables (at once)?

Pandas - Filter data frame with another data frame

creating a new data frame by extracting columns from one data frame based on the value of column in another data frame

Dropping columns in a data frame that are not in another data frame

How do I copy the value of columns in one data frame to another data frame based on column names?

When using mutate for several columns in dplyr, how can I reference to another data frame by row name?

How can I create a new wide data frame with rows based on all combos of values in two columns?

How can I create a new series by using specific rows and columns of a pandas data frame?

How can I create a new column in a pandas data frame by extracting words from sentences in another column?

How can I sort a data frame conditionally by multiple columns in R?

How can I select discrete columns from data frame

Pandas: How can I make data frame with double columns?

How can I summarize a data frame in a "long" format by multiple columns?

How can I rollapply in R with two columns of a data frame?

Calculating a new accumulative column in a data frame based on existing columns

Calculating new column in a data frame based on existing columns

Creating a new accumulative column in a data frame based on existing columns

R: How can I transfer values from one data frame to another data frame depending on certain circumstances?

How can I divide positive elements of a data frame by a specific data frame column and negative elements by another?

How can I search in a data frame if all possible combinations exist in another data frame in R using dplyr?

TOP Ranking

HotTag

Archive