簡體   English   中英

我如何像使用.lang一樣從rlang的.data獲取數據屬性?

[英]How can I get data attributes from rlang's .data like I can with .?

我正在構建一個整齊兼容的函數,以便在dplyrmutate ,我想在其中傳遞變量以及正在使用的數據集,並使用兩者的信息來構建向量。

作為一個基本示例,假設我想返回一個包含變量平均值和數據集中行數的字符串(我知道我可以取var的長度,忽略它,這是一個示例)。

library(tidyverse)
library(rlang)

info <- function(var,df = get(".",envir = parent.frame())) {
  paste(mean(var),nrow(df),sep=', ')
}

dat <- data.frame(a = 1:10, i = c(rep(1,5),rep(2,5)))

#Works fine, 'types' contains '5.5, 10'
dat %>% mutate(types = info(a))

好的,到目前為止很好。 但是現在也許我希望它能處理分組數據。 var來自一組,但是. 將是完整的數據集。 因此,我將改用rlang.data代詞,它只是正在處理的數據。

但是, .data不像. . 是數據集,但.data只是代名詞,我可以使用.data[[varname]]提取變量。

info2 <- function(var,df = get(".data",envir = parent.frame())) {
  paste(mean(var),nrow(.data),sep=', ')
}

#Doesn't work. nrow(.data) gives blank strings
dat %>% group_by(i) %>% mutate(types = info2(a))

我如何從.data獲取全部內容 我知道我沒有在示例中包括它,但是具體來說,我都需要attr(dat)某些內容以及dat中的變量(這些內容已適當地歸類為分組attr(dat)某些內容,因此都沒有還原為. 也不會僅僅提取變量並從那里獲取東西會起作用。

正如Alexis在上面的評論中提到的那樣,這是不可能的,因為這不是.data的預期用途。 但是,既然我已經放棄了直接執行此操作,那么我已經使用的組合來解決了問題. .data

info <- function(var,df = get(".",envir = parent.frame())) {
  #First, get any information you need from .
  fulldatasize <- nrow(df)

  #Then, check if you actually need .data,
  #i.e. the data is grouped and you need a subsample
  if (length(var) < nrow(df)) {
      #If you are, get the list of variables you want from .data, maybe all of them
      namesiwant <- names(df)

      #Get .data
      datapronoun <- get('.data',envir=parent.frame())

      #And remake df using just the subsample
      df <- data.frame(lapply(namesiwant, function(x) datapronoun[[x]]))
      names(df) <- namesiwant
  }

  #Now do whatever you want with the .data data
  groupsize <- nrow(df)

  paste(mean(var),groupsize,fulldatasize,sep=', ')
}

dat <- data.frame(a = 1:10, i = c(rep(1,5),rep(2,5)))

#types contains the within-group mean, then 5, then 10
dat %>% group_by(i) %>% mutate(types = info(a))

為什么不在這里使用length()而不是nrow()

dat <- data.frame(a = 1:10, i = c(rep(1,5),rep(2,5)))

info <- function(var) {
  paste(mean(var),length(var),sep=', ')
}
dat %>% group_by(i) %>% mutate(types = info(a))
#> # A tibble: 10 x 3
#> # Groups:   i [2]
#>        a     i types
#>    <int> <dbl> <chr>
#>  1     1     1 3, 5 
#>  2     2     1 3, 5 
#>  3     3     1 3, 5 
#>  4     4     1 3, 5 
#>  5     5     1 3, 5 
#>  6     6     2 8, 5 
#>  7     7     2 8, 5 
#>  8     8     2 8, 5 
#>  9     9     2 8, 5 
#> 10    10     2 8, 5

暫無
暫無

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

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