关于r:dplyr

96次阅读

共计 6601 个字符,预计需要花费 17 分钟才能阅读完成。

arrange(.data, ..., .by_group = FALSE)
msleep%>%group_by(vore)%>%arrange(sleep_total)%>%write_csv("C:/Users/panda/Desktop/R-result/arrange0.csv")
msleep%>%group_by(vore)%>%arrange(sleep_total,.by_group=TRUE)%>%write_csv("C:/Users/panda/Desktop/R-result/arrange1.csv")

第一次执行组存在但表中看不出

count(x, ..., wt = NULL, sort = FALSE, name = NULL)
tally(x, wt = NULL, sort = FALSE, name = NULL)
add_count(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = deprecated())
add_tally(x, wt = NULL, sort = FALSE, name = NULL)
msleep%>%count(vore,sort=TRUE,name='n')%>%write_csv("C:/Users/panda/Desktop/R-result/count0.csv")
msleep%>%count(vore,wt=sleep_total,sort=TRUE,name='n')%>%write_csv("C:/Users/panda/Desktop/R-result/count1.csv")
msleep%>%group_by(vore)%>%tally(sort=TRUE,name='n')%>%write_csv("C:/Users/panda/Desktop/R-result/tally.csv")
msleep%>%add_count(vore,sort=TRUE,name='n')%>%write_csv("C:/Users/panda/Desktop/R-result/add_count.csv")
msleep%>%add_tally(wt=sleep_total,sort=TRUE,name='n')%>%write_csv("C:/Users/panda/Desktop/R-result/add_tally.csv")

<font color=red> .drop = deprecated()</font>

count(列名)==group_by(列名)%>%tally()

add_保留原数据并增加新列 (新列反复项必定多)

wt 如果是数值型数据新列是 sum 的后果

distinct(.data, ..., .keep_all = FALSE)
msleep%>%distinct(vore,.keep_all=TRUE)%>%write_csv("C:/Users/panda/Desktop/R-result/distinct0.csv")
msleep%>%distinct(vore)%>%write_csv("C:/Users/panda/Desktop/R-result/distinct1.csv")

.keep_all = TRUE 保留所有列并反复时抉择第一行

filter(.data, ..., .preserve = FALSE)
msleep%>%group_by(vore)%>%filter(sleep_total>10)%>%write_csv("C:/Users/panda/Desktop/R-result/filter0.csv")

.preserve = FALSE 组的数量可能会缩小,当在某组没有满足条件的行

mutate(
  .data,
  ...,
  .keep = c("all", "used", "unused", "none"),
  .before = NULL,
  .after = NULL
)
transmute(.data, ...)
msleep%>%mutate(test=sleep_total*2,.keep='none')%>%write_csv("C:/Users/panda/Desktop/R-result/mutate.csv")
msleep%>%transmute(test=sleep_total*2)%>%write_csv("C:/Users/panda/Desktop/R-result/transmute.csv")

mutate(.data,.keep = “none”)==transmute(.data)

all 保留所有列,used 保留应用到的列,unused 保留没应用的列,none 只保留新列

pull(.data, var = -1, name = NULL, ...)
msleep%>%pull(name ='vore')

pull() 只有数据没列名,此段代码蕴含两列数据

正整数计数从右边,负整数右侧计数,默认值返回最初一个列

relocate(.data, ..., .before = NULL, .after = NULL)
msleep%>%relocate(vore,.before='name')%>%write_csv("C:/Users/panda/Desktop/R-result/relocate.csv")
rename(.data, ...)
rename_with(.data, .fn, .cols = everything(), ...)
msleep%>%rename(test=vore)%>%write_csv("C:/Users/panda/Desktop/R-result/rename.csv")
msleep%>%rename_with(toupper)%>%write_csv("C:/Users/panda/Desktop/R-result/rename_with.csv")

<font color=’red’>.cols = everything()</font>

select(.data, ...)
msleep%>%select(vore,sleep_total)%>%write_csv("C:/Users/panda/Desktop/R-result/select.csv")
summarise(.data, ..., .groups = NULL)
summarize(.data, ..., .groups = NULL)
msleep%>%group_by(vore)%>%summarize(mean(sleep_total))%>%write_csv("C:/Users/panda/Desktop/R-result/summarise.csv")

.groups 参数管制后果的分组构造,<font color=’red’>”drop_last” 去掉最初一个分组 </font>,”drop” 时去掉所有分组程度,此时没有组属性;”keep” 时放弃与.data 雷同的分组构造;“rowwise” 每一行自成一组

slice(.data, ..., .preserve = FALSE)
slice_head(.data, ..., n, prop)
slice_tail(.data, ..., n, prop)
slice_min(.data, order_by, ..., n, prop, with_ties = TRUE)
slice_max(.data, order_by, ..., n, prop, with_ties = TRUE)
slice_sample(.data, ..., n, prop, weight_by = NULL, replace = FALSE)
msleep%>%group_by(vore)%>%slice(1:5)%>%write_csv("C:/Users/panda/Desktop/R-result/slice.csv")
msleep%>%slice_head(prop=0.2)%>%write_csv("C:/Users/panda/Desktop/R-result/slice_head.csv")
msleep%>%slice_head(n=10)%>%write_csv("C:/Users/panda/Desktop/R-result/slice_tail.csv")
msleep%>%group_by(vore)%>%slice_min(sleep_total,n=5,with_ties = TRUE)%>%write_csv("C:/Users/panda/Desktop/R-result/slice_min.csv")
msleep%>%group_by(vore)%>%slice_max(sleep_total,n=5,with_ties = FALSE)%>%write_csv("C:/Users/panda/Desktop/R-result/slice_max.csv")
msleep%>%slice_sample(n=30,replace=TRUE)%>%write_csv("C:/Users/panda/Desktop/R-result/slice_sample.csv")

.preserve = TRUE 与上文作用雷同

n, prop 提供行数,或抉择行的比例

可能会返回比您申请的更多的行,with_ties=FALSE 返回第一行

weight_by 减少此列值大的抽中概率

bind_rows(..., .id = NULL)
bind_cols(
  ...,
  .name_repair = c("unique", "universal", "check_unique", "minimal")
)
one<-msleep%>%slice_head(n=10)
two<-msleep%>%slice_tail(n=10)
bind_rows(one,two,.id='id')%>%write_csv("C:/Users/panda/Desktop/R-result/bind_rows.csv")
bind_cols(one,two)%>%write_csv("C:/Users/panda/Desktop/R-result/bind_cols.csv")

.id 将减少一列,用来辨别此行数据来自哪个表

相当于 pandas 的 concat(), 横向纵向拼接

<font color=’red’>.name_repair =c(“unique”, “universal”, “check_unique”, “minimal”)</font>

检索和修复名称 -vec_as_names•vctrs (r-lib.org)

union_all(x, y, ...)
one<-msleep%>%slice_head(n=10)
two<-msleep%>%slice_head(n=10)
union_all(one,two)%>%write_csv("C:/Users/panda/Desktop/R-result/union_all.csv")

横向拼接,不删除反复值

inner_join(
  x,
  y,
  by = NULL,
  copy = FALSE,
  suffix = c(".x", ".y"),
  ...,
  keep = FALSE,
  na_matches = c("na", "never")
)
left_join(
  x,
  y,
  by = NULL,
  copy = FALSE,
  suffix = c(".x", ".y"),
  ...,
  keep = FALSE,
  na_matches = c("na", "never")
)
right_join(
  x,
  y,
  by = NULL,
  copy = FALSE,
  suffix = c(".x", ".y"),
  ...,
  keep = FALSE,
  na_matches = c("na", "never")
)
full_join(
  x,
  y,
  by = NULL,
  copy = FALSE,
  suffix = c(".x", ".y"),
  ...,
  keep = FALSE,
  na_matches = c("na", "never")
)
one<-msleep%>%slice_head(n=5)
two<-msleep%>%slice_head(n=8)
left_join(one,two,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/left_join.csv")
inner_join(one,two,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/inner_join.csv")
right_join(one,two,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/right_join.csv")
full_join(one,two,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/full_join.csv")

<font color=’red’>copy = FALSE,suffix = c(“.x”, “.y”), …, keep = FALSE,na_matches = c(“na”, “never”)</font>

nest_join(x, y, by = NULL, copy = FALSE, keep = FALSE, name = NULL, ...)
one<-msleep%>%slice_head(n=5)
two<-msleep%>%slice_head(n=8)
nest_join(one,two,by='name')

nest_join() 相似 left_join(),返回的模式不一样 two <list>

<font color=’red’> copy = FALSE, keep = FALSE, name = NULL</font>

                      Core Socialist Value
prosperity      democracy         civility       harmony
freedom         equality          justice        rule of law
patriotism      dedication        integrity      friendship
semi_join(x, y, by = NULL, copy = FALSE, ..., na_matches = c("na", "never"))
anti_join(x, y, by = NULL, copy = FALSE, ..., na_matches = c("na", "never"))
one<-msleep%>%slice_head(n=5)
two<-msleep%>%slice_head(n=8)
semi_join(two,one,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/semi_join.csv")
anti_join(two,one,by='name')%>%write_csv("C:/Users/panda/Desktop/R-result/anti_join.csv")

<font color=’red’>copy = FALSE, …, na_matches = c(“na”, “never”)</font>

group_by(.data, ..., .add = FALSE, .drop = group_by_drop_default(.data))

ungroup(x, ...)
msleep%>%group_by(vore)%>%ungroup()

<font color=’red’>.add = FALSE, .drop = group_by_drop_default(.data)</font>

group_cols(vars = NULL, data = NULL)
msleep%>%group_by(vore)%>%select(group_cols())%>%write_csv("C:/Users/panda/Desktop/R-result/group_cols.csv")

<font color=’red’>vars = NULL, data = NULL</font>

rowwise(data, ...)
msleep%>%rowwise(vore)

正文完
 0