gpt4 book ai didi

r - 如何调试R中无效的下标类型 'integer'错误

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

我正在尝试在 R: extract maximum value in vector under certain conditions 中运行代码但我不断收到错误

Error in list(id.2 = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,  : 
invalid subscript type 'integer'

代码如下:
require(dplyr)
dat <- read.table(header = TRUE, text = "id name year job job2 cumu_job2
1 Jane 1980 Worker 0 0
1 Jane 1981 Manager 1 1
1 Jane 1982 Sales 0 0
1 Jane 1983 Sales 0 0
1 Jane 1984 Manager 1 1
1 Jane 1985 Manager 1 2
1 Jane 1986 Boss 0 0
2 Bob 1985 Worker 0 0
2 Bob 1986 Sales 0 0
2 Bob 1987 Manager 1 1
2 Bob 1988 Manager 1 2
2 Bob 1989 Boss 0 0
3 Jill 1989 Worker 0 0
3 Jill 1990 Boss 0 0")

dat %.%
group_by(id) %.%
mutate(
all_jobs = sum(unique(job) %in% c("Sales","Manager","Boss")),
cumu_max = max(cumu_job2)
) %.%
filter(all_jobs == 3, job %in% c("Sales","Boss"))

Source: local data frame [5 x 8]
Groups: id

id name year job job2 cumu_job2 all_jobs cumu_max
1 1 Jane 1982 Sales 0 0 3 2
2 1 Jane 1983 Sales 0 0 3 2
3 1 Jane 1986 Boss 0 0 3 2
4 2 Bob 1986 Sales 0 0 3 2
5 2 Bob 1989 Boss 0 0 3 2

最佳答案

示例代码也适用于我。但是我发现如果我尝试这样做,我可以重现类似的错误:

dat %.%
group_by(dat$id) %.%
mutate(
all_jobs = sum(unique(job) %in% c("Sales","Manager","Boss")),
cumu_max = max(cumu_job2)
) %.%
filter(all_jobs == 3, job %in% c("Sales","Boss"))

也就是说,如果我输入“group_by(dat$id)”而不是“group_by(id)”

关于r - 如何调试R中无效的下标类型 'integer'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21598295/

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