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”列值的名称。
length(agg)
将计算您的data.frame
的列数。您确定这就是您想要的吗?您的解释不是很清楚,因此需要提供所需的输出。split( agg , agg$Project )
是否给您所需的内容? - Simon O'Hanlon