R语言【dplyr】——使用 relocate() 更改列的位置,其语法与 select() 相同,便于一次移动多个列块

Package dplyr version 1.1.4


Parameters

R 复制代码
relocate(.data, ..., .before = NULL, .after = NULL)

参数【.data】:数据集、数据集扩展(如 tibble)或 lazy data frames(如来自 dbplyrdtplyr 的数据集)。

参数【...】:<tidy-select> 要移动的列。

参数【.before,.after】:<tidy-select> 通过参数【...】选择的列的目的地。两者都不提供会将列移动到左侧;同时指定两者则会出错。


Value

与参数【.data】类型相同的对象。输出具有以下属性:

  • 行不会受到影响。
  • 输出中会出现相同的列,但(通常)位置不同,可能还会重命名。
  • 数据集属性保留不变。
  • 分组不受影响。

Examples

1. relocate() 默认将列放到首列

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(f)

# A tibble: 1 × 6
  f         a     b     c d     e    
  <chr> <dbl> <dbl> <dbl> <chr> <chr>
1 a         1     1     1 a     a 

2. relocate() 的参数【.after】将列放到该列后面

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(a, .after = c)

# A tibble: 1 × 6
      b     c     a d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a  


df %>% relocate(a, .after = last_col())

# A tibble: 1 × 6
      b     c d     e     f         a
  <dbl> <dbl> <chr> <chr> <chr> <dbl>
1     1     1 a     a     a         1

3. relocate() 的参数【.before】将列放到该列前面

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(f, .before = b)

# A tibble: 1 × 6
      a f         b     c d     e    
  <dbl> <chr> <dbl> <dbl> <chr> <chr>
1     1 a         1     1 a     a 

4. relocate() 在改变列的位置的同时,还可以重命名列名

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(ff = f)

# A tibble: 1 × 6
  ff        a     b     c d     e    
  <chr> <dbl> <dbl> <dbl> <chr> <chr>
1 a         1     1     1 a     a   

5. relocate() 通过变量的类型选择列

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(where(is.character))

# A tibble: 1 × 6
  d     e     f         a     b     c
  <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 a     a     a         1     1     1


df %>% relocate(where(is.numeric), .after = last_col())

# A tibble: 1 × 6
  d     e     f         a     b     c
  <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 a     a     a         1     1     1

6. relocate() 通过其他的选择方法挑选列

R 复制代码
df <- tibble(a = 1, b = 1, c = 1, d = "a", e = "a", f = "a")
df

# A tibble: 1 × 6
      a     b     c d     e     f    
  <dbl> <dbl> <dbl> <chr> <chr> <chr>
1     1     1     1 a     a     a


df %>% relocate(any_of(c("a", "e", "i", "o", "u")))

# A tibble: 1 × 6
      a e         b     c d     f    
  <dbl> <chr> <dbl> <dbl> <chr> <chr>
1     1 a         1     1 a     a  

7. 在调动多列的同时使用了参数【.after,.before】,列会立即排在选择列的前/后

R 复制代码
df2 <- tibble(a = 1, b = "a", c = 1, d = "a")
df2

# A tibble: 1 × 4
      a b         c d    
  <dbl> <chr> <dbl> <chr>
1     1 a         1 a   


df2 %>% relocate(where(is.numeric), .after = where(is.character))

# A tibble: 1 × 4
  b     d         a     c
  <chr> <chr> <dbl> <dbl>
1 a     a         1     1


df2 %>% relocate(where(is.numeric), .before = where(is.character))

# A tibble: 1 × 4
      a     c b     d    
  <dbl> <dbl> <chr> <chr>
1     1     1 a     a   
相关推荐
czhc11400756633 天前
LINUX913 shell:set ip [lindex $argv 0],\r,send_user,spawn ssh root@ip “cat “
tcp/ip·r语言·ssh
zhangfeng11334 天前
win7 R 4.4.0和RStudio1.25的版本兼容性以及系统区域设置有关 导致Plots绘图面板被禁用,但是单独页面显示
开发语言·人工智能·r语言·生物信息
zhangfeng11334 天前
在 R 语言里,`$` 只有一个作用 按名字提取“列表型”对象里的单个元素 对象 $ 名字
开发语言·windows·r语言
高-老师4 天前
R语言生物群落(生态)数据统计分析与绘图实践技术应用
开发语言·r语言·生物群落
WangYan20225 天前
R语言:数据读取与重构、试验设计(RCB/BIB/正交/析因)、ggplot2高级绘图与统计检验(t检验/方差分析/PCA/聚类)
r语言·ggplot2·dplyr
zhangfeng11335 天前
错误于make.names(vnames, unique = TRUE): invalid multibyte string 9 使用 R 语言进行数据处理时
开发语言·r语言·生物信息
zhangfeng11335 天前
R geo 然后读取数据的时候 make.names(vnames, unique = TRUE): invalid multibyte string 9
开发语言·chrome·r语言·生物信息
梦想的初衷~6 天前
R语言生物群落数据分析全流程:从数据清洗到混合模型与结构方程
机器学习·r语言·生态·环境
没有梦想的咸鱼185-1037-16638 天前
基于R语言机器学习方法在生态经济学领域中的实践技术应用
开发语言·机器学习·数据分析·r语言
zhangfeng11338 天前
R 语法高亮为什么没有,是需要安装专用的编辑软件,R语言自带的R-gui 功能还是比较简单
开发语言·r语言