r - 按组的相关性
问题描述
已经有一些关于此的其他线程。我想实施以下建议的解决方案。
作为示例数据集:
data(Leinhart, package = "carData")
dat <- tibble::rowid_to_column(Leinhardt, var = "ID")
dat$income <- as.numeric(dat$income)
head(dat)
ID income infant region oil
1 3426 26.7 Asia no
2 3350 23.7 Europe no
3 3346 17.0 Europe no
4 4751 16.8 Americas no
5 5029 13.5 Europe no
6 3312 10.1 Europe no
我认为,这是其他帖子和我的错误中建议的解决方案。为什么会这样?
library(tidyverse)
library(broom)
dat %>%
group_by(region) %>%
summarize(correlation = cor(infant, income, method = "sp"))
Fehler in summarize(., correlation = cor(infant, income, method = "sp")) :
Argument "by" fehlt (ohne Standardwert)
R 版本:“R 版本 4.0.4 (2021-02-15)” Dplyr 版本:“1.0.4”。
(我已将其发布在另一个问题中,之前我已将其删除,因为有两个单独的问题引起了混乱。)
谢谢你。
解决方案
这段代码在我的机器上运行:
library(carData)
df <- Leinhardt
df %>%
group_by(region) %>%
summarize(correlation = cor(infant, income, method = "sp"))
# output
# A tibble: 4 x 2
region correlation
<fct> <dbl>
1 Africa -0.129
2 Americas NA
3 Asia NA
4 Europe -0.624
# try this code with your machine:
library(ggcorrplot)
model.matrix(~0+., data=df) %>%
cor(use="pairwise.complete.obs") %>%
ggcorrplot(show.diag = F, type="lower", lab=TRUE, lab_size=2)
推荐阅读
- javascript - 如何使用 vue 从对象数组中提取数据来提交预填充表单
- python - 我的编码并没有让我再次播放,而是循环播放
- swift - UserNotification 从未来的日期开始每分钟重复一次
- python - 为什么我的代码每次运行时都会产生不同的结果?(Python)
- javascript - HTML 中的动态超链接
- node.js - 如何从本地节点 js 转移到我的 vps 上?
- date - 自定义列和计算列
- sql - Hasura SQL 查询 id:整数不工作错误
- haskell - 无法理解 Haskell 跟踪
- ios - 从 SwiftUIView 中导航到 UIViewController