简体   繁体   English

R 中的重新排序因子基于对应于原始因子的“子因子”的特定级别的值

[英]Reorder factor in R based on values corresponding to a specific level of a "subfactor" of the original factor

I have a dataset in R with the following structure:我在 R 中有一个具有以下结构的数据集:

sub ses value价值_

sub1 0.1454 sub1 0.1454

sub1 1.0857 sub1 1.0857

sub2 0.1123子2 0.1123

sub2 1.2345 sub2 1.2345

sub3 0.2105 sub3 0.2105

sub3 1.0665 sub3 1.0665

df <- data.frame(sub=c("sub1","sub1","sub2","sub2","sub3","sub3"),ses=c(0,1,0,1,0,1),value=c(.1454,.0857,.1123,.2345,.2105,.0665))

For the purposes of plotting the change in value between session 0 and 1, I'm interested in reordering the dataset so that the order of Sub reflects the variable Value only for Ses == 0 in descending order, so you can easily visualize differences in "Baseline" (Ses == 0) values across subjects, while keeping both of each subject's values together in the data frame:为了绘制 session 0 和 1 之间的值变化,我有兴趣对数据集重新排序,以便 Sub 的顺序以降序反映适用于 Ses == 0 的变量值,因此您可以轻松地将差异可视化跨对象的“基线”(Ses == 0)值,同时将每个对象的两个值一起保存在数据框中:

sub ses value价值_

sub2 0.1123子2 0.1123

sub2 1.2345 sub2 1.2345

sub1 0.1454 sub1 0.1454

sub1 1.0857 sub1 1.0857

sub3 0.2105 sub3 0.2105

sub3 1.0665 sub3 1.0665

df <- data.frame(sub=c("sub2","sub2","sub1","sub1","sub3","sub3"),ses=c(0,1,0,1,0,1),value=c(.1123,.2345,.1454,.0857,.2105,.0665))

This has turned out to be a nontrivial solution for a relatively large dataset.对于相对较大的数据集,这已证明是一个非常重要的解决方案。 I want something akin to我想要类似的东西

df.ordered <- df %>% group_by(sub) %>% arrange(desc(ses == 0, .by_group=TRUE))

but this syntax is incorrect, as you cannot specify a value of ses this way (it will just reorder by ses in this case).但此语法不正确,因为您不能以这种方式指定 ses 的值(在这种情况下它只会按 ses 重新排序)。 Much appreciation for anyone who can give me guidance on how to implement this.非常感谢任何能给我指导如何实施的人。 Thanks!谢谢!

We can use arrange on a factor with levels specified according to the custom values we want it to order我们可以使用arrange on 一个factor ,其levels根据我们希望它排序的自定义值指定

library(dplyr)
df %>%
   dplyr::arrange(factor(sub, levels = 
               unique(sub[ses ==0][order(value[ses == 0])])))
#  sub ses  value
#1 sub2   0 0.1123
#2 sub2   1 0.2345
#3 sub1   0 0.1454
#4 sub1   1 0.0857
#5 sub3   0 0.2105
#6 sub3   1 0.0665

You can first extract the correct order in which you want to arrange the data by filtering only the 0 values and arranging them by value您可以首先通过仅过滤 0 值并按value排列它们来提取要arrange数据的正确顺序

library(dplyr)
correct_order <- df %>%
                  filter(ses == 0) %>%
                  arrange(value) %>%
                  pull(sub)
correct_order
#[1] "sub2" "sub1" "sub3"

You can then arrange them using match :然后您可以使用match arrange它们:

df %>% arrange(match(sub, correct_order), ses)
#  sub ses  value
#1 sub2   0 0.1123
#2 sub2   1 0.2345
#3 sub1   0 0.1454
#4 sub1   1 0.0857
#5 sub3   0 0.2105
#6 sub3   1 0.0665

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM