【发布时间】:2016-05-27 19:28:23
【问题描述】:
我正在尝试从我的数据集中删除包含某些植被类型的行。我想从我的未调查数据中删除那些在我的调查数据中找不到植被类型的行。我找到了一种方法来做到这一点,但正在寻找一种单线方法。我目前正在这样做:
> setdiff(unsurveyed_1$VEGETATION, surveyed_1$VEGETATION)
返回七种植被类型,然后我删除它们:
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Acer rubrum- Nyssa sylvatica saturated forest alliance",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Acer rubrum/Quercus coccinea-Acer rubrum-Vaccinium corybosum-Vaccinium palladium",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Building",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Parking Lot",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Prunus serotina",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Typha (angustifolia, latifolia) - (Schoenoplectus spp.) Eastern Herbaceous Vegetation",]
> unsurveyed_1 <- unsurveyed_1[!unsurveyed_1$VEGETATION == "Water",]
我尝试了一些不同的选项,包括子集,但到目前为止收效甚微,我认为这将是我的最佳选择。我也在寻找与 intersect 类似的东西,但我假设它会得到类似的答案。
编辑: 除了使用@Cath 提供的代码之外,我还对其进行了编辑以得到相反的结果。
> unsurveyed_2 <- unsurveyed_2[unsurveyed_2$VEGETATION %in% setdiff(unsurveyed_2$VEGETATION, surveyed_1$VEGETATION), ]
【问题讨论】:
-
unsurveyed_1[unsurveyed_1$VEGETATION %in% unique(surveyed_1$VEGETATION), ]呢? -
R 中的数据框中没有任何称为
deleting的内容。如@Cath 所述将其子集。 -
谢谢@Cath。这正是我想要的。
标签: r intersect set-difference