for 循环中的变量赋值 [英] Variable assignment within a for-loop

查看:61
本文介绍了for 循环中的变量赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
R:如何将字符串转换为变量名? >

在 R 中,我正在编写一个 for 循环,它将迭代地创建变量名,然后为每个变量赋值.

In R, I'm writing a for-loop that will iteratively create variable names and then assign values to each variable.

这是一个简化版本.目的是根据迭代变量 i 的值创建变量名称,然后用 NA 值填充新变量.

Here is a simplified version. The intention is to create the variable's name based on the value of iterating variable i, then fill the new variable with NA values.

(我只是在下面 1:1 迭代,因为出现的问题与循环本身无关,而是与创建和分配变量的方式有关.)

(I'm only iterating 1:1 below since the problem occurs isn't related to the looping itself, but rather to the way the variable is being created and assigned.)

for (i in 1:1) {

    #name variable i "Variablei"
    varName = paste("Variable", as.character(i), sep="")

    #fill variable with NA values
    varName = rep(NA, 12)

    print(varName)
    print(Variable1)
}

现在,varName 打印为

Now, varName prints out as

 [1] NA NA NA NA NA NA NA NA NA NA NA NA

并且未找到变量 1.

我在某种程度上理解为什么这是错误的.在第一行中,varName 成为一个向量,它的唯一条目是字符串Variable1".然后重新分配 varName 以保存 NA 值.所以当我尝试打印 Variable1 时,它不存在.

I understand on some level why this is buggy. In the first line, varName becomes a vector whose only entry is the string "Variable1". Then varName gets reassigned to hold the NA values. So when I try to print Variable1, it doesn't exist.

我认为更普遍的问题是分配与平等.在第一行中,我希望 varName 等于新创建的字符串,但在下一行中,我希望将 varName 分配给 NA 值向量.

I think the more general issue is assignment vs. equality. In the first line, I want varName to be equal to the newly made string, but in the next line, I want varName to be assigned to the NA value vector.

创造这种区别的最简单方法是什么?我也愿意接受完全不同、更好的方法来解决这个问题.

What is the simplest way of creating that distinction? I'm also open to entirely different, better ways to go about this.

更改标题,因为我错误地描述了问题.

推荐答案

分配的工作方式如下:

<varname> = <expression>

或更传统的

<varname> <- <expression>

因此,在您的代码中,您只分配给了 varName.这不是关于分配与平等,只是分配.你可能想看看 assign:

So, in your code, you have only ever assigned to varName. It's not about assignment vs equality, just assignment. You may want to look at assign:

for (i in 1:5) {
  assign(paste0("Variable", i), 10*i)
} 

作为玩具示例.

此外,如评论中所述,您的应用程序可能有更好的方法.例如,为什么不只使用向量 myvector 而不是使用名为 Variable1Variable2 等的变量,您可以参考 myvector[1]myvector[2]

Moreover, as noted in the comments, there are probably better approaches for your application. For example, why not just use a vector myvector and instead of having variables called Variable1, Variable2, etc you can refer to myvector[1], myvector[2] etc.

举个例子,假设您计划与我们合作

As an example, let us say you had planned to work with

Variable1 <- 'foo'
Variable2 <- 'bar'
Variable3 <- 'baz'

然后,你可以改变你的方法,并设置

then, you could change you approach, and set

mydata <- c('foo', 'bar', 'baz')

在您之前使用 Variable2(其中包含 'bar')的地方,您改为使用 mydata[2](其中还包含'bar').这里的要点是,在 R 中使用向量和数据框比使用一长串变量要容易得多.

and where you would previously have used Variable2 (which contains 'bar') you instead use mydata[2] (which also contains 'bar'). The point here is that it is much easier to work with vectors and dataframes in R than a long list of variables.

您可以进一步为条目命名:

You could go further and name the entries:

names(mydata) <- paste0("V", 1:3)

然后允许您编写 mydata["V2"] 来检索 bar.

which then allows you to write mydata["V2"] to retrieve bar.

这篇关于for 循环中的变量赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆