【问题标题】:Subsetting dataframe with values of two columns are equal to each other at same time, to check for zero at all other columns具有两列值的子集数据帧同时彼此相等,以检查所有其他列的零
【发布时间】:2018-01-13 06:53:29
【问题描述】:

我是 R 编程的初学者。我目前正在使用具有 865921 行和 8 列的数据框。我想对数据集进行子集化,其中两列具有相同的值,并且在同一实例中,我还想检查所有其他列的零值。但我无法克服这一步。如果有人可以帮助或建议任何前进的步骤,那将非常有帮助。提前致谢。

【问题讨论】:

  • “检查所有其他列的零值”是什么意思?
  • 第一步是new_df <- subset(old_df,colA == colB)
  • new_df
  • 检查所有其他列中出现的零 @ Moody_Mudskipper

标签: r loops dataframe subset multiple-conditions


【解决方案1】:

这应该可以满足您的需求:

# create a sample dataset
auto <- mtcars

# subset by columns being equal
auto_2 <- auto[auto$gear == auto$carb, ]

# find where the zeros are
zeros <- apply(auto_2, 1, function(x) all(x != 0))

# subset to remove rows with zero
auto_3 <- auto_2[zeros, ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多