m <- 10
mtcars %>% dplyr::mutate(disp = .data$disp * .env$m)
相當于
m <- 10
mtcars %>% dplyr::mutate(disp = cur_data()$disp * .env$m)
你能舉一個例子cur_data(),并.data會產生不同的結果?
據我所知,cur_data()并.data沒有在所有背景關系中可以互換。
uj5u.com熱心網友回復:
這里是取自一個例子這里示出了不同的結果/錯誤
library(dplyr)
library(rstatix)
data %>%
summarise(across(where(is.numeric),
~ cur_data() %>%
levene_test(reformulate("Treatment", response = cur_column())))) %>%
unclass %>%
bind_rows(.id = 'flux')
# A tibble: 3 × 5
flux df1 df2 statistic p
<chr> <int> <int> <dbl> <dbl>
1 flux1 1 8 0.410 0.540
2 flux2 1 8 2.85 0.130
3 flux3 1 8 1.11 0.323
data %>%
summarise(across(where(is.numeric),
~ .data %>%
levene_test(reformulate("Treatment", response = cur_column())))) %>%
unclass %>%
bind_rows(.id = 'flux')
錯誤:
summarise()輸入有問題..1。?..1 = across(...). ? 無法將 class '"rlang_data_pronoun"' 強制轉換為 data.frame 運行rlang::last_error()以查看錯誤發生的位置。
資料
data <- data.frame(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L),
.Label = c("S1 ", "S2 ", "S3 "), class = "factor"),
plot = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L),
.Label = c(" Tree 1 ", " Tree 2 ", " Tree 3 "), class = "factor"),
Treatment = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("T1", "T2"), class = "factor"),
flux1 = c(11.52188065, 8.43156699, 4.495312274, -1.866676811, 3.861102035, -0.814742373, 6.51039536, 4.767950345, 10.36544542, 1.065963875),
flux2 = c(0.142259208, 0.04060245, 0.807631744, 0.060127596, -0.157762562, 0.062464942, 0.043147603, 0.495001652, 0.34363348, 0.134183704),
flux3 = c(0.147506197, 1.131009714, 0.038860728, 0.0176834, 0.053191593, 0.047591306, 0.00573377, -0.034926075, 0.123379247, 0.018882469))
uj5u.com熱心網友回復:
在 group_by 中.data仍然包括所有列,但cur_data()不包括 group_by 列。例如,下面cur_data()[["cyl"]] 是 NULL,因為 cyl 是按列分組,所以 x 不會出現在結果中,而 y 會出現。
library(dplyr)
mtcars %>%
group_by(cyl) %>%
mutate(x = cur_data()[["cyl"]], y = .data[["cyl"]]) %>%
ungroup %>%
names
## [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
## [11] "carb" "y"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/391167.html
