简体   繁体   English

如何根据因子水平对观察结果进行子集化

[英]How to subset observations based on factor level

I have a data frame and need to group the observations based on one variable(vector)'s value into equal number of groups. 我有一个数据框,需要根据一个变量(向量)的值将观察结果分组为相等数量的组。 Below is to illustrate, I used to cut function to create factor based on vector A' value. 下面是说明,我曾经使用过剪切函数来基于向量A'的值创建因子。 It create three levels, and each value of the vector A fall into one of the level/group. 它创建三个级别,向量A的每个值都属于级别/组之一。 Now how do I extract/refer to the vector A value based on the cut level of X. For instance, if I want to select observations of A fall into level 1:[1.2.33], how to do that? 现在,如何基于X的剪切级别提取/引用向量A的值。例如,如果要选择A的观察值落入级别1:[1.2.33],该怎么做?

> A<-1:5  
> X<-cut(A,breaks=quantile(A,probs=c(0:3)/3),labels=1:3,include.lowest=TRUE)  
> A  
[1] 1 2 3 4 5  
> X  
[1] [1,2.33]    [1,2.33]    (2.33,3.67] (3.67,5]    (3.67,5]   
Levels: [1,2.33] (2.33,3.67] (3.67,5]

Here's a better example than what you provided: 这是比您提供的更好的示例:

> v <-1:10
> X <- cut(v, breaks=quantile(v,probs=c(0:3)/3), labels=letters[1:3], include.lowest=TRUE)
> X
 [1] a a a a b b b c c c
Levels: a b c

Two select values from v which correspond to level "a" , just run: v中选择两个与"a"级相对应的选择值,即可运行:

> v[X=="a"]
[1] 1 2 3 4 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R按因子水平细分 - R Subset by Factor level 几乎没有观察值的下降因子水平 - Drop factor level with few observations 我如何在没有循环的情况下通过数据帧中该级别中另一个因子的子集来操作因子级别内的数据 - How can i manipulate data within a factor level by a subset of another factor in that level in a dataframe without loops 用因子包装数据帧的子集,并在子集的任一侧进行最近的观察 - Wrapping a subset of a data frame with nearest observations on either side of subset by factor 按 R 中的因子水平对不同数量的观察值进行采样 - Sample different number of observations by level of a factor in R 根据另一个因素的水平改变一个因素的水平 - Change the level of a factor based on the level of another factor 基于要素水平的估算 - Imputation based on factor level 如何根据另一个数据框提取数据子集并在该子集之前和之后获取观察结果 - How can I extract a subset of data based on another data frame and grab observations before and after that subset 如何使用 forcats 根据另一个变量的子集(方面)对因子重新排序? - How to reorder a factor based on a subset (facets) of another variable, using forcats? 如何基于因子变量的子集创建归一化等级列? - How to create a normalised rank column based on subset of factor variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM