根据列中的条件对数据框中的行进行子集/过滤 [英] Subset / filter rows in a data frame based on a condition in a column

查看:65
本文介绍了根据列中的条件对数据框中的行进行子集/过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数据框"foo",如何从"foo"中仅选择那些行?例如 foo $ location =有" ?

Given a data frame "foo", how can I select only those rows from "foo" where e.g. foo$location = "there"?

foo = data.frame(location = c("here", "there", "here", "there", "where"), x = 1:5, y = 6:10)
foo
#   location x  y
# 1     here 1  6
# 2    there 2  7
# 3     here 3  8
# 4    there 4  9
# 5    where 5 10

所需结果,"bar":

Desired result, "bar":

#   location x y
# 2    there 2 7
# 4    there 4 9

推荐答案

以下是两种主要方法.我更喜欢它的可读性:

Here are the two main approaches. I prefer this one for its readability:

bar <- subset(foo, location == "there")

请注意,您可以将许多条件字符串与& | 组合在一起,以创建复杂的子集.

Note that you can string together many conditionals with & and | to create complex subsets.

第二个是索引方法.您可以使用数字或布尔切片来索引R中的行. foo $ location ==有" 返回一个向量,该向量的 T F 值的长度与 foo的行的长度相同.您可以执行此操作以仅返回条件返回true的行.

The second is the indexing approach. You can index rows in R with either numeric, or boolean slices. foo$location == "there" returns a vector of T and F values that is the same length as the rows of foo. You can do this to return only rows where the condition returns true.

foo[foo$location == "there", ]

这篇关于根据列中的条件对数据框中的行进行子集/过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆