Rscript test.R
5+5
2*2
5-7
(5+5)/2
2^2
28%%6
> getwd() # 获取当前路径
> setwd("~/") # 设置工作路径
数值型 numerics (1, 2.5)
逻辑判断 logical (TRUE or FALSE)
字符型 characters
因子 factors
判断数据类型 class(变量名)
a <- c(1, 2, 5, 3, 6, -2, 4)
b <- c("apple", "pear", "orange")
c <- c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)
矩阵是一个二维数组,只有每个元素是相同的数值型、字符型或逻辑型。可通过matrix()创建矩阵
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,
byrow=logical_value)
> myMatrix <- matrix(1:15, nrow=3, ncol=5)
> y <- matrix(1:18, nrow=2)
students <- c("A", "B", "C", "D")
math_score<-c(100, 80, 70, 95)
english_score<-c(96, 86, 77, 99)
students_scores<-data.frame(students, math_score,english_score)
students_scores[,2]
students_scores[2,2]
excellence<- c("excellent", "bad", "good", "okay", "bad")
excellence<- factor(excellence)
excellence <- factor(excellence, order=TRUE,levels=c("bad", "okay","good","excellent"))
excellence
a <- "My First List"
b <- c(25, 26, 18, 39)
c <- matrix(1:10, nrow=5)
d <- c("one", "two", "three")
mylist <- list(title=a ,b,c,d)
a <- c(2,3,4,5);
b <- c(1,3,5,7);
print(a+b);
print(a-b);
print(a*b);
print(a/b);
print(a%%b);
print(a^b);
print(a>b);
print(a==b);
print(a>=b);
print(a!=b);
print(a&b)
print(a|b)
v1 <- c(3,1,TRUE,2+3i);
v2 <<- c(3,1,TRUE,2+3i);
v3 = c(3,1,TRUE,2+3i);
print(v1);
print(v2);
print(v3);
c(3,1,TRUE,2+3i) -> v1;
c(3,1,TRUE,2+3i) ->> v2;
print(v1);
print(v2);
function_name <- function(arg_1, arg_2, ...) {
Function body
}
a = 'Hello'
b <- 'How'
c <- "are you? "
print(paste(a,b,c))
print(paste(a,b,c, sep = "-"))
print(paste(a,b,c, sep = " ", collapse = " "))