Call a variable created within a dplyr pipe - R

78 Views Asked by At

Reproducible data:

'# Truncated Exponential Dist's
lambda_1 <- 1/2
lambda_2 <- 1/10
ff1 <- function(x) pexp(x, lambda_1)
f1.inv <- function(q) qexp(q, lambda_1)
ff2 <- function(x) pexp(x, lambda_2)
f2.inv <- function(q) qexp(q, lambda_2)
a <- 0
n <- 50
x1 <- f1.inv(runif(n))
x1.trunc <- f1.inv(runif(n, ff1(a)))
x2 <- f2.inv(runif(n))
x2.trunc <- f2.inv(runif(n, ff2(a)))
T_Phone <- c(x1.trunc,x2.trunc)
#Normal Data - Equal Variances
Normal_1_Eq <- rnorm(n = 50, mean = 24.6, sd = .95)
Normal_2_Eq <- rnorm(n = 50, mean = 38, sd = 1.05)
Weight <- c(Normal_1_Eq,Normal_2_Eq)

#Normal Data - Unequal Variances
Normal_1_Uneq <- rnorm(n = 50, mean = 24.6, sd = .23)
Normal_2_Uneq <- rnorm(n = 50, mean = 38, sd = 2.95)
Head_Circumference <- c(Normal_1_Uneq, Normal_2_Uneq)
#Poisson
Poisson_1 <- rpois(n = 50, lambda = 4.5)
Poisson_2 <- rpois(n = 50, lambda = 14.5)
Daily_Snacks <- c(Poisson_1,Poisson_2)
#Assign Groups
Group <- rep(c("A","B"), each = 50)
ID <- rep(c(1:50), each = 1, times = 2)
#Group <- sample(Group)

#Set Into Dataframe
df <- data.frame(ID,Group, Weight,Head_Circumference,Daily_Snacks,T_Phone)
df[,c(1:2)] <- lapply(df[,c(1:2)], as.factor)
df[,c(3:6)] <- lapply(df[,c(3:6)], as.numeric)
df <- df %>% janitor::clean_names()`

Question I am attempting to work with the above long-data format and only reshape it into wide format when needed in a dplyr piped chain. I've been successful in doing so with the following (only applied to variable "weight")

df %>% select(id,group, weight) %>% spread(key = "group", value = "weight") 

Now, I want to call the new variables, A and B, and test homogeneity of variances between them:

df %>% select(id,group, weight) %>% spread(key = "group", value = "weight") %>% var.test(.$A,.$B)

However, the only variables I have access to when using the last command (var.test(.$)) are the originally selected variables in my df (e.g., id and group)

If I save this to a new data frame:

t_frame <- df %>% select(id,group, weight) %>% spread(key = "group", value = "weight") var.test(t_frame$A,t_frame$B)

Then everything works. How can I get the newly created A and B variables to populate in var.test within the pipe?

1

There are 1 best solutions below

0
PGSA On BEST ANSWER

Replace your last pipe with:

%>% {var.test(.$A, .$B)}

Without the {} your code passes the whole data frame as the first argument. the curly braces suppress this allowing you to select just the subsets with $.