简体   繁体   English

从动态变量中选择一列

[英]Select a column from a dynamic variable

How can I select the second column of a dynamically named variable? 如何选择动态命名变量的第二列?

I create variables of the form "population.USA", "population.Mexico", "population.Canada". 我创建“ population.USA”,“ population.Mexico”,“ population.Canada”形式的变量。 Each variable has a column for the year, and another column for the population value. 每个变量都有一列用于年份,另一列用于人口值。 I would like to select the second column from each of these variables during a loop. 我想在循环中从这些变量的每一个中选择第二列。

I use this syntax: 我使用以下语法:

sprintf("population.%s", country)[, 2]

R returns the error: Error in sprintf("population.%s", country)[, 2] : incorrect number of dimensions R返回错误: Error in sprintf("population.%s", country)[, 2] : incorrect number of dimensions

Based on your sequence of questions over the last few minutes, I have two general recommendations for you as you get familiar with R: 根据您最近几分钟的提问顺序,当您熟悉R时,我有两个一般建议:

  1. Don't use sprintf . 不要使用sprintf
  2. Don't use assign . 不要使用assign

Now, obviously, those functions are both useful at times. 现在,显然,这些功能有时都是有用的。 But you've learned about them too early, before you've mastered some basic stuff about R's data structures. 但是,在您掌握有关R的数据结构的一些基本知识之前,您还为时尚早。 Try to write code without those crutches (for the time being!), as they're just causing you problems. 尝试编写那些没有拐杖的代码(暂时!),因为它们只会给您带来麻烦。

Rather than creating separate individual variables for each nation's population, place them in a list. 与其为每个国家的人口创建单独的变量,不如将它们放在列表中。

population <- vector("list",3)
names(population) <- c('USA','Mexico','Russia')

Then you can access each using the string representation of the name of each country: 然后,您可以使用每个国家/地区名称的字符串表示形式访问每个国家/地区:

population[['USA']] <- 10000

Or, 要么,

region <- 'USA'
population[[region]]

In this example, I've assigned a single value to a list element, lists will hold any other data type, including matrices or data frames. 在此示例中,我为list元素分配了一个值,列表将保存任何其他数据类型,包括矩阵或数据帧。 It will be a lot less typing than using sprintf and assign , and a lot safer and more efficient as well. 这将是一个比使用少了很多打字sprintfassign ,很多更安全和更有效的为好。

See ?get . 请参阅?get Here is an example: 这是一个例子:

> country <- "FOO"
> assign(sprintf("population.%s", country), data.frame(runif(5), runif(5)))
> 
> get(sprintf("population.%s", country))[,2]
[1] 0.2241105 0.5640709 0.5945869 0.1830719 0.1895938

It is critically important to look at the object returned by a function if you get an error. 如果发现错误,查看函数返回的对象至关重要。 It is immediately clear why your example fails if you just look at what it returns: 现在很清楚,如果仅查看示例返回的内容,为什么示例会失败:

> sprintf("population.%s", country)
[1] "population.FOO"

At that point it would be immediately clear, if you didn't already know or have thought to read ?sprintf , that sprintf() returns a string not the object of that name. 到那时,如果您还不知道或不打算阅读?sprintf ,那sprintf()将返回一个不是该名称的对象的字符串,这将立即很明显。 Armed with that knowledge you would have narrowed down the problem to how to recall an object from the computed name? 有了这些知识,您就可以将问题范围缩小到如何从计算出的名称中调用对象了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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