3 回答

TA貢獻1155條經(jīng)驗 獲得超0個贊
舊的離場-替代伎倆:
a<-data.frame(x=1:10,y=1:10)
test<-function(z){
mean.x<-mean(z$x)
nm <-deparse(substitute(z))
print(nm)
return(mean.x)}
test(a)
#[1] "a" ... this is the side-effect of the print() call
# ... you could have done something useful with that character value
#[1] 5.5 ... this is the result of the function call
編輯:使用新的測試對象運行它。
注意:當(dāng)一組列表項從第一個參數(shù)傳遞到lapply(當(dāng)對象從給定的列表中傳遞給for-循環(huán)如果結(jié)構(gòu)結(jié)果是正在處理的命名向量,則可以從結(jié)構(gòu)結(jié)果中提取“.names”-屬性和處理順序。
> lapply( list(a=4,b=5), function(x) {nm <- deparse(substitute(x)); strsplit(nm, '\\[')} )
$a
$a[[1]]
[1] "X" "" "1L]]"
$b
$b[[1]]
[1] "X" "" "2L]]"
> lapply( c(a=4,b=5), function(x) {nm <- deparse(substitute(x)); strsplit(nm, '\\[')} )
$a
$a[[1]]
[1] "structure(c(4, 5), .Names = c(\"a\", \"b\"))" ""
[3] "1L]]"
$b
$b[[1]]
[1] "structure(c(4, 5), .Names = c(\"a\", \"b\"))" ""
[3] "2L]]"

TA貢獻1796條經(jīng)驗 獲得超7個贊
deparse(quote(var))
- 3 回答
- 0 關(guān)注
- 500 瀏覽
添加回答
舉報