I am working with a WHO's data on suicide rates and I want to extract data for the UK only. How do I do delete all rows that do not contain United Kingdom in their country column?
Thanks!
I am working with a WHO's data on suicide rates and I want to extract data for the UK only. How do I do delete all rows that do not contain United Kingdom in their country column?
Thanks!
There are quite a few different ways to get subsets of your observations in R
https://www.statmethods.net/management/subset.html
Here are two examples using base R functions that are automatically loaded when you fire up R (note: I'm just wrapping them in head()
to limit the number of rows that print)
iris <- iris
head(iris[ which(iris$Species == "setosa"), ])
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
head(subset(iris, Species == "setosa"))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
Created on 2018-12-27 by the reprex package (v0.2.1.9000)
You can give the subset a new name, or overwrite the data frame from which you're working, if you so choose.
You could also do this with dplyr's filter()
function.
library(dplyr, warn.conflicts = FALSE)
head(filter(iris, Species == "setosa"))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
Created on 2018-12-27 by the reprex package (v0.2.1.9000)
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.