13

次のような関数があります。

removeRows <- function(dataframe, rows.remove){
  dataframe <- dataframe[-rows.remove,]
  print(paste("The", paste0(rows.remove, "th"), "row was removed from", "xxxxxxx"))
}

次のような関数を使用して、データフレームから 5 行目を削除できます。

removeRows(mtcars, 5)

関数は次のメッセージを出力します。

"The 5th row was removed from xxxxxxx"

この場合、 xxxxxxx を使用したデータフレームの名前に置き換えるにはどうすればよいmtcarsですか?

4

1 に答える 1

15

未評価のコンテキストで変数名にアクセスする必要があります。substituteこれには次を使用できます。

removeRows <- function(dataframe, rows.remove) {
  df.name <- deparse(substitute(dataframe))
  dataframe <- dataframe[rows.remove,]
  print(paste("The", paste0(rows.remove, "th"), "row was removed from", df.name))
}

実際、それが主な用途です。ドキュメントに従って、

の典型的な用途はsubstitute、データ セットとプロットに有益なラベルを作成することです。

于 2013-05-24T20:16:42.407 に答える