根据向量元素的数量截断数据框 [英] Truncating a dataframe according to count of vector elements
问题描述
我有一个数据框 df
,其中包含三个向量:
I have a dataframe df
, containing three vectors:
subject condition value
01 A 12
01 A 6
01 B 10
01 B 2
02 A 5
02 A 11
02 B 3
02 B 5
02 B 9
...
对于主题01,有四个观测值(因此有四行),其中两个观测值对应于条件A
,另外两个观测值对应于条件B
。假设由于技术错误,对象02有三个 条件B
个观测值。
There are four observations (and hence four rows) for subject 01, with two observations corresponding to condition A
and two corresponding to condition B
. Let's say that due to a technical error, there are three condition B
observations for subject 02.
我的问题是:我如何截断 df
以确保每个条件对每个主题只有两个观察结果(因此删除错误的第三行,其中 condition == B
对于主题02)?
My question is this: how can I truncate df
to ensure that each condition only has two observations for each individual subject (hence removing the erroneous third row where condition==B
for subject 02)?
在此先感谢您的帮助!
推荐答案
这是 dplyr
解决方案-
df %>%
group_by(subject, condition) %>%
filter(row_number() < 3) %>%
ungroup()
# A tibble: 8 x 3
subject condition value
<chr> <chr> <dbl>
1 01 A 12
2 01 A 6
3 01 B 10
4 01 B 2
5 02 A 5
6 02 A 11
7 02 B 3
8 02 B 5
这篇关于根据向量元素的数量截断数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!