108

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 lines. How could I achieve such a function by Python scripts?

My idea is to create an R vector and add those wanted elements into this vector so that the final vector is the same as that in R. I created a seq(), but it seems that it has an initial digit 1, so the final result would always start with the digit 1, which is not what I want. So, is there a better way to do this?

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
ligwin
  • 1,151
  • 3
  • 10
  • 7

7 Answers7

102
vec <- vector()

See also vector help

?vector
Brani
  • 6,454
  • 15
  • 46
  • 49
70

I pre-allocate a vector with

> (a <- rep(NA, 10))
 [1] NA NA NA NA NA NA NA NA NA NA

You can then use [] to insert values into it.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • 1
    This is the best answer if the length of the vector is known from the outset, as it will be most memory efficient – stevec Jan 18 '20 at 11:56
  • 1
    I find myself here 2 years later, about to comment how good this answer is, and I see the comment above already praising it. Thanks for a great answer! – stevec Jan 05 '22 at 22:54
28

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)

Aaron Statham
  • 2,048
  • 1
  • 15
  • 16
  • I like your solution with numeric(), but my experience has led me to use NA instead of 0 (if you use numeric(2), you will get 0 0). But that's my personal preference. – Roman Luštrik Aug 05 '10 at 12:19
  • Many thanks!! But is it possible to free the previous one after the addition of one new element? should I simply use rm()? – ligwin Aug 05 '10 at 12:41
  • There is another problem that typeof(numeric(0)) gives "double" whereas the wanted elements gives "integer", when added, a error was raised saying "The type for the new value cannot be different", how to convert? – ligwin Aug 05 '10 at 12:53
  • when working in python, the "as" is a keyword, in addition, python do not have such data types, so I think "as.integer" won't work? – ligwin Aug 05 '10 at 12:58
  • romunov - numeric(0) means create a numeric vector with length of 0 (ie no elements) not a vector of length 1 with that element being 0 – Aaron Statham Aug 05 '10 at 14:23
11

To create an empty vector use:

vec <- c();

Please note, I am not making any assumptions about the type of vector you require, e.g. numeric.

Once the vector has been created you can add elements to it as follows:

For example, to add the numeric value 1:

vec <- c(vec, 1);

or, to add a string value "a"

vec <- c(vec, "a");
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Eldaw
  • 119
  • 1
  • 2
  • 4
    Have you actually tried this? `vec <- c()` sets vec to `NULL`. If you want an empty vector you would use `vec <- character()`, `vec <- numeric()` etc. – Matthew Wise Oct 09 '15 at 16:25
7

I've also seen

x <- {}

Now you can concatenate or bind a vector of any dimension to x

rbind(x, 1:10)
cbind(x, 1:10)
c(x, 10)
JoFrhwld
  • 8,867
  • 4
  • 37
  • 32
4

As pointed out by Brani, vector() is a solution, e.g.

newVector <- vector(mode = "numeric", length = 50)

will return a vector named "newVector" with 50 "0"'s as initial values. It is also fairly common to just add the new scalar to an existing vector to arrive at an expanded vector, e.g.

aVector <- c(aVector, newScalar)

Sam
  • 791
  • 6
  • 9
3

In rpy2, the way to get the very same operator as "[" with R is to use ".rx". See the documentation about extracting with rpy2

For creating vectors, if you know your way around with Python there should not be any issue. See the documentation about creating vectors

lgautier
  • 11,363
  • 29
  • 42
  • I used to create a function in python for this purpose, which is very complicated and I am not sure how widely it could apply. Now I think with this operators, life could be much easier:) – ligwin Aug 07 '10 at 12:01