gpt4 book ai didi

r - R中的分组xtabs

转载 作者:行者123 更新时间:2023-12-01 21:36:05 25 4
gpt4 key购买 nike

我想为不同的站准备xtabs。但它给了我跨站的整体表格。我使用了以下代码

library(tidyverse)

df %>% group_by(Station) %>%
xtabs( ~ Observed + Forecasted, data = .)

这是给了我

#>        Forecasted
#>Observed 1 3 4 5 9
#> 1 132 5 31 31 3
#> 3 16 0 6 13 7
#> 4 6 0 13 23 8
#> 5 4 0 16 33 15
#> 9 0 0 0 2 0

但我想像这样输出站位

Aizawl
#> Forecasted
#>Observed 1 3 4 5 9
#> 1 132 5 31 31 3
#> 3 16 0 6 13 7
#> 4 6 0 13 23 8
#> 5 4 0 16 33 15
#> 9 0 0 0 2 0

Serchhip
#> Forecasted
#>Observed 1 3 4 5 9
#> 1 132 5 31 31 3
#> 3 16 0 6 13 7
#> 4 6 0 13 23 8
#> 5 4 0 16 33 15
#> 9 0 0 0 2 0

然后我想将输出导出到 .csv 文件中。

数据

df = structure(list(Station = c("Aizawl", "Aizawl", "Aizawl", "Aizawl", 
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl", "Aizawl",
"Aizawl", "Aizawl", "Aizawl", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchhip",
"Serchhip", "Serchhip", "Serchhip", "Serchhip", "Serchh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 4, 3, 5, 5, 5, 5, 5, 4, 5, 5, 5, 4,
5, 4, 4, 5, 5, 4, 4, 5, 4, 1, 4, 4, 5, 5, 4, 5, 4, 5, 4,
5, 5, 5, 1, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 9)), row.names = c(NA,
364L), class = "data.frame")

最佳答案

如果你想对xtabs(三维数组)而不是listdata.frame进行操作,那么把分层变量,即 Station,到 xtabs() 中公式的最后位置。

res <- xtabs( ~ Observed + Forecasted + Station, df)
res

# , , Station = Aizawl
#
# Forecasted
# Observed 1 3 4 5 9
# 1 56 2 13 13 1
# 3 12 0 4 8 3
# 4 4 0 9 11 4
# 5 3 0 7 23 9
# 9 0 0 0 0 0
#
# , , Station = Serchhip
#
# Forecasted
# Observed 1 3 4 5 9
# 1 76 3 18 18 2
# 3 4 0 2 5 4
# 4 2 0 4 12 4
# 5 1 0 9 10 6
# 9 0 0 0 2 0

class(res)
# [1] "xtabs" "table"

将数组打印到 2 个单独的 csv 文件:

lapply(dimnames(res)$Station, function(x) write.csv(res[,,x], paste0("table_", x, ".csv")))

将数组转换为 data.frame 格式:

library(tidyr)

res %>%
as.data.frame() %>%
pivot_wider(names_from = Forecasted, values_from = Freq)

# # A tibble: 10 x 7
# Observed Station `1` `3` `4` `5` `9`
# <fct> <fct> <int> <int> <int> <int> <int>
# 1 1 Aizawl 56 2 13 13 1
# 2 3 Aizawl 12 0 4 8 3
# 3 4 Aizawl 4 0 9 11 4
# 4 5 Aizawl 3 0 7 23 9
# 5 9 Aizawl 0 0 0 0 0
# 6 1 Serchhip 76 3 18 18 2
# 7 3 Serchhip 4 0 2 5 4
# 8 4 Serchhip 2 0 4 12 4
# 9 5 Serchhip 1 0 9 10 6
# 10 9 Serchhip 0 0 0 2 0

关于r - R中的分组xtabs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61968797/

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