Let's assume I have the following:
df = data.frame(attribute_1 = c(234, 456, 778, 89, 77, 665, 44, 33),
attribute_2 = c( 9,67,78,777,6, 1, 22, 100))
vec = c(1,44,33,667,77)
subset(df, df$attribute_1 != vec)
how can I exclude the values in vec from the df ?
The code as it is written here gets an error message
Instead of !=
, use %in%
with !
as !=
or ==
are elementwise comparison operators which works only when the lengths
are same or if it is having a length of 1 which gets recycled
subset(df, ! attribute_1 %in% vec)
attribute_1 attribute_2
1 234 9
2 456 67
3 778 78
4 89 777
6 665 1
You can either do
subset(df, ! attribute_1 %in% vec)
or (using package tidyverse )
df %>% filter(!attribute_1 %in% vec)
or simply
df[!df$attribute_1 %in% vec, ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.