[英]Removing certain rows whose column value does not match another column (all within the same data frame)
I'm attempting to remove all of the rows (cases) within a data frame in which a certain column's value does not match another column value. 我正在尝试删除某个特定列的值与另一个列的值不匹配的数据框中的所有行(案例)。
The data frame bilat_total
contains these 10 columns/variables: 数据框
bilat_total
包含以下10列/变量:
bilat_total[,c("year", "importer1", "importer2", "flow1",
"flow2", "country", "imports", "exports", "bi_tot",
"mother")]
Thus the table's head is: 因此,桌子的头是:
year importer1 importer2 flow1 flow2 country
6 2009 Afghanistan Bhutan NA NA Afghanistan
11 2009 Afghanistan Solomon Islands NA NA Afghanistan
12 2009 Afghanistan India 516.13 120.70 Afghanistan
13 2009 Afghanistan Japan 124.21 0.46 Afghanistan
15 2009 Afghanistan Maldives NA NA Afghanistan
19 2009 Afghanistan Bangladesh 4.56 1.09 Afghanistan
imports exports bi_tot mother
6 6689.35 448.25 NA United Kingdom
11 6689.35 448.25 NA United Kingdom
12 6689.35 448.25 1.804361e-02 United Kingdom
13 6689.35 448.25 6.876602e-05 United Kingdom
15 6689.35 448.25 NA United Kingdom
19 6689.35 448.25 1.629456e-04 United Kingdom
I've attempted to remove all the cases in which importer2
do not match mother
by making a subset: 我试图通过制作一个子集来删除所有
importer2
不匹配mother
的情况:
subset(bilat_total, importer2 == mother)
But each time I do, I get the error: 但是每次我这样做,都会收到错误消息:
Error in
Ops.factor(importer2, mother)
: level sets of factors are differentOps.factor(importer2, mother)
错误:因子级别集不同
How would I go about dropping all the rows/cases in which importer 2
and mother
don't match? 我该如何删除
importer 2
和mother
不匹配的所有行/案例?
The error may be because the columns are factor
class. 该错误可能是因为列是
factor
类。 We can convert the columns to character
class and then compare to subset
the rows. 我们可以将列转换为
character
类,然后比较这些行的subset
。
subset(bilat_total, as.character(importer2) == as.character(mother))
Based on the data example showed 根据数据示例显示
subset(bilat_total, importer2 == mother)
# Error in Ops.factor(importer2, mother) :
# level sets of factors are different
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.