简体   繁体   中英

R- Specific merging of rows in a dataframe within unique groups

I have a huge data frame in R like the following:

df <- data.frame("ITEM" = c(1,1,1,2,2,3,3,3,3,4),
                 "ID" = c("A","B","C","D","E","F","G","A","B","C"),
                 "Score" = c(7,8,7,3,5,4,6,9,10,5), 
                 "Date" = = c("1/1/2018","1/3/2018","1/6/2018","1/7/2017","1/10/2017","1/1/2003","1/3/2004","1/5/2008","1/7/2010","1/8/2010"))

    ITEM ID Score  Date
 1     1  A     7  1/1/2018
 2     1  B     8  1/3/2018
 3     1  C     7  1/6/2018
 4     2  D     3  1/7/2017
 5     2  E     5  1/10/2017
 6     3  F     4  1/1/2003
 7     3  G     6  1/3/2004
 8     3  A     9  1/5/2008
 9     3  B    10  1/7/2010
10     4  C     5  1/8/2010
11     4  H     8  1/3/2011

The data is already grouped by unique items and in ascending date order. I would like to transpose the data into the following:

    ITEM ID Score  Date     ID_2 Score_2 Date_2
 1     1  A     7  1/1/2018  B     8     1/3/2018   
 2     1  B     8  1/3/2018  C     7     1/6/2018
 4     2  D     3  1/7/2017  E     5     1/10/2017
 6     3  F     4  1/1/2003  G     6     1/3/2004
 7     3  G     6  1/3/2004  A     9     1/5/2008
 8     3  A     9  1/5/2008  B     10    1/7/2010
10     4  C     5  1/8/2010  H     8     1/3/2011

Each item has an owner and is transferred to another person and given a score. Eg Item 1 is held by A who gets a score of 7, then it moves to B who scores 8, then C who scores 7.

I would like to get it in the above format...to merge each row with the above row (but within the item groups) - I tried reshaping the data using dcast from what I know, but you would get ID_3, ID_4 columns as well for some items whereas I only want the columns for ID_2, Score_2 and Date_2.

Any ideas? Thanks.

Based on the expected output, we could split by 'ITEM', cbind the rows with the lag of rows and then convert the list of data.frame to a single data.frame with rbind

out <- do.call(rbind, lapply(split(df, df$ITEM),
                      function(x) cbind(x[-nrow(x), ], x[-1, -1])))
row.names(out) <- NULL
out
#  ITEM ID Score     Date ID Score      Date
#1    1  A     7 1/1/2018  B     8  1/3/2018
#2    1  B     8 1/3/2018  C     7  1/6/2018
#3    2  D     3 1/7/2017  E     5 1/10/2017
#4    3  F     4 1/1/2003  G     6  1/3/2004
#5    3  G     6 1/3/2004  A     9  1/5/2008
#6    3  A     9 1/5/2008  B    10  1/7/2010
#7    4  C     5 1/8/2010  H     8  1/3/2011

Or using tidyverse

library(tidyverse)
df %>% 
   group_by(ITEM) %>% 
   nest %>%
   mutate(data = map(data, ~ bind_cols(.x[-nrow(.x), ], .x[-1, ]))) %>% 
   unnest
# A tibble: 7 x 7
#   ITEM ID    Score Date     ID1   Score1 Date1    
#  <int> <chr> <int> <chr>    <chr>  <int> <chr>    
#1     1 A         7 1/1/2018 B          8 1/3/2018 
#2     1 B         8 1/3/2018 C          7 1/6/2018 
#3     2 D         3 1/7/2017 E          5 1/10/2017
#4     3 F         4 1/1/2003 G          6 1/3/2004 
#5     3 G         6 1/3/2004 A          9 1/5/2008 
#6     3 A         9 1/5/2008 B         10 1/7/2010 
#7     4 C         5 1/8/2010 H          8 1/3/2011 

data

df <- structure(list(ITEM = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 
4L), ID = c("A", "B", "C", "D", "E", "F", "G", "A", "B", "C", 
"H"), Score = c(7L, 8L, 7L, 3L, 5L, 4L, 6L, 9L, 10L, 5L, 8L), 
Date = c("1/1/2018", "1/3/2018", "1/6/2018", "1/7/2017", 
"1/10/2017", "1/1/2003", "1/3/2004", "1/5/2008", "1/7/2010", 
"1/8/2010", "1/3/2011")), class = "data.frame", row.names = c("1", 
 "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM