我有一个看起来像这样的函数:
removeRows <- function(dataframe, rows.remove){
dataframe <- dataframe[-rows.remove,]
print(paste("The", paste0(rows.remove, "th"), "row was removed from", "xxxxxxx"))
}
Run Code Online (Sandbox Code Playgroud)
我可以使用这样的函数从数据帧中删除第5行:
removeRows(mtcars, 5)
Run Code Online (Sandbox Code Playgroud)
该函数输出此消息:
"The 5th row was removed from xxxxxxx"
Run Code Online (Sandbox Code Playgroud)
如何将xxxxxxx替换为我使用的数据帧的名称,所以在这种情况下mtcars?
Kon*_*lph 10
您需要在未评估的上下文中访问变量名称.我们可以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))
}
Run Code Online (Sandbox Code Playgroud)
事实上,这是它的主要用途; 根据文件,
典型的用途
substitute是为数据集和图创建信息标签.
| 归档时间: |
|
| 查看次数: |
8781 次 |
| 最近记录: |