Reputation: 135
I have this dataframe list containing different dataframes.
df_list <- list(
`1.3.A` =
tibble::tribble(
~Person, ~Height, ~Weight,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L
),
`2.2.A` =
tibble::tribble(
~Person, ~Height, ~Weight,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L
),
`1.1.B` =
tibble::tribble(
~Person, ~Height, ~Weight,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L,
"Alex", 175L, 75L,
"Gerard", 180L, 85L,
"Clyde", 179L, 79L
)
)
What I was trying to do was to test for homogeneity of variance among groups (Person in this case). But after several trials of piping lapply() with leveneTest() my codes showed two different results.
This particular code worked and gave me the result I was looking for.
ht <- function(df){
leveneTest(df$Height ~ df$Person)
}
final_ht <- lapply(df_list, ht)
However, for this code
wood <- lapply(df_list, function(i) ~leveneTest(y = Height, group = Person, data = i))
would give a result showing <environment: 0x55869b4289b8>
I have no idea why the function in wood
didn't return or run the leveneTest.
What is happening with lapply()
in both codes?
Can somebody enlighten me on what lapply()
is doing on both cases?
Also, in the code ht
it returns a warning, is this ok?
In leveneTest.default(y = y, group = group, ...) :
group coerced to factor. Error in cat("1: In leveneTest.default(y = y,
group = group, ...) : group coerced to factor.", :
argument 2 (type 'list') cannot be handled by 'cat'
Would very much appreciate any help.
Upvotes: 0
Views: 73
Reputation: 160417
Remove the tilde, as in
wood <- lapply(df_list, function(i) leveneTest(y = Height, group = Person, data = i))
If you were using purrr
(or some other rlang
-derived set of functions), then you might use it as something like:
purrr::map(df_list, ~ leveneTest(y = Height, group = Person, data = .x))
# or
purrr::map(df_list, ~ leveneTest(y = Height, group = Person, data = .))
In base R, the tilde returns a formula/expression that stores with it the environment of the calling expression, which is why you see <environment:...>
. In base R, it is often used to define relationships between response and model variables, such as lm(mpg ~ disp + cyl, data=mtcars)
; it can also be used to define marginal variables, as in xtabs(~ cyl + vs, data = mtcars)
. In neither of those situations is the actual calling environment (stored with the ~
) used, but it's still there.
In purrr
, it can be used to more tersely define anonymous functions. The following are, for the most part, equivalent:
lapply(df_list, function(dat) leveneTest(y = Height, group = Person, data = dat))
purrr::map(df_list, function(dat) leveneTest(y = Height, group = Person, data = dat))
purrr::map(df_list, ~ leveneTest(y = Height, group = Person, data = .))
purrr::map(df_list, ~ leveneTest(y = Height, group = Person, data = .x))
# new to R-4.1
lapply(df_list, \(dat) leveneTest(y = Height, group = Person, data = dat))
Upvotes: 2