首页 > 其他分享 >R语言观察日志(part26)--去除重复行

R语言观察日志(part26)--去除重复行

时间:2022-10-04 22:08:46浏览次数:47  
标签:15 -- frame grade part26 df 日志 data c1


学习笔记,仅供参考,有错必纠


准备

df <- data.frame(grade = c("A", "A", "A", "B", "B", "C"), 
a = c(1:6),
b = c(6:1),
c = c(10:15))
> df
grade a b c
1 A 1 6 10
2 A 2 5 11
3 A 3 4 12
4 B 4 3 13
5 B 5 2 14
6 C 6 1 15

distinct()

Select only unique/distinct rows from a data frame. This is similar to ​​unique.data.frame()​​ but considerably faster.

library(dplyr)
df.0 <- distinct(df, grade, .keep_all = T)
> df.0
grade a b c
1 A 1 6 10
2 B 4 3 13
3 C 6 1 15

aggregate()

Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form.

df.1 <- aggregate(df[,-1], list(df$grade), FUN=sum)
> df.1
Group.1 a b c
1 A 6 15 33
2 B 9 5 27
3 C 6 1 15


标签:15,--,frame,grade,part26,df,日志,data,c1
From: https://blog.51cto.com/u_15181342/5731768

相关文章