列举10个r语言编程程序
时间: 2023-08-16 07:09:46 浏览: 113
好的,以下是10个R语言编程程序的例子:
1. 矩阵转置:
```
x <- matrix(c(1,2,3,4), nrow=2)
t(x)
```
2. 数据框排序:
```
df <- data.frame(a=c(3,1,2), b=c("c","a","b"))
df[order(df$a),]
```
3. 数据框筛选:
```
df <- data.frame(a=c(3,1,2), b=c("c","a","b"))
subset(df, a>1)
```
4. 列表操作:
```
mylist <- list(1:3, c("a","b","c"), TRUE)
mylist[[2]][1]
```
5. 绘制散点图:
```
x <- rnorm(50)
y <- rnorm(50)
plot(x, y)
```
6. 绘制折线图:
```
x <- 1:10
y <- x^2
plot(x, y, type="l")
```
7. 线性回归:
```
x <- 1:10
y <- 2*x + rnorm(10)
fit <- lm(y ~ x)
summary(fit)
```
8. 统计描述:
```
x <- rnorm(100)
mean(x)
sd(x)
quantile(x)
```
9. 字符串操作:
```
x <- "Hello, world!"
substr(x, start=1, stop=5)
toupper(x)
```
10. 读写文件:
```
write.csv(df, file="mydata.csv")
mydata <- read.csv("mydata.csv")
```
阅读全文