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.
rnorouzian
  • 7,397
  • 5
  • 27
  • 72

1 Answers1

1

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())
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • As you must be aware one column can consist of data of only one type. You cannot have mix of integer/character/logical values in one column. So in the first case (`data %>% pivot_longer(cols = -id)`), `id` column is still numeric and rest of them are characters. For the second case, since we are trying combine all the values in 1 column and 1 column can have only 1 type so we turn `id` column to character as well. – Ronak Shah Sep 16 '20 at 07:15
  • Not when it comes in long format. (`data %>% mutate(across(.fns = as.character)) %>% pivot_longer(cols = everything())`). `value` column has integers + character values. So everything turns into character. – Ronak Shah Sep 16 '20 at 07:18