我具有以下功能:
library(dplyr)
apply_fun <- function(data) {
data %>%
group_by(Type) %>%
summarise(across(starts_with('x'), list(median = median,
first_quartile = ~quantile(., 0.25),
third_quartile = ~quantile(., 0.75))))
}
对于结构如下的数据集,它为我提供了每个列,每个“类型”的数据集的中位数,第一和第三四分位数:
Type x1 x2 x3 ...
1: type1 1.54 1.48 1.88
2: type2 1.46 1.99 1.48
3: type1 2.01 1.02 1.03
...
该函数产生的数据如下:
x1_median x1_first_quartile x1_third_quartile x2_first...
type1 1.505 1.122 ...
type2 1.488 1.026 ...
... ...
我还有其他以相同方式构造的数据集。我想在函数中包含每种类型相对于x值的中位数和四分位数的图。 x值是列名中的数字,不一定从1开始。 类似于以下内容的情节:
我为特定情况制作了这张图。
plot(some_vector, unlist(FactorMedians[1, 2500]), type = "l", las = "1",
main = "Median values by Factor")
lines(some_vector, unlist(FactorMedians[2, 2500]), type = "l")
lines(some_vector, unlist(FactorMedians[3, 2500]), type = "l")
lines(some_vector, unlist(FactorMedians[4, 2500]), type = "l")
lines(some_vector, unlist(FactorMedians[5, 2500]), type = "l")
我不知道如何找到一个通用的表格。
因子中位数的计算如下:
library(dplyr)
FactorMedians = mydata %>%
group_by(Type) %>%
summarise(across(starts_with('x'),
median, probs = quant0, na.rm = TRUE))
如果我想要的内容不清楚,也许请看我先前的问题How can I create a function that computes the median and quartiles for each column of data, for each factor of data?
答案 0 :(得分:1)
您的意思是这样的吗?
# make dummy data
x <- 1:20
y <- data.frame(a=rnorm(20), b=rnorm(20), c=rnorm(20))
# prepare plot area
plot(NULL, xlim = range(x), ylim = range(y), xlab = "X", ylab = "Y")
# sapply ~= 'foreach', seq_len = sequence from 1 to n
sapply(seq_len(ncol(y)), function(i){
lines(x, y[,i], type = "l", col = i)
})
收益