library(xlsx)
## Loading required package: rJava
## Loading required package: xlsxjars
setwd("/Users/chenfeiyang")
cameraData <- read.xlsx("./data/cameras.xlsx", sheetIndex = 1, header = TRUE)
cameraData <- read.xlsx("./data/cameras.xlsx", "Baltimore Fixed Speed Cameras",
header = TRUE)
head(cameraData)
## address direction street crossStreet
## 1 S CATON AVE & BENSON AVE N/B Caton Ave Benson Ave
## 2 S CATON AVE & BENSON AVE S/B Caton Ave Benson Ave
## 3 WILKENS AVE & PINE HEIGHTS AVE E/B Wilkens Ave Pine Heights
## 4 THE ALAMEDA & E 33RD ST S/B The Alameda 33rd St
## 5 E 33RD ST & THE ALAMEDA E/B E 33rd The Alameda
## 6 ERDMAN AVE & N MACON ST E/B Erdman Macon St
## intersection Location.1
## 1 Caton Ave & Benson Ave (39.2693779962, -76.6688185297)
## 2 Caton Ave & Benson Ave (39.2693157898, -76.6689698176)
## 3 Wilkens Ave & Pine Heights (39.2720252302, -76.676960806)
## 4 The Alameda & 33rd St (39.3285013141, -76.5953545714)
## 5 E 33rd & The Alameda (39.3283410623, -76.5953594625)
## 6 Erdman & Macon St (39.3068045671, -76.5593167803)
# Read specific rows and columns in Excel
colIndex <- 2:3
rowIndex <- 1:4
cameraDataSubset <- read.xlsx("./data/cameras.xlsx", sheetIndex = 1, colIndex = colIndex,
rowIndex = rowIndex)
cameraDataSubset
## direction street
## 1 N/B Caton Ave
## 2 S/B Caton Ave
## 3 E/B Wilkens Ave
# Subsetting - quick review
set.seed(13435)
X <- data.frame(var1 = sample(1:5), var2 = sample(6:10), var3 = sample(11:15))
X <- X[sample(1:5), ]
X$var2[c(1, 3)] = NA
X
## var1 var2 var3
## 1 2 NA 15
## 4 1 10 11
## 2 3 NA 12
## 3 5 6 14
## 5 4 9 13
X[, 1]
## [1] 2 1 3 5 4
X[, "var1"]
## [1] 2 1 3 5 4
X[1:2, "var2"]
## [1] NA 10
# Logicals and: & , or: |
X[(X$var1 <= 3 & X$var3 > 11), ]
## var1 var2 var3
## 1 2 NA 15
## 2 3 NA 12
X[(X$var1 <= 3 | X$var3 > 15), ]
## var1 var2 var3
## 1 2 NA 15
## 4 1 10 11
## 2 3 NA 12
## Dealing with missing values
X[which(X$var2 > 8), ]
## var1 var2 var3
## 4 1 10 11
## 5 4 9 13
# Sorting
sort(X$var1)
## [1] 1 2 3 4 5
sort(X$var1, decreasing = TRUE)
## [1] 5 4 3 2 1
sort(X$var2, na.last = TRUE)
## [1] 6 9 10 NA NA
# Ordering
X[order(X$var1), ]
## var1 var2 var3
## 4 1 10 11
## 1 2 NA 15
## 2 3 NA 12
## 5 4 9 13
## 3 5 6 14
X[order(X$var1, X$var3), ]
## var1 var2 var3
## 4 1 10 11
## 1 2 NA 15
## 2 3 NA 12
## 5 4 9 13
## 3 5 6 14
## Sort using the arrange function of the plyr package
library(plyr)
arrange(X, var1)
## var1 var2 var3
## 1 1 10 11
## 2 2 NA 15
## 3 3 NA 12
## 4 4 9 13
## 5 5 6 14
arrange(X, desc(var1))
## var1 var2 var3
## 1 5 6 14
## 2 4 9 13
## 3 3 NA 12
## 4 2 NA 15
## 5 1 10 11
# Add row and column
X$var4 <- rnorm(5)
X
## var1 var2 var3 var4
## 1 2 NA 15 0.18760
## 4 1 10 11 1.78698
## 2 3 NA 12 0.49669
## 3 5 6 14 0.06318
## 5 4 9 13 -0.53613
Y <- cbind(X, rnorm(5))
Y
## var1 var2 var3 var4 rnorm(5)
## 1 2 NA 15 0.18760 0.62578
## 4 1 10 11 1.78698 -2.45084
## 2 3 NA 12 0.49669 0.08909
## 3 5 6 14 0.06318 0.47839
## 5 4 9 13 -0.53613 1.00053
没有合适的资源?快使用搜索试试~ 我知道了~
r语言-data-preprocessing.rar
共5个文件
r:5个
需积分: 1 0 下载量 174 浏览量
2024-09-18
08:38:09
上传
评论
收藏 3KB RAR 举报
温馨提示
r语言-data_preprocessing.rar
资源推荐
资源详情
资源评论
收起资源包目录
data_preprocessing.rar (5个子文件)
data_preprocessing
k_folds.r 743B
data_processing.r 3KB
data_normalization_standardization.r 2KB
dimensionality_reduction_algorithms.r 122B
lasso.r 731B
共 5 条
- 1
资源评论
蜡笔小流
- 粉丝: 2413
- 资源: 1270
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功