0

我正在尝试遍历变量(我仍在努力了解 apply() 系列,很快就会有一天)并构造我希望为其获取标签的变量名称。

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

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)
}

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

非常感谢您的见解和指示。

谢谢,

松紧线

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

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

4

2 回答 2

2

这将起作用:

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

这会更简单:

label(t)
于 2012-10-26T15:23:15.880 回答
1

嘿,这是一种“黑客”,但试试这个:

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

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

于 2012-10-26T17:23:21.933 回答