summarise()
R语言中的函数用于将 DataFrame 列的各种值压缩为一个值。
用法: summarise(x, expr)
参数:
x:数据帧
expr:压缩数据的操作
范例1:
# R program to condense data
# of a data frame
# Loading library
library(dplyr)
# Create a data frame
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16),
ht = c(46, NA, NA, 69),
school = c("yes", "yes", "no", "no") )
# Calculating min age
summarise(d, min_age = min(age))
# Calculating max age
summarise(d, max_age = max(age))
输出:
min_age 1 5 max_age 1 16
范例2:
# R program to condense data
# of a data frame
# Loading library
library(dplyr)
# Create a data frame
d <- data.frame( name = c("Abhi", "Bhavesh", "Chaman", "Dimri"),
age = c(7, 5, 9, 16),
ht = c(46, NA, NA, 69),
school = c("yes", "yes", "no", "no") )
# Calculating mean of age
summarise(d, mean = mean(age))
# Calculating median of age
summarise(d, med = median(age))
输出:
mean 1 9.25 med 1 8
相关用法
- R语言 data.matrix()用法及代码示例
- R语言 with()用法及代码示例
- R语言 transform()用法及代码示例
- R语言 expand.grid()用法及代码示例
- R语言 subset()用法及代码示例
- R语言 melt()用法及代码示例
- R语言 rename()用法及代码示例
- R语言 select()用法及代码示例
- R语言 mutate()用法及代码示例
- R语言 sample_n()用法及代码示例
- R语言 all_equal()用法及代码示例
- R语言 set.seed()用法及代码示例
- R语言 sweep()用法及代码示例
注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Condense Column Values of a Data Frame in R Programming – summarise() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。