0

I just recently started learning R and I ran into an issue when trying to use the gather function. How can I use the gather() fxn to produce a data frame with more than two columns when I transform it from wide to long.

For example this code:

long_iris <- gather(head(iris, 3), "Col_1", "Col_2", "Col_3", "Col_4", "Col_5",1:5)

Outputs this error message:

> long_iris <- gather(head(iris, 3), "Col_1", "Col_2", "Col_3", "Col_4", "Col_5",1:5)
Error: Can't subset columns that don't exist.
x Column `Col_3` doesn't exist.
Run `rlang::last_error()` to see where the error occurred.
r2evans
  • 141,215
  • 6
  • 77
  • 149
Hamm
  • 11
  • 1
  • 1
  • 3
    (1) `gather` is a bit out-dated for `tidyr`; while still supported, I suggest it would be better (for functionality, features, and future supportability) to shift to `tidyr::pivot_wider`. (2) Please show your expected output. Thanks! – r2evans Jun 02 '21 at 00:47
  • 1
    (And oops ... `pivot_longer`, not `_wider`.) – r2evans Jun 02 '21 at 01:04
  • 1
    Refer - https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format – Ronak Shah Jun 02 '21 at 02:21

2 Answers2

2

Using the preferred tidyr::pivot_longer:

tidyr::pivot_longer(head(iris, 3), -Species)
# # A tibble: 12 x 3
#    Species name         value
#    <fct>   <chr>        <dbl>
#  1 setosa  Sepal.Length   5.1
#  2 setosa  Sepal.Width    3.5
#  3 setosa  Petal.Length   1.4
#  4 setosa  Petal.Width    0.2
#  5 setosa  Sepal.Length   4.9
#  6 setosa  Sepal.Width    3  
#  7 setosa  Petal.Length   1.4
#  8 setosa  Petal.Width    0.2
#  9 setosa  Sepal.Length   4.7
# 10 setosa  Sepal.Width    3.2
# 11 setosa  Petal.Length   1.3
# 12 setosa  Petal.Width    0.2
r2evans
  • 141,215
  • 6
  • 77
  • 149
1

Edit: Included @onyambu suggestion:

library(tidyverse)
    
    gather(head(iris, 3), 'key', 'value', -Species)
#>    Species          key value
#> 1   setosa Sepal.Length   5.1
#> 2   setosa Sepal.Length   4.9
#> 3   setosa Sepal.Length   4.7
#> 4   setosa  Sepal.Width   3.5
#> 5   setosa  Sepal.Width   3.0
#> 6   setosa  Sepal.Width   3.2
#> 7   setosa Petal.Length   1.4
#> 8   setosa Petal.Length   1.4
#> 9   setosa Petal.Length   1.3
#> 10  setosa  Petal.Width   0.2
#> 11  setosa  Petal.Width   0.2
#> 12  setosa  Petal.Width   0.2

Created on 2021-06-01 by the reprex package (v2.0.0)

library(tidyverse)
gather(head(iris, 3), "Sepal.Length","Sepal.Width", "Petal.Length", "Petal.Width", key = 'Sepal_or_Petal', value = 'value' )
#>    Species Sepal_or_Petal value
#> 1   setosa   Sepal.Length   5.1
#> 2   setosa   Sepal.Length   4.9
#> 3   setosa   Sepal.Length   4.7
#> 4   setosa    Sepal.Width   3.5
#> 5   setosa    Sepal.Width   3.0
#> 6   setosa    Sepal.Width   3.2
#> 7   setosa   Petal.Length   1.4
#> 8   setosa   Petal.Length   1.4
#> 9   setosa   Petal.Length   1.3
#> 10  setosa    Petal.Width   0.2
#> 11  setosa    Petal.Width   0.2
#> 12  setosa    Petal.Width   0.2

Created on 2021-06-01 by the reprex package (v2.0.0)

Other way:

library(tidyverse)

gather(head(iris, 3), starts_with('Sepal') | starts_with('Petal') , key = 'Sepal_or_Petal', value = 'value' )
#>    Species Sepal_or_Petal value
#> 1   setosa   Sepal.Length   5.1
#> 2   setosa   Sepal.Length   4.9
#> 3   setosa   Sepal.Length   4.7
#> 4   setosa    Sepal.Width   3.5
#> 5   setosa    Sepal.Width   3.0
#> 6   setosa    Sepal.Width   3.2
#> 7   setosa   Petal.Length   1.4
#> 8   setosa   Petal.Length   1.4
#> 9   setosa   Petal.Length   1.3
#> 10  setosa    Petal.Width   0.2
#> 11  setosa    Petal.Width   0.2
#> 12  setosa    Petal.Width   0.2

Created on 2021-06-01 by the reprex package (v2.0.0)

jpdugo17
  • 6,816
  • 2
  • 11
  • 23
  • 1
    instead of listing all the variables, list the ones you dont want eg `gather(head(iris, 3), 'key', 'value', -Species)` – Onyambu Jun 02 '21 at 01:34