tidyr "pivot_longer": repeat column gives object not found error

Viewed 490

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.

2 Answers

You need to eliminate T_type from pivot_longer because the first argument of this function is the dataset (which can be omitted in you are in a %>% pipeline)

dd %>% pivot_longer(cols = A:D, names_to = "response", values_to = "y_obs")

Output

# A tibble: 32 x 3
#    T_type response y_obs
#    <chr>  <chr>    <int>
#  1 M      A            7
#  2 M      B            4
#  3 M      C            4
#  4 M      D            3
#  5 M      A            8
#  6 M      B            3
#  7 M      C            5
#  8 M      D            3
#  9 M      A            4
# 10 M      B            6
# ... with 22 more rows

Try this :

dd %>% 
  gather("response", "y_obs", -T_type)

Or :

dd %>% pivot_longer(names_to = "response", values_to = "y_obs", -T_type)

Or :

dd %>% pivot_longer(names_to = "response", values_to = "y_obs", A:D)

Youy specify the range of cols : A to D, so you will not find T_type

Related