我正在尝试计算数据框中每列的平均值,并将平均值返回到数据框中的每一行,跨多个具有相似名称的列。我的想法是使用
mutate(across(starts_with()))
函数调用我想要操作的列,然后使用 ~summarize(mean())
计算每列的平均值并改变每列的原始值。但是,我收到一条错误消息,指出 summarize()
不能与 Fruits - Apples
列中我的数据类一起使用。当我使用 str()
检查该列时,它确认这些值属于字符类,因此我使用 as.numeric()
转换了所有内容。当我运行我的代码时,我仍然遇到同样的错误。
# Sample Data
test<-structure(list(`Fruits - Apples` = c("1", "4"), `Fruits - Oranges` = c("2",
"6"), `Fruits - Bananas` = c("5", "3")), row.names = c(NA, -2L
), class = c("tbl_df", "tbl", "data.frame"))
> test
# A tibble: 2 × 3
`Fruits - Apples` `Fruits - Oranges` `Fruits - Bananas`
<chr> <chr> <chr>
1 1 2 5
2 4 6 3
# Attempted Code
nicetry<-test%>%
mutate(across(everything(), ~as.numeric(.x)))%>%
mutate(across(starts_with("Fruits -"), ~ summarize(mean = mean(.x, na.rm = T))))
# Error Code
Error in `mutate()`:
ℹ In argument: `across(starts_with("Fruits -"), ~summarize(mean = mean(.x, na.rm = T)))`.
Caused by error in `across()`:
! Can't compute column `Fruits - Apples`.
Caused by error in `UseMethod()`:
! no applicable method for 'summarise' applied to an object of class "c('double', 'numeric')"
Run `rlang::last_trace()` to see where the error occurred.
# Desired Output
`Fruits - Apples` `Fruits - Oranges` `Fruits - Bananas`
2.5 4 4
2.5 4 4
你可以做
colMeans(type.convert(test, as.is = TRUE))
Fruits - Apples Fruits - Oranges Fruits - Bananas
2.5 4.0 4.0
我们从复制中得到什么?