Skip to Content
R 錦囊妙計
book

R 錦囊妙計

by Paul Teetor
January 2014
Beginner to intermediate
488 pages
8h 1m
Chinese
GoTop Information, Inc.
Content preview from R 錦囊妙計
172
|
第六章
在本書中,我將這些因子視為
分組因子
grouping factors
)。藉由將資料分組,因子有
效地將資料切割成小塊。此外,分組資料在統計學中,常用來比較組平均、比較組比
例,與進行變異數分析等。
本章涵蓋之錦囊內容,部分包括使用分組因子將向量元素分配至各組別(錦囊 6.1),
函數應用於向量中的各組(錦囊 6.5),應用函數至資料框架中的列資料組(錦囊 6.6)。
而在其他章節中,分組資料也應用於檢驗組平均值(錦囊 9.19),執行一因子變異數分
析(錦囊 11.20),以及依據群組繪製資料點(錦囊 10.4)等。
6.1 將向量分組
問題點
您有一個向量,其中每個向量元素隸屬於不同組別;而且,組別是依據分組因子界定。
您想要依據分組因子將向量元素分組。
解決方案
假設向量為
x
,因子為
f
。您可以使用
split
函數進行分組如下:
> groups <- split(x, f)
或者,使用
unstack
函數:
> groups <- unstack(data.frame(x,f))
以上兩個函數皆回傳一個向量列表,其中,每個向量都包含已分組的元素。
此外,
unstack
函數有更進階的功能:若所有的向量具有相同長度,則可將列表轉換為
資料框架。
討論說明
名稱為
Cars93
的資料集包含一個名為
Origin
的因子變數,此因子有兩個組別:
USA
non-USA
;也包含名為
MPG.city
的變數。我們可依據
Origin
MPG.city
資料進行分組,
如下所示:
> library(MASS)
> split(Cars93$MPG.city, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

R 语言经典实例(原书第 2 版)

R 语言经典实例(原书第 2 版)

J.D. Long, Paul Teetor
優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769829