pivot_longer() works only if id variable is dropped in tidyverse


Below, I'm trying to make my data into long-form but as long as id variable is present in my data, it won't convert to long-form?

Is there a way I can keep id variable and make my data long-form?


data <- read_csv('https://raw.githubusercontent.com/rnorouzian/e/master/surv.csv')
data %>%
  pivot_longer(cols = everything())

# Error: Internal error: Trace data is not square.

answered question

1 Answer


I think what you need is :


data %>% pivot_longer(cols = -id)

If you really want everything() to be in long format all the columns which are going to be in long format should have the same class.

data %>%
  mutate(across(.fns = as.character)) %>%
  pivot_longer(cols = everything())

posted this

Have an answer?


Please login first before posting an answer.