gpt4 book ai didi

r - 如何在计算每组中的前 3 个元素时避免 "operations are possible only for numeric, logical or complex types"

转载 作者:行者123 更新时间:2023-12-04 17:35:17 25 4
gpt4 key购买 nike

我有一个包含 4 列的矩阵 (AOD_median)。我想找到每年的 3 个最大数据元素(按 AOD)并确定与这些元素相关的月份。这是我的数据:

date      month year     AOD
1-Mar-00 3 2000 0.226
1-Apr-00 4 2000 0.454
1-May-00 5 2000 0.328
1-Jun-00 6 2000 0.314
1-Jul-00 7 2000 0.354
1-Aug-00 8 2000 0.282
1-Sep-00 9 2000 0.278
1-Oct-00 10 2000 0.183
1-Nov-00 11 2000 0.173
1-Dec-00 12 2000 0.21
1-Jan-01 1 2001 0.171
1-Feb-01 2 2001 0.281
1-Mar-01 3 2001 0.241
1-Apr-01 4 2001 0.269
1-May-01 5 2001 0.292
1-Jun-01 6 2001 0.222
1-Jul-01 7 2001 0.322
1-Aug-01 8 2001 0.268
1-Sep-01 9 2001 0.276
1-Oct-01 10 2001 0.169
1-Nov-01 11 2001 0.16
1-Dec-01 12 2001 0.15

这是 dput 文本:

structure(list(X1 = c("1-Mar-00", "1-Apr-00", "1-May-00", "1-Jun-00", 
"1-Jul-00", "1-Aug-00", "1-Sep-00", "1-Oct-00", "1-Nov-00", "1-Dec-00",
"1-Jan-01", "1-Feb-01", "1-Mar-01", "1-Apr-01", "1-May-01", "1-Jun-01",
"1-Jul-01", "1-Aug-01", "1-Sep-01", "1-Oct-01", "1-Nov-01", "1-Dec-01"
), X2 = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12), X3 = c(2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001, 2001,
2001, 2001, 2001, 2001, 2001), X4 = c(0.226, 0.454, 0.328, 0.314,
0.354, 0.282, 0.278, 0.183, 0.173, 0.21, 0.171, 0.281, 0.241,
0.269, 0.292, 0.222, 0.322, 0.268, 0.276, 0.169, 0.16, 0.15)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -22L))

我尝试使用以下代码执行此操作:

for(i in 2000:2001) {(d <- as.matrix(AOD_median[which(AOD_median[,3]==i),]))&
(order_AOD <- d[order(d[,4], decreasing = TRUE)])&
print(order_AOD[1:3,2])}

我希望得到这样的结果:

"4" "7" "5" 
"7" "2" "9"

相反,我得到了这个错误:

Error in (d <- as.matrix(AOD_median[which(AOD_median[, 3] == i), ])) &  : 
operations are possible only for numeric, logical or complex types

最佳答案

具体的错误是因为你使用&来分隔代码行;这不起作用,因为这是 R 中的逻辑运算符。您可以使用 ; 代替或换行符来分隔行。

但是,退后一步,您正在尝试计算数据集中每年的前 3 个月,如 AOD 字段所衡量的那样。由于您使用的是 dplyr,因此可以通过以下方式更顺利地完成此操作:

AOD_median %>%
arrange(-AOD) %>%
group_by(year) %>%
top_n(3, AOD) %>%
select(year, month)
# A tibble: 6 x 2
# Groups: year [2]
# year month
# <dbl> <dbl>
# 1 2000 4
# 2 2000 7
# 3 2000 5
# 4 2001 7
# 5 2001 5
# 6 2001 2

如果您不介意三个月(按 AOD)乱序,您可以删除 arrange(-AOD​​) 行。

数据:

AOD_median <- structure(list(date = c("1-Mar-00", "1-Apr-00", "1-May-00", "1-Jun-00", "1-Jul-00", "1-Aug-00", "1-Sep-00", "1-Oct-00", "1-Nov-00", "1-Dec-00", "1-Jan-01", "1-Feb-01", "1-Mar-01", "1-Apr-01", "1-May-01", "1-Jun-01",  "1-Jul-01", "1-Aug-01", "1-Sep-01", "1-Oct-01", "1-Nov-01", "1-Dec-01"), month = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6,7, 8, 9, 10, 11, 12), year = c(2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2001, 2001, 2001, 2001, 2001, 2001, 2001,2001, 2001, 2001, 2001, 2001), AOD = c(0.226, 0.454, 0.328, 0.314,0.354, 0.282, 0.278, 0.183, 0.173, 0.21, 0.171, 0.281, 0.241, 0.269, 0.292, 0.222, 0.322, 0.268, 0.276, 0.169, 0.16, 0.15)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -22L))

关于r - 如何在计算每组中的前 3 个元素时避免 "operations are possible only for numeric, logical or complex types",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56939456/

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