1、
for (i in 1:10)
{
print("hello,world!")
}
2、
for (i in 1:10)
{
print(i)
}
3、
sum = 0
for (i in seq(1,100,1))
{
sum = sum + i
}
print(sum)
4、求二维数组中的最大值
x<-c(11,34,12,87,89,93,54,31,88)
ar<-array(x,c(3,3))
max<-ar[1,1]
for(i in 1:3)
{
for(j in 1:3)
{
if (ar[i,j] > max)
{
max = ar[i,j]
}
}
}
print(max)
求三维数组的最大值
dim1 <- c("a1","a2","a3")
dim2 <- c("b1","b2","b3","b4","b3")
dim3 <- c("c1","c2")
tmp <- sample(1:100,3*5*2)
a <- array(tmp,c(3,5,2),dimnames = list(dim1,dim2,dim3))
max = a[1,1,1]
for (k in 1:2)
{
for (j in 1:5)
{
for (i in 1:3)
{
if (a[i,j,k] > max)
{
max = a[i,j,k]
}
}
}
}
print(max)
max(tmp)
5、求Fibonacci序列
n <- 30
a <- c(0,1)
for (i in 3:n)
{
a[i] = a[i - 1] + a[i - 2]
}
print(a)
6、已知Fibonacc数列为:1,1,2,3,5,8,求Fibonacc数列前50项的和
n <- 50
a <- c(1,1)
sum <- a[1] + a[2]
for (i in 3:n)
{
a[i] = a[i - 1] + a[i - 2]
sum = sum + a[i]
}
print(sum)
7、处理矩阵、数据框元素
a <- matrix(1:16,4,4)
a
for (i in 1:nrow(a))
{
for (j in 1:ncol(a))
{
a[i,j] = a[i,j] * 2
}
}
a