I am trying to remove rows that belong to the same TRIAL group and Trait group with empty traitvalue
dt <- tibble(
TRIAL = c("M20-07RND1", "M20-07RND1", "M20-07RND1", " M20-07RND1", " M20-06RND5", " M20-06RND5", " M20-06RND5", " M20-06RND5", " V-01RND1", " V-01RND1", " V-01RND1", "V-01RND1"),
PLOT= c(1, 2, 3, 1, 6, 3, 2, 3, 1,67,34,34),
trait = c("ASI", "ASI", "AD", "AD", "ASI", "ASI", "FY", "FY", "FOR", "FOR", "CY", "CY"),
trialValue = c(2, 7, 2, 2, NA,34 ,NA ,NA ,10 ,NA ,NA ,NA)
)
# # A tibble: 11 x 4
# TRIAL PLOT trait trialValue
<chr> <dbl> <chr> <dbl
#1 M20-07RND1 1 ASI 2
#2 M20-07RND1 2 ASI 7
#3 M20-07RND1 3 AD 2
#4 M20-07RND1 1 AD 2
#5 M20-06RND5 6 ASI
#6 M20-06RND5 3 ASI 34
#7 M20-06RND5 2 FY
#8 M20-06RND5 3 FY
#9 V-01RND1 1 FOR 10
#10 V-01RND1 67 FOR
#11 v-01RND1 34 CY
#12 v-01RND1 34 CY
This is what I want to achieve: If a group of trait has an empty traitvalue and belong to the same TRIAL Group then remove the whole rows where the condition met.
# # A tibble: 11 x 4
# TRIAL PLOT trait trialValue
<chr> <dbl> <chr> <dbl
#1 M20-07RND1 1 ASI 2
#2 M20-07RND1 2 ASI 7
#3 M20-07RND1 3 AD 2
#4 M20-07RND1 1 AD 2
#5 M20-06RND5 6 ASI
#6 M20-06RND5 3 ASI 34
#9 V-01RND1 1 FOR 10
#10 V-01RND1 67 FOR
I don’t know how to go about it, kindly help out.
CodePudding user response:
library(dplyr)
dt %>%
group_by(TRIAL, trait) %>%
filter(sum(trialValue, na.rm = TRUE) != 0)
# A tibble: 8 x 4
# Groups: TRIAL, trait [5]
TRIAL PLOT trait trialValue
<chr> <dbl> <chr> <dbl>
1 "M20-07RND1" 1 ASI 2
2 "M20-07RND1" 2 ASI 7
3 "M20-07RND1" 3 AD 2
4 " M20-07RND1" 1 AD 2
5 " M20-06RND5" 6 ASI NA
6 " M20-06RND5" 3 ASI 34
7 " V-01RND1" 1 FOR 10
8 " V-01RND1" 67 FOR NA
CodePudding user response:
Use any
:
library(dplyr)
dt %>%
group_by(TRIAL, trait) %>%
filter(any(!is.na(trialValue)))
output
# A tibble: 8 x 4
# Groups: TRIAL, trait [5]
TRIAL PLOT trait trialValue
<chr> <dbl> <chr> <dbl>
1 "M20-07RND1" 1 ASI 2
2 "M20-07RND1" 2 ASI 7
3 "M20-07RND1" 3 AD 2
4 " M20-07RND1" 1 AD 2
5 " M20-06RND5" 6 ASI NA
6 " M20-06RND5" 3 ASI 34
7 " V-01RND1" 1 FOR 10
8 " V-01RND1" 67 FOR NA