3 回答

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊
舊的離場(chǎng)-替代伎倆:
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
編輯:使用新的測(cè)試對(duì)象運(yùn)行它。
注意:當(dāng)一組列表項(xiàng)從第一個(gè)參數(shù)傳遞到lapply(當(dāng)對(duì)象從給定的列表中傳遞給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貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
deparse(quote(var))
- 3 回答
- 0 關(guān)注
- 812 瀏覽
添加回答
舉報(bào)