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"

私は困惑しています、私は次のようにeval()内にpaste()を配置しようとしました...

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 に答える