Dynamic Variable naming in r

Viewed 16828
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

I am trying to do: For each unique project name is want create separate variable name containing the desired values.

I am trying the below code:

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
}

in other words i want to create separate variable name each time the for loop executes.

NOTE: it is preferred to have variable names to be in the name of "project"columns values.

2 Answers
Related