R中常用函数---第一讲

1. abs()

abs()R编程中的方法用于获取绝对值,即正值不变,负值转换为正值。
用法:
abs(value)
返回:返回绝对值。
范例1:

# R program to calculate absolute value
  
# Using abs() method
answer1 <- abs(-12)
answer2 <- abs(21)
answer3 <- abs(-2)
  
print(answer1)
print(answer2)
print(answer3)

输出:
[1] 12
[2] 21
[3] 2

范例2:

# R program to calculate absolute value
  
# Using abs() method
answer1 <- abs(c(1, 2, 3, 4))
answer2 <- abs(c(1, -2, 3, -4))
  
print(answer1)
print(answer2)

输出:
[1] 1 2 3 4
[2] 1 2 3 4

2. seq_len()

seq_len() 函数用于生成从 1 到指定数字的序列。
范例1:

# R program to illustrate
# seq_len function
  
# Calling the seq_len() function
seq_len(1)
seq_len(3)
seq_len(0)
seq_len(6)

输出:
[1] 1
[1] 1 2 3
integer(0)
[1] 1 2 3 4 5 6
范例2:

# R program to illustrate
# seq_len function
  
# Creating a vector of length 5
my_vector <- c(5, 6, 1, 13, 7)
  
# Calling seq_len() function
seq_len(length(my_vector))

输出:
[1] 1 2 3 4 5

3. 数据合并-cbind(),rbind

cbind(): 根据列进行合并,即叠加所有列,m列的矩阵与n列的矩阵cbind()最后变成m+n列
合并前提:cbind(a, c)中矩阵a、c的行数必需相符

rbind(): 根据行进行合并,就是行的叠加,m行的矩阵与n行的矩阵rbind()最后变成m+n行
合并前提:rbind(a, c)中矩阵a、c的列数必需相符

4. 取整函数

Floor(): Floor是R中的一个数学函数,它返回小于输入值的最大整数值,即输出值将不大于输入值。
Ceiling(): Ceiling函数也是R中的另一个数学函数,它将返回最接近输入值但大于输入值的值。

floor(5.9)
floor(-2.8)
 
#creates a vector 
df<-c(1.34,5.67,8.96,4,5.6678,0.00,6.55)
#returns the nearest value which will not be greater than the input values
floor(df)

 
#returns the final value which is nearest to input and also less than that. 
floor(-3.55 + 2.566 - 8.99 + 3.44 - 5.98)

输出:
5
-3
1 5 8 4 5 0 6
-13

#creates a vector
df<-c(1.34,5.66,7.89,9.54)
#returns the nearest value which is greater than the input 
ceiling(df)

#creates a vector
df<-c(-2.34,5.67,-9.87,-6.77)
#returns the nearest value which is greater than the input 
ceiling(df)

#returns the nearest value which is greater than the input 
ceiling(-3.45 + 5.678 - 7.890 - 4.678 + 6.89000 + 2.456 + 5.678)

输出:
2 6 8 10
-2 6 -9 -6
5

5. length() 和 lengths()

length()用来统计向量,因子,列表中元素的个数,返回一个值
lengths()用来统计列表、向量中每个元素的长度,返回一个列表

6.排序函数, order() rank() sort()

order函数用于返回向量大小顺序的秩。

a <- c(3,5,2,0)
order(a)

输出:4 3 1 2

order函数默认用于返回向量从下到大排序在原始向量中的位次(秩)。
加参数decreasing = T,降序排列

a <- c(3,5,2,0)
order(a,decreasing = T)

输出:2 1 3 4

对数据框排序

a <- c(3,7,5,2,9)
b <- c(9,3,7,8,1)
c <- c(3,7,8,2,5)
d <- data.frame(a,b,c)
#利用order函数对数据框的任意一列进行排序:
order(d$a)
实例演示1

order(d$a) 后
输出:4 1 3 2 5

按照数据框的a列进行排序:
d[order(d$a),]
实例演示二
实现a列升序 b列降序排列:
d[order(d$a,-d$b),]
实例演示三

在R中,和排序相关的函数主要有三个:sort(),rank(),order()。
sort(x)是对向量x进行排序,返回值排序后的数值向量。rank()是求秩的函数,它的返回值是这个向量中对应元素的“排名”。而order()的返回值是对应“排名”的元素所在向量中的位置。

> x<-c(97,93,85,74,32,100,99,67)
> sort(x)
[1]  32  67  74  85  93  97  99 100
> order(x)
[1] 5 8 4 3 2 1 7 6
> rank(x)
[1] 6 5 4 3 1 8 7 2

7. pmax()

pmax() 可以用来求多个变量之间的最大值。

x<- c(1,3,5,7,9)
y<-c(8,4,2,6,10)
d <- as.data.frame(cbind(x,y))

max(d$x, d$y)
#取x和y所有值里面的最大值
结果是10

pmax(d$x, d$y)
#取的是x[i]、y[i]这样每一组元素对比之后的最大值。
结果是8 4 5 7 10

8. levels()

水平就是因子的不同取值,R中一般把分类变量作为因子变量

a<- factor(c("a","b","c","a"))
a
[1] a b c a
Levels: a b c
> levels(a)
[1] "a" "b" "c"

你可能感兴趣的:(R中常用函数---第一讲)