how to debug invalid subscript type 'integer' error in R -
i'm trying run code in r: extract maximum value in vector under conditions keep getting error
error in list(id.2 = c(3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, 3l, : invalid subscript type 'integer'
the code following:
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
the sample code works me, too. have found can repro similar error if attempt this:
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"))
that is, if type "group_by(dat$id)" instead of "group_by(id)"
Comments
Post a Comment