Delete observations in a column that fit a certain criteria


#1

Hello, let's say I have the following data frame in R:

SERIALNO NP TYPE ACR BDSP BLD
70 4 1 House on less than one acre 2 One-family house detached
106 0 1 House on less than one acre 2 2 Apartments
163 2 1 House on less than one acre 2 One-family house detached
78 1 1 House on less than one acre 0 One-family house detached
243 2 1 House on less than one acre 4 One-family house detached
300 1 1 House on less than one acre 0 Mobile home or trailer
603 3 1 House on less than one acre 3 One-family house detached

I would like to create a new data frame wherein rows where BDSP = 0 are deleted. Pretty straightforward but my use of subset() is not working. Thank you in advance.

Greg


#2

You should include a reprex with your question.

A prose description isn't sufficient, you also need to make a simple reprex that:

  1. Builds the input data you are using.
  2. The function you are trying to write, even if it doesn't work.
  3. Usage of the function you are trying to write, even if it doesn't work.
  4. Builds the output data you want the function to produce.

You can learn more about reprex's here:

Right now the is an issue with the version of reprex that is in CRAN so you should download it directly from github.

Until CRAN catches up with the latest version install reprex with

devtools::install_github("tidyverse/reprex")

if this gives you an error saying that devtools is not available then use

install.packages("devtools")

and try again.

The reason we ask for a reprex is that it is the easiest and quickest way for someone to understand the issue you are running into and answer it.

Nearly everyone here who is answering questions is doing it on their own time and really appreciate anything you can do to minimize that time.

In any case here is a reprex that shows a couple of ways to select rows from a data.frame:

suppressPackageStartupMessages(library(tidyverse))

# make data.frame
t <- tribble(
~SERIALNO,  ~NP,    ~TYPE, ~ACR,    ~BDSP, ~BLD,
70, 4,  1,  "House on less than one acre",  2,  "One-family house detached",
106,    0,  1,  "House on less than one acre",  0,  "2 Apartments"
)

t
#> # A tibble: 2 x 6
#>   SERIALNO    NP  TYPE ACR                          BDSP BLD              
#>      <dbl> <dbl> <dbl> <chr>                       <dbl> <chr>            
#> 1      70.    4.    1. House on less than one acre    2. One-family house…
#> 2     106.    0.    1. House on less than one acre    0. 2 Apartments
# using tidyverse
filter(t, BDSP != 0)
#> # A tibble: 1 x 6
#>   SERIALNO    NP  TYPE ACR                          BDSP BLD              
#>      <dbl> <dbl> <dbl> <chr>                       <dbl> <chr>            
#> 1      70.    4.    1. House on less than one acre    2. One-family house…

# not using tidyverse
t[t["BDSP"]!= 0,]
#> # A tibble: 1 x 6
#>   SERIALNO    NP  TYPE ACR                          BDSP BLD              
#>      <dbl> <dbl> <dbl> <chr>                       <dbl> <chr>            
#> 1      70.    4.    1. House on less than one acre    2. One-family house…

Created on 2018-03-20 by the reprex package (v0.2.0).