使用for循环在R中创建动态对象

发布于 2025-02-13 13:52:13 字数 1769 浏览 0 评论 0原文

我正在尝试使用for循环来创建R中的一组动态对象。这些将包含一个针对某个指标的组织和值的列表 - 每个指标将是单个度量的值。

实际上,这将用于使用GGPLOT2创建图表对象,然后将其用于RmarkDown。对于下面的示例,它只是每个度量标准的head()函数的示例。

我尝试使用粘贴函数创建此名称,但是它给出以下错误:

粘贴中的错误(“ ankerisation_short”,“ _”,metricids [x])< - 头(OrganisationData_Jan2021):作业的目标扩展到 非语言对象

可能会有所帮助,但我不确定如何使用它。 (我的尝试也产生了错误)。我在下面的链接中找到了一个类似的问题,但是它以直接将数据输送到分配的方式进行设置。我也不清楚什么“值=”。在做。此查询如下: 在r

i 中,动态名称对象 'value = = 。”指的是被管道输送到分配函数的数据。我创建了下面代码中的替代版本。

分配中的错误(x = ankyisationdata_jan2021,value = paste0(“ sampledata”,:: 无效的第一个参数

想法是按照以下方式创建输出文件:ankerisation_short_abc123,ankerisation_short_abc323,ankerisation_short_kjm088

我将感谢您可能拥有的任何指导!

    MetricIDs <- c('ABC123','ABC323','KJM088')

# Attempt using paste
    for (x in 1:3) 
      {
      organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
      paste("organisation_short","_", MetricIDs[x]) <- head(organisationdata_Jan2021)  # Goal: Create object that includes the Metric ID.
      }

# Attempt using assign 
for (x in 1:3) 
{
  organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
  assign(x=organisationdata_Jan2021, value=paste0("sampledata",MetricIDs[x])) 
}

    
    
    # Expected object names:  organisation_short_ABC123, organisation_short_ABC323, organisation_short_KJM088
    # This will be used to create chart objects using ggplot2, and those objects will be used in an R MarkDown document.

I'm trying to use a for loop to create a set of dynamic objects in R. These will contain a list of organisations and values against a certain metric--each output will be the values of an individual metric.

In practice, this will be used to create chart objects using ggplot2, which I'll then use in RMarkdown. For the example below, it's just a sample using a head() function for each metric.

I tried using the paste function to create this name, but it gives the following error:

Error in paste("organisation_short", "_", MetricIDs[x]) <-
head(organisationdata_Jan2021) : target of assignment expands to
non-language object

I understand that the assign function might help, but I'm not sure how to use it. (My attempts also produced errors). I found a similar question in the link below, but it's set up in a way that pipes data directly into assign. I'm also not clear what "value = ." is doing. This query is below:
dynamically name objects in R

I believe the "value = ." refers to the data being piped into the assign function. I created an alternative version which is in the code below.

Error in assign(x = organisationdata_Jan2021, value = paste0("sampledata", :
invalid first argument

The idea is to create output files along the lines of: organisation_short_ABC123, organisation_short_ABC323, organisation_short_KJM088

I would be grateful for any guidance you might have!

    MetricIDs <- c('ABC123','ABC323','KJM088')

# Attempt using paste
    for (x in 1:3) 
      {
      organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
      paste("organisation_short","_", MetricIDs[x]) <- head(organisationdata_Jan2021)  # Goal: Create object that includes the Metric ID.
      }

# Attempt using assign 
for (x in 1:3) 
{
  organisationdata_Jan2021 <- organisationdata_CM0040_Jan2021 %>% filter(Metric_ID==MetricIDs[x])  # Filter data to specific Metric ID
  assign(x=organisationdata_Jan2021, value=paste0("sampledata",MetricIDs[x])) 
}

    
    
    # Expected object names:  organisation_short_ABC123, organisation_short_ABC323, organisation_short_KJM088
    # This will be used to create chart objects using ggplot2, and those objects will be used in an R MarkDown document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文