How to delete a specific value based on the column before in R

Viewed 70

I have a dataframe in R that has a list of variables. However, I have printed the last two columns which I am interested in. I want to delete all occurrences of NAs in the value column only when bs_Scores == bs_24.

bs_Scores   value
bs_0    16.7
bs_1    41.7
bs_12   33.3
bs_24   NA
bs_0    25
bs_1    41.7
bs_12   NA
bs_24   0
bs_0    16.7
bs_1    41.7
bs_12   16.7
bs_24   16.7
bs_0    NA

Essentially, I want to delete all the rows within bs_24 that have an NA in the value column while preserving all the NAs in bs_0, bs_1 and bs_12.

SO it should look like this

bs_Scores   value
bs_0    16.7
bs_1    41.7
bs_12   33.3
bs_0    25
bs_1    41.7
bs_12   NA
bs_24   0
bs_0    16.7
bs_1    41.7
bs_12   16.7
bs_24   16.7
bs_0    NA

Thank you!

3 Answers

with subset using dplyr you can use the code below


library(dplyr)

df %>% subset(!is.na(value) & bs_Scores != "bs_24" )

Using base R with subset

subset(df1, !((is.na(value) & bs_Scores == 'bs_24')|bs_Scores == ""))

-output

 bs_Scores value
1       bs_0  16.7
2       bs_1  41.7
3      bs_12  33.3
5       bs_0  25.0
6       bs_1  41.7
7      bs_12    NA
8      bs_24   0.0
9       bs_0  16.7
10      bs_1  41.7
11     bs_12  16.7
12     bs_24  16.7
13      bs_0    NA

data

df1 <- structure(list(bs_Scores = c("bs_0", "bs_1", "bs_12", "bs_24", 
"bs_0", "bs_1", "bs_12", "bs_24", "bs_0", "bs_1", "bs_12", "bs_24", 
"bs_0"), value = c(16.7, 41.7, 33.3, NA, 25, 41.7, NA, 0, 16.7, 
41.7, 16.7, 16.7, NA)), class = "data.frame", row.names = c(NA, 
-13L))

A dplyr solution:

library(tidyverse)

bs_scores <- tibble::tribble(
               ~bs_Scores, ~value,
                   "bs_0",   16.7,
                   "bs_1",   41.7,
                  "bs_12",   33.3,
                  "bs_24",     NA,
                   "bs_0",     25,
                   "bs_1",   41.7,
                  "bs_12",     NA,
                  "bs_24",      0,
                   "bs_0",   16.7,
                   "bs_1",   41.7,
                  "bs_12",   16.7,
                  "bs_24",   16.7,
                   "bs_0",     NA
               )
bs_scores %>% 
  filter(!(bs_Scores == "bs_24" & (is.na(value))))
#> # A tibble: 12 × 2
#>    bs_Scores value
#>    <chr>     <dbl>
#>  1 bs_0       16.7
#>  2 bs_1       41.7
#>  3 bs_12      33.3
#>  4 bs_0       25  
#>  5 bs_1       41.7
#>  6 bs_12      NA  
#>  7 bs_24       0  
#>  8 bs_0       16.7
#>  9 bs_1       41.7
#> 10 bs_12      16.7
#> 11 bs_24      16.7
#> 12 bs_0       NA

Created on 2021-10-11 by the reprex package (v2.0.1)

Related