select_rename_relocate
qliu
2020/6/4
加載包
library(tidyverse, warn.conflicts = F)
select() 和 rename()
? 按照位置:
– df %>% select(1, 5, 10)
– df %>% select(1:4)
? 按照名字:
– df %>% select(a, e, j)
– df %>% select(c(a, e, j))
– df %>% select(a:d)
? 按照函數(shù)選擇:
– df %>% select(starts_with("x"))
– df %>% select(ends_with("s"))
– 也可以通過與 contains() 和 matches() 函數(shù)連用
? 按照數(shù)據(jù)類型:
– df %>% select(where(is.numeric))
– df %>% select(where(is.factor))
? 通過布爾操作符 &|! 進(jìn)行多個組合
– df %>% select(!where(is.factor))
– df %>% select(where(is.numeric) & starts_with("x"))
– df %>% select(starts_with("a") | ends_with("z"))
rename()
? 直接修改:
– df1 %>% rename(b = 2);b 表示修改后的列名,2 表示第二列
? 按照函數(shù):
– df2 %>% rename_with(toupper)
– df2 %>% rename_with(toupper, starts_with("x"))
– df2 %>% rename_with(toupper, where(is.numeric))
relocate()
? df3 %>% relocate(y, z);將 yz 列移到最前面
? df3 %>% relocate(where(is.character));將字符串類型列都放到最前面
? df3 %>% relocate(w, .after = y);將 w 列移動到 y 列后面
? df3 %>% relocate(w, .before = y);將 w 列移動到 y 列前面
? df3 %>% relocate(w, .after = last_col());將 w 列移至最后面