提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
R是一种用于统计计算和图形的编程语言和软件环境。R语言由Ross Ihaka和Robert Gentleman在1993年在新西兰奥克兰大学统计系创建。R是GNU项目的一部分,并且其源代码在GNU通用公共许可证下发布。
R语言中的数据类型包括:
# 示例代码
num <- 123 # 数值型
char <- "Hello, R!" # 字符型
log <- TRUE # 逻辑型
factor_var <- factor(c("Male", "Female", "Male")) # 因子型
# 向量示例
vec <- c(1, 2, 3, 4, 5)
# 矩阵示例
mat <- matrix(1:9, nrow = 3, ncol = 3)
# 数据框示例
df <- data.frame(Name = c("Alice", "Bob", "Charlie"), Age = c(25, 30, 35))
# 列表示例
lst <- list(num = 1:5, char = c("a", "b", "c"), log = TRUE)
# 条件语句示例
if (2 > 1) {
print("2 is greater than 1")
} else {
print("2 is not greater than 1")
}
# for循环示例
for (i in 1:5) {
print(i)
}
# while循环示例
count <- 1
while (count <= 5) {
print(count)
count <- count + 1
}
在R中,函数是组织代码的基本单元。
# 自定义函数示例
my_function <- function(x) {
return(x^2)
}
# 调用函数
result <- my_function(4)
print(result)
R语言拥有强大的绘图功能,常用的包包括ggplot2
等。
# 基础绘图示例
plot(1:10, rnorm(10), type = "o", col = "blue", xlab = "X轴", ylab = "Y轴", main = "基础绘图示例")
# 使用ggplot2绘图示例(需先安装并加载ggplot2包)
# install.packages("ggplot2")
library(ggplot2)
data <- data.frame(x = rnorm(100), y = rnorm(100))
ggplot(data, aes(x = x, y = y)) + geom_point() + theme_minimal()
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,自学记录R语言基础知识点总览。