Subset Data Frame to Include Only Levels of One Factor That Have Values in Both Levels of Another Factor

Subset data frame to include only levels of one factor that have values in both levels of another factor

Here is one option with data.table

library(data.table)
setDT(d)[, .SD[all(c("juvenile", "adult") %in% age)], ID]

Or a base R option with ave

d[with(d, ave(as.character(age), ID, FUN = function(x) length(unique(x)))>1),]
# ID age size
#1 a1 juvenile -1.4545407
#2 a2 juvenile -0.4695317
#3 a3 juvenile 0.2271316
#5 a1 juvenile 0.2961210
#6 a2 adult -0.8331993
#9 a1 adult -0.6924967
#10 a3 adult -0.4619550

R: Subset factor levels that co-occur with two levels from another factor

Here is one idea. You define groups with Gene. In each group, you want to check if there is more than one unique value.

group_by(df, Gene) %>% 
filter(n_distinct(Tissue) >= 2)

Gene Tissue
<fct> <fct>
1 GeneA TissueA
2 GeneA TissueB

Subsetting a data.frame based on factor levels in a second data.frame

df.1[,unique(df.2$Var[which(df.2$Info=="X1")])]

           A            C
1 0.8924861 0.7149490854
2 0.5711894 0.7200819517
3 0.7049629 0.0004052017
4 0.9188677 0.5007302717
5 0.3440664 0.9138259818
6 0.8657903 0.2724015017
7 0.7631228 0.5686033906
8 0.8388003 0.7377064163
9 0.0796059 0.6196693045
10 0.5029824 0.8717568610

subset dataframe based on hierarchical preference of factor levels within column in R

You can use order with match and then simply !duplicated:

df1 <- df1[order(match(df1$method, c("CACL","KCL","H2O"))),]
df1[!duplicated(df1$ID),]
# ID method
#1 1 CACL
#5 2 KCL
#6 3 H2O

#Variant not changing df1
i <- order(match(df1$method, c("CACL","KCL","H2O")))
df1[i[!duplicated(df1$ID[i])],]

How to subset a dataframe by factor levels of another in R?

An alternative solution might be:

B[B$factor%in%A$factor,]

Subset a dataframe by multiple factor levels

You can use %in%

  data[data$Code %in% selected,]
Code Value
1 A 1
2 B 2
7 A 3
8 A 4

R - How to extract a subset of a data frame containing many observations of two factors, each with multiple levels based on conditions?

Here is a dplyr solution:

library(dplyr)
group_by(df, Alphabet) %>% filter(!(L == 3 & any(L %in% c(1, 2))))


Related Topics



Leave a reply



Submit