-3

I am currently trying to pivot_wider but am having some trouble. I have done pivot_longer before but going in the reverse seems more difficult.

enter image description here

In this picture I have the data frame on the top and want to get it to look like the bottom. I would post some code but frankly I have not really gotten anywhere :/

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
Gregtt
  • 9
  • 2
  • 4
    Please don't post data as images. Take a look at how to make a [great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ways of showing data. – Martin Gal Aug 25 '21 at 19:57
  • Welcome @Gregtt. You will need to further clarify the difficulty that you're having with pivoting wider in your post. Edit your post to show the code(s) that you're struggling with. Name the function that you're trying to use (and the package its from if it's not from base R). – LC-datascientist Aug 25 '21 at 20:14
  • In all likeliness, your question may have been answered in previous post on StackOverflow. E.g., there are plenty of posts about `reshape`, `tidyr::spread`, and `tidyr::pivot_wider`, as well as tutorial material on the web. I advise trying the tutorial examples first, then look through the most relevant SO posts. If you're still stuck, edit your post to clarify what seems to be the problem. – LC-datascientist Aug 25 '21 at 20:14
  • 2
    pivot_wider() requires a set of columns that uniquely identifies each observation. Your observations are not unique, so id_cols is defaulting to all columns. – Susan Switzer Aug 25 '21 at 20:14

2 Answers2

4

I'm not sure if this is what you have in mind, but I guess there is something wrong about your desired output. One row might be missing there:

library(dplyr)
library(tidyr)

df %>%
  group_by(ID) %>%
  mutate(id = row_number()) %>%
  pivot_wider(names_from = ID, 
              values_from = c(Time, Identifier, Box),
              names_glue = "{.value}{ID}")

# A tibble: 3 x 10
     id Time1 Time2 Time3 Identifier1 Identifier2 Identifier3  Box1  Box2  Box3
  <int> <dbl> <dbl> <dbl> <chr>       <chr>       <chr>       <dbl> <dbl> <dbl>
1     1    25    32    25 A22         A22         B23           123   123   123
2     2    27    23    27 A22         A22         B23           124   124   124
3     3    26    26    19 A22         A22         B23           125   125   125
Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41
1

pivot_wider() requires a set of columns that uniquely identifies each observation. Your observations are not unique, so id_cols is defaulting to all columns.

You will need to create unique identified for each observation. Since your screen shot of your desired outcome is not complete, hopefully this attempt will meet your goal.

df <- data.frame(ID = c(1, 1, 1, 2, 2, 2, 3, 3, 3), 
                 Time = c(25,27, 26, 32, 23, 26, 25, 27, 19), 
                 Identifier = c(rep('A22', 6), rep('B23', 3)), 
                 Box = c(123,124,125, 123,124,125, 123,124,125))

library(tidyverse)

dfWide <- df %>% 
  unite(idTime, ID, Time, sep = '_', remove = F) %>% 
  pivot_wider(names_from = ID,
              values_from = Identifier:Box) 
Susan Switzer
  • 1,531
  • 8
  • 34