Nad
Nad

Reputation: 31

extracting rows out of a table based on a string

I'm using a dataset that has a lot of rows but I only want to keep rows based on their value in col A. Here is a snippet of my table;

ï..hlpi_name        year    age size    income  expenditure eqv_income  eqv_exp
All households      2008    35.9    2.7 46704   42394       26869       25132
Beneficiary         2008    29.9    2.6 23404   25270       14258       15824
Income quintile 1   2008    40.0    2.3 16747   21145       13402       14408
random figure       2008    34.7    2.8 31308   29855       18917       18266
Income quintile 3   2008    31.5    3.0 49106   46561       26870       24672
Income quintile 4   2008    35.3    2.6 61674   52776       36691       31958

So using tidyverse i've tried the following code;

gfDS2 <- gfDS
gfDS2 <- gfDS2 %>%
filter(!grepl("All households", ï..hlpi_name))
filter(!grepl("Beneficiary", ï..hlpi_name))
filter(!grepl("expenditure quintile 1", ï..hlpi_name))
filter(!grepl("expenditure quintile 2", ï..hlpi_name))
filter(!grepl("expenditure quintile 3", ï..hlpi_name))
filter(!grepl("expenditure quintile 4", ï..hlpi_name))
filter(!grepl("expenditure quintile 5", ï..hlpi_name))

but its giving me this error;

Error in grepl("Beneficiary", ï..hlpi_name): object 'ï..hlpi_name' not found
Traceback:

1. filter(!grepl("Beneficiary", ï..hlpi_name))
2. grepl("Beneficiary", ï..hlpi_name)

I dont understand why it can't see the column name as an object :(

Upvotes: 1

Views: 22

Answers (2)

Onyambu
Onyambu

Reputation: 79188

You could also decide to collapse all of them ie:

library(dplyr)
gfDS2 %>%
   filter(!grepl("All households|Beneficiary|expenditure quantile [1-5]{1}", `ï..hlpi_name`))

Upvotes: 1

akrun
akrun

Reputation: 886938

After the second line of code , the %>% was not present. In addition, it may be better to backquote if the names begin with special characters

library(dplyr)
gfDS2 %>%
   filter(!grepl("All households", `ï..hlpi_name`)) %>%
   filter(!grepl("Beneficiary", `ï..hlpi_name`)) %>%
    ...

Upvotes: 1

Related Questions