簡體   English   中英

通過應用函數R傳遞的同名

[英]Colnames passed through an apply function R

我試圖在data.frame上使用Apply和用戶定義的函數。 在函數內部,我想使用列名(用於情節的標題),但是apply似乎會去除列名並僅傳遞一個向量。 MWE:

trialData <- data.frame('a' = rnorm(100),
                        'b' = rnorm(100),
                        'c' = rnorm(100))

someData <- function(dataInput){
  return(colnames(dataInput))
}

dataOutput <- apply(trialData, 2, someData)

print(dataOutput)

返回NULL 有什么方法可以訪問函數內部的列名?

感謝評論者,我到達了下面,這給了我想要的結果。

trialData <- data.frame('a' = rnorm(100),
                        'b' = rnorm(100),
                        'c' = rnorm(100))

someData <- function(dataInput){
  # lots of code here
  return(
    dataName = colnames(dataInput)
  )
}

dataOutput <- lapply(colnames(trialData), function(x){someData(trialData[x])})

print(dataOutput)

暫無
暫無

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

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