Reputation: 163
df1:
a = c(2, 3, 5, 8, 10, 12)
b = c("aa", "bb", "cc", "aa", "bb","aa")
c = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
df1 = data.frame(a, b, c)
df2:
df2 = c("aa", "bb")
I want to evaluate df1$b to see if the value is in df2$d. If it is, then keep the value. If it's not then input "Rare"
Output: df1
a b c
2 aa true
3 bb false
5 rare true
8 aa false
10 bb true
12 aa false
Upvotes: 0
Views: 1220
Reputation: 422
A data.table
way would look the following:
# Load library
library(data.table)
# Convert created tables to data.table object
setDT(df1)
# Add 'rare'
df1[!b %in% df2, b := "rare"]
If df2
is a table (not a vector like it is in the original post and the code chunk above), then you should reference the appropriate column. For example, df2$b
The outcome:
> df1
a b c
1: 2 aa TRUE
2: 3 bb FALSE
3: 5 rare TRUE
4: 8 aa FALSE
5: 10 bb TRUE
6: 12 aa FALSE
Upvotes: 1
Reputation: 39595
This can be done in base R
:
#Data
a = c(2, 3, 5, 8, 10, 12)
b = c("aa", "bb", "cc", "aa", "bb","aa")
c = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
df1 = data.frame(a, b, c,stringsAsFactors = F)
df2 <- data.frame(d=c("aa", "bb"),stringsAsFactors = F)
#Compute
df1$b <- ifelse(df1$b %in% df2$d,df1$b,'rare')
Output:
a b c
1 2 aa TRUE
2 3 bb FALSE
3 5 rare TRUE
4 8 aa FALSE
5 10 bb TRUE
6 12 aa FALSE
Upvotes: 1