jupyter notebook 安装 R

conda install -c r r-essentials

基本操作

# 下载包
install.packages("lubridate", dependencies=TRUE, repos='http://cran.rstudio.com/')

# Get and print current working directory.
print(getwd())

# 设置工作目录
setwd("F:/Projects/time_series_forecast/ARIMA")

# 退出命令行
q()

# 查看变量类型
class(var)

# 变量打印
print(var)

# 变量长度
nrow(var)

# 赋值
myString <- "Hello, World!"
print (myString)

# The $ allows you extract elements by name from a named list
x <- list(a=1, b=2, c=3)
x$b
# [1] 2

#You can find the names of a list using names()
names(x)
# [1] "a" "b" "c"

# 删除变量
rm(var.3)

# 绘图
plot(data)

# 查看头部数据
head(data)

# Create a vector.
apple <- c('red','green',"yellow")

# Create a list.
list1 <- list(c(2,5,3),21.3,sin)

# 函数定义

function_name <- function(arg_1, arg_2, ...) {
   Function body 
}

# 循环
for (value in vector) {
   statements
}

# 判断
x <- 30L
if(is.integer(x)) {
   print("X is an Integer")
}

数据分析

# 读取csv文件
data2<-read.table('item.csv',header=TRUE,fileEncoding='utf-8')

# 查询文件行数和列数
print(is.data.frame(data))
print(ncol(data))
print(nrow(data))

# Get the max salary from data frame.
sal <- max(data$salary)
print(sal)

# Get the person detail having max salary.
retval <- subset(data, salary == max(salary))
print(retval)

# 获取薪水大于600且是IT部分的员工信息
info <- subset(data, salary > 600 & dept == "IT")
print(info)

# 写入csv文件
data <- read.csv("input.csv")
retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
write.csv(retval,"output.csv")

类型转换

# to int
as.integer(x)

# Use is.foo to test for data type foo. Returns TRUE or FALSE
is.numeric(a)
# Use as.foo to explicitly convert it.
a <- as.character(a)

时间序列

# frequency:取值包括1,4,12,分别代表年度,季度,月度数据
ts(data = NA, start = , end = , frequency = , ...)