Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Column is lost when dataframe is converted from wide to long format

I have the dataframe below

df<-structure(list(Participant = c("Valentin", "Valentin", "Valentin", 
"Valentin", "Valentin", "Valentin", "Valentin", "Valentin", "Valentin", 
"Valentin"), Number = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Level = c("A1", 
"A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1", "A1"), `Spanish use` = c(42, 
42, 42, 42, 42, 42, 42, 42, 42, 42), Block = c(1, 2, 3, 4, 5, 
1, 2, 3, 4, 5), `Place of articulation` = c("Bilabial", "Bilabial", 
"Bilabial", "Bilabial", "Bilabial", "Dental", "Dental", "Dental", 
"Dental", "Dental"), `VOT 0` = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0
), `VOT 5` = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), `VOT 10` = c(1, 
0, 0, 0, 0, 0, 0, 0, 0, 0), `VOT 15` = c(1, 0, 0, 0, 0, 0, 0, 
0, 0, 0), `VOT 20` = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0), `VOT -10` = c(1, 
1, 1, 1, 1, 0, 1, 1, 1, 1), `VOT -20` = c(1, 1, 1, 1, 1, 0, 1, 
1, 1, 1), `VOT -30` = c(1, 1, 1, 1, 1, 0, 1, 1, 1, 1), `VOT -40` = c(1, 
1, 1, 1, 1, 0, 1, 1, 1, 1), `VOT -50` = c(1, 1, 1, 1, 1, 0, 1, 
1, 1, 1), `VOT -60` = c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

and with:

df_long <- df %>% 
  pivot_longer(cols = starts_with("VOT"), names_to = "VOT", values_to = "response")

I convert it to:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# A tibble: 6 × 6
  Participant Number Level `Place of articulation` VOT     response
  <chr>        <dbl> <chr> <chr>                   <chr>      <dbl>
1 Valentin         1 A1    Bilabial                VOT 0          1
2 Valentin         1 A1    Bilabial                VOT 5          1
3 Valentin         1 A1    Bilabial                VOT 10         1
4 Valentin         1 A1    Bilabial                VOT 15         1
5 Valentin         1 A1    Bilabial                VOT 20         1
6 Valentin         1 A1    Bilabial                VOT -10        1

but I want also to include the Block column in the new data

>Solution :

i dont see any issue with your code as the same code produced the below result where i see Block column

library(tidyverse)

df_long <- df %>% 
  pivot_longer(cols = starts_with("VOT"), names_to = "VOT", values_to = "response")
# A tibble: 110 × 8
   Participant Number Level `Spanish use` Block `Place of articulation` VOT     response
   <chr>        <dbl> <chr>         <dbl> <dbl> <chr>                   <chr>      <dbl>
 1 Valentin         1 A1               42     1 Bilabial                VOT 0          1
 2 Valentin         1 A1               42     1 Bilabial                VOT 5          1
 3 Valentin         1 A1               42     1 Bilabial                VOT 10         1
 4 Valentin         1 A1               42     1 Bilabial                VOT 15         1
 5 Valentin         1 A1               42     1 Bilabial                VOT 20         1
 6 Valentin         1 A1               42     1 Bilabial                VOT -10        1
 7 Valentin         1 A1               42     1 Bilabial                VOT -20        1
 8 Valentin         1 A1               42     1 Bilabial                VOT -30        1
 9 Valentin         1 A1               42     1 Bilabial                VOT -40        1
10 Valentin         1 A1               42     1 Bilabial                VOT -50        1
# … with 100 more rows
# ℹ Use `print(n = ...)` to see more rows
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading