【发布时间】:2020-12-17 04:51:58
【问题描述】:
我见过这个Subsetting a data frame based on a logical condition on a subset of rows 和那个https://statisticsglobe.com/filter-data-frame-rows-by-logical-condition-in-r
我想根据 row.names 中的特定值对 data.frame 进行子集化。
data <- data.frame(x1 = c(3, 7, 1, 8, 5), # Create example data
x2 = letters[1:5],
group = c("ga1", "ga2", "gb1", "gc3", "gb1"))
data # Print example data
# x1 x2 group
# 3 a ga1
# 7 b ga2
# 1 c gb1
# 8 d gc3
# 5 e gb1
我想根据组对data 进行子集化。一个子集应该是在其组中包含 a 的行,在其组中包含 b 的行和在其组中包含 c 的行。可能是grepl?
结果应该是这样的
data.a
# x1 x2 group
# 3 a ga1
# 7 b ga2
data.b
# x1 x2 group
# 1 c gb1
# 5 e gb1
data.c
# 8 d gc3
我会对如何对这些输出示例之一进行子集化感兴趣,或者循环也可以工作。
我从这里修改了示例https://statisticsglobe.com/filter-data-frame-rows-by-logical-condition-in-r
【问题讨论】:
-
您的问题已完全按照您的要求得到回答。好问题。