DW1310
DW1310

Reputation: 323

Long to wide format in R based based on one column

I am struggling with probably quite a simple exercise, I have a dataframe consisting of two columns. One is a unique identifier and one is an image scan number. One unique identifier can have multiple image scan numbers and I am looking to reshape the data so all the scan numbers that relate to an individual ID are in one columns.

So going from:

to

For example Long Format I would like to reshape this to a wide format based upon the ID,

to: Wide Format

Many thanks

Upvotes: 0

Views: 677

Answers (2)

akrun
akrun

Reputation: 887078

We can use dcast from data.table

library(data.table)
dcast(setDT(df), ID ~ paste0("Scan_Number", rowid(ID)), value.var = 'Scan_Number')
#   ID Scan_Number1 Scan_Number2
#1:  1          E43          E56
#2:  2          E65          E98

data

df <-  structure(list(ID = c(1L, 1L, 2L, 2L), Scan_Number = c("E43", 
"E56", "E65", "E98")), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 0

Duck
Duck

Reputation: 39595

Try this tidyverse approach. You data is in long format, so you have to reshape to wide. Here the code:

library(tidyverse)
#Code
df %>% group_by(ID) %>% mutate(Var=paste0('Scan_Number_',1:n())) %>%
  pivot_wider(names_from = Var,values_from=Scan_Number)

Output:

# A tibble: 2 x 3
# Groups:   ID [2]
     ID Scan_Number_1 Scan_Number_2
  <int> <chr>         <chr>        
1     1 E43           E56          
2     2 E65           E98   

Some data used:

#Data
df <- structure(list(ID = c(1L, 1L, 2L, 2L), Scan_Number = c("E43", 
"E56", "E65", "E98")), class = "data.frame", row.names = c(NA, 
-4L))

Upvotes: 1

Related Questions