在特定组内排列值 [英] Arrange values within a specific group

查看:35
本文介绍了在特定组内排列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在嵌套数据框中的精确组内按降序排列值.我的输入数据看起来像这样.我有两个分组变量(group1group2)和三个值(即 idvalue2value3).

图书馆(tidyverse)设置种子(1234)df <- tibble(group1 = c(rep(LETTERS[1:3], 4)),group2 = c(rep(0, 6), rep(2, 6)),value2 = norm(12, 20, sd = 10),值3 = norm(12, 20, sd = 50)) %>%group_by(group1)%>%变异(id = c(1:4)) %>%取消分组()

我决定按 group1group2 然后按 nest() 对它们进行分组:

df_nested <- df %>%group_by(group1, group2)%>%巢()# 小费:6 x 3# 组: group1, group2 [6]group1 group2 数据<chr><dbl><列表>1 A 0 <tibble [2 x 3]>2 B 0 <tibble [2 x 3]>3 C 0 <tibble [2 x 3]>4 A 2 <tibble [2 x 3]>5 B 2 <tibble [2 x 3]>6 C 2 <tibble [2 x 3]>

完美.现在我只需要按 idgroup2 等于 2 的那些 data 进行排序.但是我收到以下错误:

df_nested %>%变异(数据 = map2_df(.x = 数据,.y = group2,~ifelse(.y == 2, 排列(-.x$id),.X)))

<块引用>

错误:参数 1 必须有名称

解决方案

你可以这样做:

库(dplyr)图书馆(咕噜咕噜)df_nested$data <- map2(df_nested$data, df_nested$group2,~if(.y == 2)安排(.x, -.x$id) 否则 .x)

所以group2不等于2的数据不排序

df_nested$data[[1]]# 小费:2 x 3# value2 value3 id# <dbl><dbl><int>#1 13.1 -89.0 1#2 9.76 -3.29 2

group2 是 2 的地方被排序.

df_nested$data[[4]]# 小费:2 x 3#value2 value3 id# <dbl><dbl><int>#1 15.0 -28.4 4#2 31.0 -22.8 3

如果你想把它们结合起来:

map2_df(df_nested$data, df_nested$group2,~if(.y == 2)arrange(.x, -.x$id) else .x)

I'm trying to arrange values in decreasing order within a exact group in a nested dataframe. My input data looks like this. I've got two grouping variables (group1 and group2) and three values (i.e. id, value2, value3).

library(tidyverse)
set.seed(1234)    

df <- tibble(group1 = c(rep(LETTERS[1:3], 4)),
             group2 = c(rep(0, 6), rep(2, 6)),
             value2 = rnorm(12, 20, sd = 10),
             value3 = rnorm(12, 20, sd = 50)) %>%  
  group_by(group1) %>% 
  mutate(id = c(1:4)) %>% 
  ungroup()

I decided to group them by group1 and group2 and then nest():

df_nested <- df %>% 
  group_by(group1, group2) %>% 
  nest()

# A tibble: 6 x 3
# Groups:   group1, group2 [6]
  group1 group2 data            
  <chr>   <dbl> <list>          
1 A           0 <tibble [2 x 3]>
2 B           0 <tibble [2 x 3]>
3 C           0 <tibble [2 x 3]>
4 A           2 <tibble [2 x 3]>
5 B           2 <tibble [2 x 3]>
6 C           2 <tibble [2 x 3]>

Perfect. Now I need to sort only those data which group2 is equal to 2 by id. However I'm receiving a following error:

df_nested %>% 
  mutate(data = map2_df(.x = data, .y = group2,
                     ~ifelse(.y == 2, arrange(-.x$id),
                             .x))) 

Error: Argument 1 must have names

解决方案

You could do :

library(dplyr)
library(purrr)

df_nested$data <- map2(df_nested$data, df_nested$group2,~if(.y == 2) 
                       arrange(.x, -.x$id) else .x)

So data where group2 is not equal to 2 is not sorted

df_nested$data[[1]]
# A tibble: 2 x 3
#  value2 value3    id
#   <dbl>  <dbl> <int>
#1  13.1  -89.0      1
#2   9.76  -3.29     2

and where group2 is 2 is sorted.

df_nested$data[[4]]
# A tibble: 2 x 3
#value2 value3    id
# <dbl>  <dbl> <int>
#1   15.0  -28.4   4
#2   31.0  -22.8   3

If you want to combine them do :

map2_df(df_nested$data, df_nested$group2,~if(.y == 2) arrange(.x, -.x$id) else .x)

这篇关于在特定组内排列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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