Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a dataframe from some rows of a already existing dataframe in R

I need to create a new dataframe from the data of 4 different countries of this already existing dataframe:

Country        Happiness.Score      GDP         year
Switzerland    7.587                1.39651     2015
Iceland        7.561                1.30232     2015
Denmark        7.527                1.32548     2015
Norway         7.522                1.459       2015
Canada         7.427                1.32629     2015
Finland        7.406                1.29025     2015
Netherlands    7.378                1.32944     2015
Sweden         7.364                1.33171     2015
New Zealand    7.286                1.25018     2015
Australia      7.284                1.33358     2015
Israel         7.278                1.22857     2015
Costa Rica     7.226                0.95578     2015
Austria        7.2                  1.33723     2015
Mexico         7.187                1.02054     2015
United States  7.119                1.39451     2015
Brazil         6.983                0.98124     2015
Ireland        6.94                 1.33596     2015
Belgium        6.937                1.30782     2015

OBS.: This dataframe above is just an example, the original dataframe has way more countries and which one has data from the following years: 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Something like

selected_countries <- c("Iceland", "Norway", "Spain", "Nigeria")
new_dd <- dd[dd$Country %in% selected_countries, ]

or

new_dd <- subset(dd, Country %in% selected_countries)

or

library(dplyr)
new_dd <- dd %>% filter(Country %in% selected_countries)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading