在循环中构造变量并使用它们

时间:2012-10-26 14:49:45

标签: r loops for-loop

我正在尝试循环变量(我仍然很难在申请()系列中找到我的头,很快就会有一天)并构建我希望获得标签的变量名称。

简化的例子可能是最简单的......

library(Hmisc)
t <- data.frame(matrix(1:100, 10))
label(t$X1)  <- "This is my first variable"
label(t$X2)  <- "This is my second variable"
label(t$X3)  <- "This is my third variable"
label(t$X4)  <- "This is my fourth variable"
label(t$X5)  <- "This is my fifth variable"
label(t$X6)  <- "This is my sixth variable"
label(t$X7)  <- "This is my seventh variable"
label(t$X8)  <- "This is my eighth variable"
label(t$X9)  <- "This is my ninth variable"
label(t$X10) <- "This is my tenth variable"
for(x in 1:10){
  my.label <- label(paste("t$X", x, sep=""))
  print(my.label)
}

运行时,这会给....

[1] ""
[1] ""
[1] ""
[1] ""
[1] ""
[1] ""
[1] ""
[1] ""
[1] ""
[1] ""

当我期待看到

[1] "This is my first variable"
[1] "This is my second variable"
[1] "This is my third variable"
[1] "This is my fourth variable"
[1] "This is my fifth variable"
[1] "This is my sixth variable"
[1] "This is my seventh variable"
[1] "This is my eighth variable"
[1] "This is my ninth variable"
[1] "This is my tenth variable"

我知道paste()函数正常工作,因为......

for(x in 1:10){
  print(paste("t$X", x, sep=""))
}
[1] "X$1"
[1] "X$2"
[1] "X$3"
[1] "X$4"
[1] "X$5"
[1] "X$6"
[1] "X$7"
[1] "X$8"
[1] "X$9"
[1] "X$10"

我很难过,我已经尝试将paste()放在eval()中,如... ...

for(x in 1:10){
  my.label <- label(eval(paste("t$X", x, sep="")))
  print(my.label)
}

......但没有快乐。这似乎是一件很简单的事情,我试图寻找解决方案,但显然没有用我正在尝试的短语来正确描述它。

非常感谢见解和指示。

谢谢,

slackline

编辑:以上是一个简单的例子来说明我想要实现的目标,这有点复杂,目前我的代码看起来像......

for(type1 in c("bouldering", "routes")){
  if(type1 == "bouldering"){
    part   <- c("indoors", "outdoors")
    xlabel <- "Grade (Fontainebleau)"
  }
  else if(type1 == "routes"){
    part <- c("onsight", "redpoint")
    xlabel <- "Grade (French Sports)"
  }
  for(type2 in part){
    for(training in c("pullup.rep", "pullup.weight", "hang.time", "hang.size", "bench.press", "bench.press.scaled", "dead.lift", "dead.lift.scaled", "front.lever", "height", "weight", "bmi")){
      ### Obtain the current variables label for using in the graph
      ylabel <- label(paste("clean.data", training, sep="$"))
      ### Paste the bouldering/routes together with indoors/
      ### outdoors or onsight/redpoint so variables and files can be constructed
      file.stub <- paste(type1, type2, sep="-")
      metric    <- paste(type1, type2, sep=".")
      file.out  <- paste("latex/figures/", gsub("\\.", "-", training) , "-", file.stub, ".png", sep="")
      png(file.out, width=1024, height=768)
      t <- qplot(metric, training,
                 data     = clean.data,
                 geom     = "boxplot",
                 fill     = factor(metric),
                 xlab     = xlabel,
                 ylab     = ylabel)
      t + opts(legend.position = "none")
      dev.off()
    }
  }
}

所以目前我没有得到标签,而且我没有得到图形,因为命令(label()qplot())不知道我指的是列名与数据框clean.data

2 个答案:

答案 0 :(得分:2)

这将有效:

for(x in 1:10){
  my.label <- label(t[paste("X", x, sep="")])
  print(my.label)
}

这会更简单:

label(t)

答案 1 :(得分:1)

嘿,这有点像'黑客',但试试这个:

for (i in 1:10)    {
my.label <- label(t[,names(t)==paste("X", i, sep="")])
print(my.label)
}

因此,不是将粘贴的材质转换为数据框列调用,而是将列名称转换为字符串。当我尝试它时,它对我有用。