R基础知识
来源:互联网 发布:手机淘宝 降价提醒 编辑:程序博客网 时间:2024/05/18 03:04
Python and R
数据科学家爱讨论Python和R谁是更好的数据分析语言。
- R:更适合专业学者,因为和Matlab很像。
- Python:更适合公司。
但是鉴于R语言有强大的可视化和统计功能,可以增强数据科学工作流以及帮助我们更有效的探索数据。
赋值(<-)
- R语言的赋值语句是起源于APL语言的
- 赋值是<-(x <- 3),而只有给函数的参数赋值时才用=(f(x = 3))
- 鉴于大家都习惯用=,2001年开始也可以用=进行赋值
- 但是鉴于代码共享,更多R语言工作习惯<-,以后我也用<-,尽管复杂点,并且Google Style Guide也推荐这个。
dogAwesomeness <- 10catAwesomeness <- 9.5
打印(print or cat)
lifeSavings <- 9999print(lifeSavings)
- output:
[1]9999
- cat函数是另外一种可以替代print的显示方式。它可以将多个对象连接并以连续的方式显示:
> cat("The zero occurs at", 2*pi, "radians.", "\n")
- output:
The zero occurs at 6.283185 radians.
变量类型(class)
R语言中常见的两个变量是character(text), and numeric(number)。
- 使用class(var)函数可以获取var的类型。
# Assign the value 800 to the variable runDistance.runDistance <- 800# This is of type "numeric".print(class(runDistance))# Assign the value "Peanut Butter Cup" to favoriteDessertfavoriteDessert <- "Peanut Butter Cup"# This is of type "character", because it contains text.print(class(favoriteDessert))
- output:
[1] “numeric”
[1] “character”
向量(Vectors)
1) c()函数来创建向量
# Store a vector of Russian presidents.russianPresidents <- c("Mikhail Gorbachev", "Boris Yeltsin", "Vladimir Putin")# Store a vector of stock prices on consecutive days.applePrices <- c(113, 114, 115)
- 向量存储的数据的类型必须一致。当你将一个字符串和数字传给一个向量,最终数字会转换会字符类型。
mixedVector <- c("Fifteen", 15, 0)# Everything in mixedVector is a character value.print(mixedVector)# mixedVector is of type character.print(class(mixedVector))
- output :
[1] “Fifteen” “15” “0”
[1] “character”
2) identical(var1,var2)判断两个变量是否相等
R几乎将所有的数据都存储为向量形式,当我们创建单个变量a <-
1,R会将他存储为只含一个元素的向量(自动调用c(1))。这样使得所有变量都一样,方便处理。
dogCount <- 1catCount <- c(1)# We can see that these two assignments are identical. dogCount is a vector, as is catCount.print(identical(dogCount, catCount))
- output:
[1] TRUE
3) 向量索引
- R向量的索引是从1开始的。
# Print the first element in salaries.print(salaries[1])# Print the 50th element in salariesprint(salaries[50])
- output
[1] 105960
[1] 50500
向量长度(length)
# Initialize the runDistances vectorrunDistances <- c(20, 10.5, 30)# Print the length of the vector.print(length(runDistances))
- output:
[1] 3
加减乘除(+ - * /)
# Create a vector of stock prices.stockPrices <- c(10, 9, 11, 15)# This results in a new vector. See how every element has had 2 added to it.# Every time you do math on a vector, it will change all the elements of the vector.print(stockPrices + 2)# But stockPrices is unaffected.print(stockPrices)
output:
- [1] 12 11 13 17
- [1] 10 9 11 15
求助(help)
# Enter your code here.help(class)
0 0
- R基础知识
- R(一): R基础知识
- R的基础知识
- R 语言基础知识
- R语言基础知识
- 【R语言】 之 基础知识初探
- R语言:R语言基础知识及操作整理
- Android 基础知识 - R.id.x 的由来
- 关于R的一些基础知识整理
- R语言学习之基础知识一
- R语言学习之基础知识二
- 电路中逻辑门的基础知识r
- R语言与网站分析——网站统计分析基础知识
- r
- r
- R
- r
- r
- JS总结
- 100个直接可以拿来用的JavaScript实用功能代码片段
- 概率论与数量统计(二)3___ 第一章__概率,频率定义
- android开发--android-async-http
- 求最长公共序列长度-递归
- R基础知识
- 码农小汪-Hibernate学习9-hibernate双向关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable
- hdu2059龟兔赛跑
- 普通树转化成二叉树
- ES6-let带来了更简单的块级作用域
- 【01背包】Bone Collector
- 希尔排序
- Step by Step into Spring (事务)
- 【BZOJ1030】【Tyvj1806】文本生成器,AC自动机+DP