Reputation: 2356
Time ago I've made this functions:
vap.vector <- function (x, y) {
y[is.infinite(y)] <- NA
y[y == 0] <- NA
logtasa <- log(y)
datos <- data.frame(x, logtasa)
datos <- na.omit(datos)
lmdatos <- lm(logtasa ~ x, data = datos)
es <- qt(0.975, lmdatos$df.residual) * summary(lmdatos)$coefficients[2, 2]
vap <- summary(lmdatos)$coefficients[2, 1]
vec <- c(vap = vap, es = es, lwr = vap - es, upr = vap + es)
round((1 - exp(vec)) * -100, 2)
}
vap_ <- function (df, x, y) {
x = lazyeval::lazy_eval(x, df)
y = lazyeval::lazy_eval(y, df)
vap.vector(x, y)
}
vap <- function (df, x, y)
vap_(df, lazyeval::lazy(x), lazyeval::lazy(y))
I have this dataset
https://drive.google.com/file/d/0Bw2XRcfksYZuX1R5bG9oZzdhZ2M/view?usp=sharing
and I wanna do:
tbl_df %>% group_by(code, sex) %>% vap(year, values)
or
tbl_df %>% group_by(code, sex) %>%
summarise(vap = vap.vector(year, values)[1],
lwr = vap.vector(year, values)[3],
upr = vap.vector(year, values)[4]
)
and as result a data.frame with code, sex, and vap, lwr and upr columns.
but I cannot do it. I the second aproach I get:
Error: 0 (non-NA) cases
There is a simply way to do it or I need to be @hadley
I'm working on Windows with R 3.2.2 and dplyr 0.4.3. Thanks in advance...
Upvotes: 1
Views: 268
Reputation: 1481
You just need do use do
:
vap.vector <- function (x, y) {
y[is.infinite(y)] <- NA
y[y == 0] <- NA
logtasa <- log(y)
datos <- data.frame(x, logtasa)
datos <- na.omit(datos)
lmdatos <- lm(logtasa ~ x, data = datos)
es <- qt(0.975, lmdatos$df.residual) * summary(lmdatos)$coefficients[2, 2]
vap <- summary(lmdatos)$coefficients[2, 1]
vec <- c(vap = vap, es = es, lwr = vap - es, upr = vap + es)
round((1 - exp(vec)) * -100, 2)
}
Data <- read.table("dataset.csv", sep = ",", header = TRUE)
library(dplyr)
Results <-
Data %>%
group_by(code, sex) %>%
do(as.data.frame(t(vap.vector(.$year, .$values)[-2])))
which produces:
> head(as.data.frame(Results))
code sex vap lwr upr
1 0 F 2.10 1.07 3.13
2 0 M 0.92 0.00 1.85
3 2 F 3.82 2.91 4.73
4 2 M 2.72 1.95 3.49
5 4 F 3.23 2.25 4.21
6 4 M 3.76 2.70 4.83
Upvotes: 1