Home > OS >  Why am I not able to remove the specific values(in rows) from my dataset?
Why am I not able to remove the specific values(in rows) from my dataset?

Time:12-27

So, I have been working on this dataset for some time. I wanted to remove the non-countries to enhance the accuracy of the graph.

Here is the code I used

Mental_health_Depression_disorder_Data <- mentalhealth


#finding the average rate for Schizophrenia globally
onlyschizophrenia <-
  mentalhealth %>% select(Entity, Year,  `Schizophrenia (%)`)
onlyschizophrenianona <- na.omit(onlyschizophrenia)
test1 <-
  onlyschizophrenianona %>% filter(`Schizophrenia (%)` < 1) %>% mutate(`Schizophrenia (%)` = `Schizophrenia (%)` * 100) %>% select(Entity, `Schizophrenia (%)`, Year)


#non-countries
noncountries <-
  c(
    "Andean Latin America",
    "Central African Republic",
    "Central Asia",
    "Central Europe, Eastern Europe, and Central Asia",
    "Central Latin America",
    "Central Sub-Saharan Africa",
    "East Asia",
    "Eastern Europe",
    "Eastern Sub-Saharan Africa",
    "High SDI",
    "High-income",
    "High-income Asia Pacific",
    "High-middle SDI",
    "Latin America and Caribbean",
    "Low SDI",
    "Low-middle SDI",
    "North Africa and Middle East",
    "North America",
    "Northern Ireland",
    "Oceania",
    "South Asia",
    "Southeast Asia",
    "Southeast Asia, East Asia, and Oceania",
    "Southern Latin America",
    "Southern Sub-Saharan Africa",
    "United States Virgin Islands",
    "Western Europe",
    "Western Sub-Saharan Africa"
  )


#removing non-countries

#method 1
cleandata1 <- test1[!(row.names(test1) %in% noncountries), ]
view(cleandata1)

#method 2
trial4 <- subset(test1 , test1$Entity != noncountries)
view(trial4)

It seems that both methods I attempted are not successful in removing the specific values from 'test1' . What am I doing wrong?

CodePudding user response:

Try this one.

#method 1
cleandata1 <- test1[!(test1$Entity %in% noncountries), ]
  • Related