# R For Loop无法存储数据R For Loop unable to store the data

``````holder<-rep(0,3)
for(i in 1:3) {
apple<-c(i+1, i*2, i^3)
holder[i]<-apple
}
``````

``````Warning messages:
1: In holder[i] <- apple :
number of items to replace is not a multiple of replacement length
2: In holder[i] <- apple :
number of items to replace is not a multiple of replacement length
3: In holder[i] <- apple :
number of items to replace is not a multiple of replacement length
``````

## 2 个回复2

### ===============>>#1 票数：3 已采纳

``````holder<-matrix(0,nrow=3,ncol=3)
for(i in 1:3){
apple<-c(i+1, i*2, i^3)
holder[,i]<-apple  # columnwise, that's how sapply does it too
}
``````

``````holder <- vector('list',3)
for(i in 1:3){
apple<-c(i+1, i*2, i^3)
holder[[i]]<-apple
}
``````

``````holder <- sapply(1:3,function(i) c(i+1, i*2,i^3))
holder.list <- sapply(1:3,function(i) c(i+1, i*2,i^3),simplify=FALSE)
``````

### ===============>>#2 票数：2

``````holder<-matrix(nrow = 3, ncol = 3)

for(i in 1:3)

{

apple<-c(i+1, i*2, i^3)

holder[i,]<-apple

}
``````

3回复

1回复

2回复

2回复

2回复

1回复

1回复

1回复

1回复

2回复