简体   繁体   English

删除某些列值与另一列不匹配的行(全部在同一数据框中)

[英]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 different Ops.factor(importer2, mother)错误:因子级别集不同

How would I go about dropping all the rows/cases in which importer 2 and mother don't match? 我该如何删除importer 2mother不匹配的所有行/案例?

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.

相关问题 从数据框中删除其列值与另一个数据框的列值不匹配的数据 - R - remove rows from data frame whose column values don't match another data frame's column values - R 用另一个值将列中的值替换为数据框(对所有值都相同) - replace values in a column into Data Frame with another value (same for all) 将每一行的值与匹配某一列但不匹配另一列的所有行相乘 - Multiply a value for each row with all rows that match a certain column while not matching another column 如何对共享另一列中给出的相同ID的所有行在数据框中标准化列值? - How can I normalize column values in a data frame for all rows that share the same ID given in another column? 删除列中的值与另一个数据集中的列中的值不匹配的所有行 - Delete all rows where value in a column does not match value in column in another dataset R:根据数据框列中的行值删除行 - R: removing rows based on row value in a column of a data frame 更改列数据框某些行的值:分类表 - Change value of certain rows of a column data frame: taxonomic table 在 R 中查找包含特定值的行中数据框列的总和 - Finding sum of data frame column in rows that contain certain value in R 比较数据框中某列中的元素与另一数据框中同一列中的另一个元素以获取R中的对应行 - comparing an element in a column in a data frame with another element in the same column in another data frame for corresponding rows in R 仅对间隔不位于另一个data.frame中的那些行进行子集 - Subset only those rows whose intervals does not fall within another data.frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM