r中的动态变量命名

时间:2013-12-31 10:26:32

标签: r variables dynamic-data

structure(list(Metrics = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L), .Label = c("  LINESCOMM ", "  NCNBLOC_FILE ", "  RCYCLOMATIC ", 
"  RISK ", "  RMAXLEVEL ", "  RNOEXSTAT "), class = "factor"), 
    Project = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
    2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L
    ), .Label = c("  Demo_Architect ", "  Demo_May_10 ", "  Demo_May_14 ", 
    "  NPP "), class = "factor"), Value = c(1172, 1500, 142, 
    4.241, 24, 98, 1139, 1419, 128, 3.546, 22, 85, 1172, 1500, 
    142, 4.241, 24, 98, 115008, 148903, 14539, 105.914, 604, 
    15710)), .Names = c("Metrics", "Project", "Value"), row.names = c(NA, 
-24L), class = "data.frame")->agg

我正在尝试:对于每个唯一的项目名称,需要创建包含所需值的单独变量名称。

我正在尝试以下代码:

x=data.frame()
attach(agg)
r<-as.character(unique(Project))
for(i in length(agg))
{
  x<-subset(agg,Project==r[i],select=c("Project","Metrics","Value"))
  assign() #This is where i am making mistake while creating dynamic variable naming
}

换句话说,我想在每次执行for循环时创建单独的变量名。

注意:首选变量名称应为“project”列值的名称。

2 个答案:

答案 0 :(得分:10)

assign用于提供您首先要创建的变量的名称以及它应该具有的值作为第二个参数。请注意,由于您的项目名称包含前导空格,我还使用str_trim来删除它们。

library(stringr)
projects <- levels(agg$Project)
for (p in projects) {
  x <- subset(agg, Project==p)
  assign(str_trim(p), x)    
}

现在您将项目作为工作区中的变量:

ls()
[1] "agg"            "Demo_Architect" "Demo_May_10"    "Demo_May_14"    "NPP"           
[6] "p"              "projects"       "x"

E.g。

> Demo_Architect
          Metrics           Project    Value
1      LINESCOMM    Demo_Architect  1172.000
2   NCNBLOC_FILE    Demo_Architect  1500.000
3    RCYCLOMATIC    Demo_Architect   142.000
4           RISK    Demo_Architect     4.241
5      RMAXLEVEL    Demo_Architect    24.000
6      RNOEXSTAT    Demo_Architect    98.000

答案 1 :(得分:3)

为什么不拆分data.frame并使用list

dflist <- split(agg, agg$Project)
str(dflist)
## List of 4
 ## $   Demo_Architect :'data.frame':   6 obs. of  3 variables:
 ## $   Demo_May_10    :'data.frame':   6 obs. of  3 variables:
 ## $   Demo_May_14    :'data.frame':   6 obs. of  3 variables:
 ## $   NPP            :'data.frame':   6 obs. of  3 variables:

names(dflist) <- paste0("project", seq_along(dflist))

如果您确实希望在全局环境中拥有列表元素(新dfs),则可以使用list2env

list2env(dflist, .GlobalEnv)
ls()
## [1] "agg"      "dflist"   "project1" "project2" "project3"
## [6] "project4"

head(project3)
##            Metrics        Project    Value
## 13      LINESCOMM    Demo_May_14  1172.000
## 14   NCNBLOC_FILE    Demo_May_14  1500.000
## 15    RCYCLOMATIC    Demo_May_14   142.000
## 16           RISK    Demo_May_14     4.241
## 17      RMAXLEVEL    Demo_May_14    24.000
## 18      RNOEXSTAT    Demo_May_14    98.000

只是想指出,使用lapplysapplyfor循环而不是使用全局环境来处理列表通常更安全。

编辑:如果您需要不同的命名方案

names(dflist) <- paste0("project_", gsub("\\s+", "", levels(agg$Project)))
list2env(dflist, .GlobalEnv)
ls()
## [1] "agg"                    "dflist"                
## [3] "project_Demo_Architect" "project_Demo_May_10"   
## [5] "project_Demo_May_14"    "project_NPP"