r - R中所有数字的中位数和所有字符的模式

标签 r dplyr

使用像原始这样的数据集:

id <- c("JF", "GH", "GH", "ANN", "GH", "ROG", "JF")
group <- c("most", "least", "most", "least", "least", "most", "least")
NP <- c(4,6,18,1,3,12,8)
iso_USA <- c(1, 0, 0, 0, 0, 1, 1)
iso_CHN <- c(0, 1, 1, 0, 0, 0, 0)
color <- c("blue", "orange", "blue", "blue", "red", "orange", "black")

original <- data.frame(id, group, NP, iso_USA, iso_CHN, color)


numeric <- unlist(lapply(original, is.numeric))  
numeric <- names(original[ , numeric])

char <- unlist(lapply(original, is.character))  
char <- names(original[ , char])
char <- char[-1]   #remove id from variables of interest

我想按“组”分组并计算数值变量的中位数字符变量的众数。因此,数据看起来像 original2。请注意,我的实际数据集的列数比此处显示的模拟版本多得多:

group <- c("least", "most")
NP <- c(6,12)
iso_USA <- c(0,1)
iso_CHN <- c(0, 0)
color <- c("orange", "blue")

original2 <- data.frame(group, NP, iso_USA, iso_CHN, color)

有什么线索吗?

最佳答案

使用 dplyracross 功能和接受的答案 at the FAQ about implementing a mode function :

Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

library(dplyr)
original %>%
  select(-id) %>%
  group_by(group) %>%
  summarize(
    across(where(is.numeric), median),
    across(where(is.character), Mode)
  )
# # A tibble: 2 × 6
#   group    NP iso_USA iso_CHN color 
#   <chr> <dbl>   <dbl>   <dbl> <chr> 
# 1 least   4.5       0       0 orange
# 2 most   12         1       0 blue  

关于r - R中所有数字的中位数和所有字符的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70189952/

相关文章:

r - 我可以使用 R ggsurvplot 更改 Kaplan Meier 图中 surv.median.line 的颜色吗?

R循环和data.frame

Rd2pdf - 特殊(和德语)字符

r - "tidyr like"从不同的列中填充 na

regex - 将标点符号转换为空格

r - 如何在ggplot中将轴值强制为科学计数法

r - 在 dplyr 中有条件地改变数据

r - 如何按组计算日期之间的时间差

r - 如何用组内以前的非 NaN 替换 NaN 值

r - 为什么 summarize() 中的 cur_data() 会返回 df_slice() 错误?