How to create an empty R vector to add new items

前端 未结 7 830
再見小時候
再見小時候 2020-12-07 10:42

I want to use R in Python, as provided by the module Rpy2. I notice that R has very convenient [] operations by which you can extract the specific columns or li

7条回答
  •  春和景丽
    2020-12-07 11:29

    You can create an empty vector like so

    vec <- numeric(0)
    

    And then add elements using c()

    vec <- c(vec, 1:5)
    

    However as romunov says, it's much better to pre-allocate a vector and then populate it (as this avoids reallocating a new copy of your vector every time you add elements)

提交回复
热议问题