Python 如何创建一个空的 R 向量来添加新项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3413879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:56:18  来源:igfitidea点击:

How to create an empty R vector to add new items

pythonrvectorrpy2

提问by ligwin

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?

我想在 Python 中使用 R,正如模块 Rpy2 提供的那样。我注意到 R 有非常方便的[]操作,您可以通过这些操作提取特定的列或行。我如何通过 Python 脚本实现这样的功能?

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?

我的想法是创建一个 R 向量并将那些想要的元素添加到这个向量中,以便最终向量与 R 中的相同。我创建了一个seq(),但它似乎有一个初始数字 1,所以最终结果总是从数字 1 开始,这不是我想要的。那么,有没有更好的方法来做到这一点?

回答by Roman Lu?trik

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.

然后,您可以使用 [] 将值插入其中。

回答by Aaron Statham

You can create an empty vector like so

您可以像这样创建一个空向量

vec <- numeric(0)

And then add elements using c()

然后使用 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)

然而,正如 romunov 所说,最好预先分配一个向量然后填充它(因为这样可以避免每次添加元素时重新分配向量的新副本)

回答by Brani

vec <- vector()

See also vector help

另请参阅矢量帮助

?vector

回答by JoFrhwld

I've also seen

我也看过

x <- {}

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

现在您可以将任何维度的向量连接或绑定到 x

rbind(x, 1:10)
cbind(x, 1:10)
c(x, 10)

回答by lgautier

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

在 rpy2 中,使用 R 获得与“[”完全相同的运算符的方法是使用“.rx”。请参阅有关使用 rpy2 提取的文档

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

对于创建向量,如果您知道使用 Python 的方法,那么应该没有任何问题。请参阅有关创建向量的文档

回答by Sam

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

正如 Brani 所指出的,vector() 是一种解决方案,例如

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

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.

将返回一个名为“newVector”的向量,其中 50 个“0”作为初始值。将新标量添加到现有向量以获得扩展向量也很常见,例如

aVector <- c(aVector, newScalar)

aVector <- c(aVector, newScalar)

回答by Eldaw

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:

例如,要添加数值 1:

vec <- c(vec, 1);

or, to add a string value "a"

或者,添加一个字符串值“a”

vec <- c(vec, "a");