参考 Schouwenaars (2016)
stringsAsFactors = FALSE
Import strings as categorical variables?
# Import hotdogs.txt: hotdogs
hotdogs <- read.delim("datasets/hotdogs.txt",sep = '\t',header = F)
# Summarize hotdogs
summary(hotdogs)
## V1 V2 V3
## Beef :20 Min. : 86.0 Min. :144.0
## Meat :17 1st Qu.:132.0 1st Qu.:362.5
## Poultry:17 Median :145.0 Median :405.0
## Mean :145.4 Mean :424.8
## 3rd Qu.:172.8 3rd Qu.:503.5
## Max. :195.0 Max. :645.0
注意 sep = '\t'
library(xfun)
##
## Attaching package: 'xfun'
## The following objects are masked from 'package:base':
##
## attr, isFALSE
library(magrittr)
read_utf8("datasets/hotdogs.txt") %>% head
## [1] "Beef\t186\t495" "Beef\t181\t477" "Beef\t176\t425" "Beef\t149\t322"
## [5] "Beef\t184\t482" "Beef\t190\t587"
\t
就是分割符号。
可以指定对应的 column name
# Finish the read.delim() call
hotdogs <- read.delim("datasets/hotdogs.txt", header = F, col.names = c("type", "calories", 'sodium'))
# Select the hot dog with the least calories: lily
lily <- hotdogs[which.min(hotdogs$calories), ]
# Select the observation with the most sodium: tom
tom <- hotdogs[which.max(hotdogs$sodium), ]
# Print lily and tom
lily
## type calories sodium
## 50 Poultry 86 358
tom
## type calories sodium
## 15 Beef 190 645
which.max
和which.min
,反馈 index,类似于
(\arg\max_{\text{index}}\text{variable})。