Skip to Content
R 语言经典实例(原书第 2 版)
book

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

by J.D. Long, Paul Teetor
June 2020
Beginner to intermediate
522 pages
9h 6m
Chinese
China Machine Press
Content preview from R 语言经典实例(原书第 2 版)
136
5
现在 R 知道 f 是一个有五个可能水平的因子。它也知道它们的正确顺序。R 软件最初
在星期二之前放置星期四,因为它默认采用字母顺序。levels 参数明确定义了正确的
顺序。
在许多情况下,没有必要显式地调用函数 factor。当 R 函数需要因子时,它通常会自
动将数据转换为因子。例如,table 函数仅适用于因子变量,因此它会自动地将输入转
换为因子。如果要指定全部水平取值或当需要控制水平的顺序时,必须显式地创建因子
变量。
5.4.4 另请参阅
有关从连续数据创建一个因子,请参阅 12.5 节。
5.5 将多个向量合并成单个向量以及一个平行因子
5.5.1 问题
有几组数据,每组包含一个向量。需要将这些向量合并成一个长向量,并且同时创建一
个平行因子来识别每个值的原始组。
5.5.2 解决方案
创建一个列表来包含这些向量。运用 stack 函数将列表合并成一个两列数据框:
comb <- stack(list(v1 = v1, v2 = v2, v3 = v3)) # Combine 3 vectors
该数据框的两个列称为 values ind。第一列包含数据,第二列包含它的平行因子。
5.5.3 讨论
为何将所有数据混合成一个长向量和一个平行因子?原因是许多重要的统计函数需要该
格式的数据。
假设你对大一新生、大二学生和大三学生的自信心水平进行调查(比如“你在学校里
感到自信的时间百分比是多少?”)。现在有三个向量,分别是 freshmen(大一新生)、
sophomores(大二学生 ...
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

大规模数据分析和建模:基于 Spark 与 R

大规模数据分析和建模:基于 Spark 与 R

Javier Luraschi, Kevin Kuo, Edgar Ruiz
解密金融数据

解密金融数据

Justin Pauley
数据压缩入门

数据压缩入门

Colt McAnlis, Aleks Haecky

Publisher Resources

ISBN: 9787111656814