gpt4 book ai didi

r - 与 dcast 相反

转载 作者:行者123 更新时间:2023-12-02 04:59:36 26 4
gpt4 key购买 nike

<分区>

想法是将频率表转换为 geom_density 可以处理的内容 (ggplot2)。

从频率表开始

> dat <- data.frame(x = c("a", "a", "b", "b", "b"), y = c("c", "c", "d", "d", "d"))
> dat
x y
1 a c
2 a c
3 b d
4 b d
5 b d

使用dcast制作频率表

> library(reshape2)
> dat2 <- dcast(dat, x + y ~ ., fun.aggregate = length)
> dat2
x y count
1 a c 2
2 b d 3

这怎么能逆转呢? melt 似乎不是答案:

> colnames(dat2) <- c("x", "y", "count")
> melt(dat2, measure.vars = "count")
x y variable value
1 a c count 2
2 b d count 3

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com