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!
CodePudding user response:
with subset using dplyr
you can use the code below
library(dplyr)
df %>% subset(!is.na(value) & bs_Scores != "bs_24" )
CodePudding user response:
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))