簡體   English   中英

在分組數據中,如何按列的值排列僅*某些*列的行,同時保持其余列不變?

[英]In grouped data, how to arrange the rows of only *some* columns by the values of a column, while keeping the rest of columns untouched?

我有長格式的數據,按一個變量分組。 我正在嘗試按特定列中的值順序按組對行進行排序/排列。 問題是,我想對某些列中的行應用這種排序,而其他列應保持不變。 我嘗試使用mutate(across(...))對感興趣的列進行操作,但出現錯誤。

示例數據

set.seed(2020)

df <-
  data.frame(name = rep(c("john", "bob", "ralph"), each = 8),
           test_scores = sample(30:100, size = 24),
           year_taken = sample(1993:2020, size = 24),
           dont_touch_this_col = LETTERS[1:24])

> df
##     name test_scores year_taken dont_touch_this_col
## 1   john          74       2002                   A
## 2   john          72       1993                   B
## 3   john          98       2007                   C
## 4   john          87       2014                   D
## 5   john          95       2001                   E
## 6   john          54       2008                   F
## 7   john          64       1998                   G
## 8   john          53       2020                   H
## 9    bob          79       2019                   I
## 10   bob          62       2012                   J
## 11   bob          83       2009                   K
## 12   bob          36       2000                   L
## 13   bob          37       2018                   M
## 14   bob          50       2004                   N
## 15   bob          85       2013                   O
## 16   bob          42       1994                   P
## 17 ralph          63       1997                   Q
## 18 ralph          34       2010                   R
## 19 ralph          33       1996                   S
## 20 ralph          48       2006                   T
## 21 ralph          77       2016                   U
## 22 ralph          52       2017                   V
## 23 ralph          82       2015                   W
## 24 ralph          47       2003                   X

name分組year_taken排列很容易

library(dplyr)

df %>%
  group_by(name) %>%
  arrange(year_taken, .by_group = TRUE)


## # A tibble: 24 x 4
## # Groups:   name [3]
##    name  test_scores year_taken dont_touch_this_col
##    <chr>       <int>      <int> <chr>              
##  1 bob            42       1994 P                  
##  2 bob            36       2000 L                  
##  3 bob            50       2004 N                  
##  4 bob            83       2009 K                  
##  5 bob            62       2012 J                  
##  6 bob            85       2013 O                  
##  7 bob            37       2018 M                  
##  8 bob            79       2019 I                  
##  9 john           72       1993 B                  
## 10 john           64       1998 G  

但我想在保持dont_touch_this_col不變的情況下進行分組安排

一種不成功的嘗試是使用mutate(across())來指定(或排除)特定變量:

df %>%
  group_by(name) %>%
  mutate(across(-dont_touch_this_col, arrange, year_taken, .by_group = TRUE))

錯誤: mutate()輸入問題..1 x 沒有適用於 'arrange_' 的方法應用於類 "c('integer', 'numeric')" i 輸入..1 across(-dont_touch_this_col, arrange, year_taken, .by_group = TRUE) i 組 1 中發生錯誤:name = "bob"。

那么,如何按感興趣的列(這里是year_taken )排列分組數據,同時將一列(或多列)排除在操作之外?

也許試試這個。 您可以使用bind_cols()添加您想要保持不變的變量並將它們從排序任務中排除。 這里的代碼:

library(dplyr)
set.seed(2020)
#Data
df <-
  data.frame(name = rep(c("john", "bob", "ralph"), each = 8),
             test_scores = sample(30:100, size = 24),
             year_taken = sample(1993:2020, size = 24),
             dont_touch_this_col = LETTERS[1:24])
#Code
newdf <- df %>% select(-dont_touch_this_col) %>%
  group_by(name) %>%
  arrange(year_taken, .by_group = TRUE) %>%
  bind_cols(df %>% select(dont_touch_this_col))

輸出:

# A tibble: 24 x 4
# Groups:   name [3]
   name  test_scores year_taken dont_touch_this_col
   <fct>       <int>      <int> <fct>              
 1 bob            83       1998 A                  
 2 bob            79       2002 B                  
 3 bob            58       2003 C                  
 4 bob            63       2005 D                  
 5 bob            85       2013 E                  
 6 bob            45       2018 F                  
 7 bob            37       2019 G                  
 8 bob            95       2020 H                  
 9 john           65       1999 I                  
10 john           57       2004 J                  
# ... with 14 more rows

使用base R

cbind(df[with(df, order(name, year_taken)), 
     c('name', 'test_scores', 'year_taken')], df['dont_touch_this_col'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM