I'm starting with a data frame with 5 columns: one treatment column, T_type, and four outcome variable columns, A, B, C and D. I'm trying to stack the outcome variables so I end up with one column for values, another with the names of the four outcome variables and then a column with the treatment names repeated down along the stacked columns. It's what's shown in the R help page for pivot_longer in the relig_income example and pretty much what Jason was trying to do here: dplyr `pivot_longer()` object not found but it's right there?
I get the same sort of error Jason was getting with pivot_longer and have no idea why. Here's what's happening.
dd <- as.data.frame(matrix(rpois(32, 4), nrow = 8))
names(dd) <- LETTERS[1:4]
dd <- data.frame(dd, T_type = rep(c("M", "P"), each = 4))
dd
A B C D T_type
1 3 5 5 4 M
2 7 5 2 2 M
3 2 3 3 10 M
4 3 3 2 3 M
5 8 3 4 3 P
6 4 4 5 1 P
7 6 4 2 6 P
8 9 4 3 6 P
So now I try pivot_longer.
dd %>% pivot_longer(-T_type, cols = A:D, names_to = "response", values_to = "y_obs")
Error in build_longer_spec(data, !!cols, names_to = names_to, values_to = values_to, :
object 'T_type' not found
Re-arranging the columns in dd so T_type is before columns A to D doesn't help.
I'd be grateful if someone could tell me what's going on here and how I can get pivot_longer to do the job.