簡體   English   中英

在函數內如何正確地將參數作為R中的數據屬性傳遞?

[英]Within function how to correctly pass a parameter as a data attribute in R?

我想編寫一個返回數據主體特定屬性的函數,如下所示

dem <- function(sale, attToCall = "year" ){
return(sale$attToCall)
}

aa <- data.frame("month" = sample(1:12, 10),"year" = sample(1990:2016, 10))

dem(aa, attToCall = "year") 

不起作用,有人可以解決嗎?

dem <- function(sale, attToCall = "year" ){
  return(sale[,attToCall])
}

aa <- data.frame("month" = sample(1:12, 10),"year" = sample(1990:2016, 10))

dem(aa, attToCall = "year") 

[1] 2012 2004 2010 2001 1995 1995 2015 2015 2009 1997

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM