简体   繁体   中英

Rbind to a vector using for loop

I'm having a simple problem but i'm a little confused how to get around it.

Let's say I want to create a vector that looks like ID = [1 2]. I use a for loop to get to that vector but instead of creating a vector, I think R is just adding the outputs up.

Here is my code:

block = 2
j = 0 
ID = data.frame(NULL)

for (j in block){
j = j + 1
ID = rbind(ID, j)
}

What I want is

ID
[1 2]

What I get instead is:

ID 
x2
2

What am I doing wrong?

We are only loopoing through a single element, so instead of 'block', it would be seq_len(block)

for(i in seq_len(block)){ 
      j = j + 1
      ID = rbind(ID, j)
  }
ID
#   X1     
#1  1
#2  2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM