0

i have a data frame, from this I created another dataframe,
which consists of a selection after a number of conditions. The result is a number of countries that satisfy the condition n > 11. Now I want to continue working with only these countries. How can I copy values from the first dataset based on the countries in the selection?

My first df looks like:

enter image description here

and the second (so the selection of countries):

enter image description here

In my final df I need every column and row from my 1st df (but only for the countries present in the second df)

2 Answers 2

3

I'm not sure about your data and reason using second dataframe, but let first and second data as df1 and df2, then

library(dplyr)

df1 %>%
  filter(Country.o... %in% df2$Country.o...)

(I cannot find out what is the column name. You should not post your data as an imange)

Sign up to request clarification or add additional context in comments.

1 Comment

That is exactly what I meant. Thank you so much
1

Two options -

  1. Do an inner join

    a) Base R -

df3 <- merge(df1, df2, by = 'Country')

b) dplyr -

library(dplyr)
df3 <- inner_join(df1, df2, by = 'Country')
  1. Instead of creating df2 from df1, I would just filter the 1st one to get the resulting dataframe.
df3 <- df1 %>% group_by(Country) %>% filter(n() > 11)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.