当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R语言 summarise()用法及代码示例


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



相关用法


注:本文由纯净天空筛选整理自nidhi_biet大神的英文原创作品 Condense Column Values of a Data Frame in R Programming – summarise() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。