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

0

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?

library(tidyverse)

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

7

I think what you need is :

library(dplyr)
library(tidyr)

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?

JD

Please login first before posting an answer.