Eliminar filas que existen en otro marco de datos?
Tengo los dos marcos de datos siguientes (ejemplo):
Df1:
name profile type strand
A 4.5 1 +
B 3.2 1 +
C 5.5 1 +
D 14.0 1 -
E 45.1 1 -
F 32.8 1 -
G 19.9 1 +
Df2:
name
A
B
C
G
Me gustaría eliminar las filas en df1
para las cuales df1$name = df2$name
para obtener lo siguiente:
Salida:
name profile type strand
D 14.0 1 -
E 45.1 1 -
F 32.8 1 -
Si alguien pudiera decirme qué pieza de código usar sería de mucha ayuda, parecía simple al principio, pero he estado arruinándolo desde ayer.
25
3 answers
Necesita el operador %in%
. Entonces,
df1[!(df1$name %in% df2$name),]
Debería darte lo que quieres.
-
df1$name %in% df2$name
prueba si los valores endf1$name
están endf2$name
- El operador
!
invierte el resultado.
32
Author: csgillespie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-27 08:37:24
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-27 08:37:24
Esto a veces se llama un anti-join :
library(dplyr)
anti_join(df1, df2, by = "name")
20
Author: Hugh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-29 00:33:27
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-29 00:33:27
df1[!(as.character(df1$jobId) %in% as.character(df2$name)), ]
Tuve que añadir as.character
a mi ejecución porque name
no es un carácter sino un factor. ¿No se supone que %in%
convierta esto directamente?
1
Author: user2635283,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-02 02:44:41
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-02 02:44:41