當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。