In R, split a dataframe so subset dataframes contain last row of previous dataframe and first row of subsequent dataframe

Tedward

There are many answers for how to split a dataframe, for example How to split a data frame?

However, I'd like to split a dataframe so that the smaller dataframes contain the last row of the previous dataframe and the first row of the following dataframe.

Here's an example

n <- 1:9
group <- rep(c("a","b","c"), each = 3)
data.frame(n = n, group)

  n  group
1 1     a
2 2     a
3 3     a
4 4     b
5 5     b
6 6     b
7 7     c
8 8     c
9 9     c

I'd like the output to look like:

 d1 <- data.frame(n = 1:4, group = c(rep("a",3),"b"))
 d2 <- data.frame(n = 3:7, group = c("a",rep("b",3),"c"))
 d3 <- data.frame(n = 6:9, group = c("b",rep("c",3)))
 d <- list(d1, d2, d3)
 d

[[1]]
  n group
1 1     a
2 2     a
3 3     a
4 4     b

[[2]]
  n group
1 3     a
2 4     b
3 5     b
4 6     b
5 7     c

[[3]]
  n group
1 6     b
2 7     c
3 8     c
4 9     c

What is an efficient way to accomplish this task?

G. Grothendieck

Suppose DF is the original data.frame, the one with columns n and group. Let n be the number of rows in DF. Now define a function extract which given a sequence of indexes ix enlarges it to include the one prior to the first and after the last and then returns those rows of DF. Now that we have defined extract, split the vector 1, ..., n by group and apply extract to each component of the split.

n <- nrow(DF)
extract <- function(ix) DF[seq(max(1, min(ix) - 1), min(n, max(ix) + 1)), ]
lapply(split(seq_len(n), DF$group), extract)

$a
  n group
1 1     a
2 2     a
3 3     a
4 4     b

$b
  n group
3 3     a
4 4     b
5 5     b
6 6     b
7 7     c

$c
  n group
6 6     b
7 7     c
8 8     c
9 9     c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas: Divide DataFrame last row by first row

From Dev

R: split concatenated dataframe by row

From Dev

Subsequent row summing in dataframe object

From Dev

DataFrame row and previous row calculation

From Dev

Set first and last row of a column in a dataframe

From Dev

Extract first and last row of a dataframe in pandas

From Dev

R split each row of a dataframe into two rows

From Dev

R split each row of a dataframe into two rows

From Dev

R delete last row in dataframe for each group

From Dev

Change an element in the last row of a DataFrame

From Java

How to subset R dataframe by starting and ending row value

From Dev

Add each column with last value of last column of the row in dataframe R

From Java

Get the first and last row values by unique id in a dataframe

From Dev

Python pandas DataFrame from first and last row of csv

From Dev

Get the first cell from last row pandas dataframe

From Dev

pandas dataframe compare first and last row from each group

From Dev

Delete rows with specific values by keeping first and last row of dataframe

From Dev

Find value that is a subset to a row in Pandas dataframe

From Dev

R divide each column in dataframe by last row value

From Dev

R divide each column in dataframe by last row value

From Java

Comparing previous row values of every column in a dataframe

From Java

How to get previous row with condition in a DataFrame of Pandas

From Dev

Comparing previous row values in Pandas DataFrame

From Dev

Delete rows in dataframe that are all previous to value in row

From Dev

Spark Dataframe access of previous calculated row

From Dev

Split dataframe to several dataframes

From Dev

min for each row with dataframe in R

From Dev

Read data into dataframe by row with R

From Dev

R: dataframe Selecting the maximum row per ID based on first timestamp

Related Related

  1. 1

    pandas: Divide DataFrame last row by first row

  2. 2

    R: split concatenated dataframe by row

  3. 3

    Subsequent row summing in dataframe object

  4. 4

    DataFrame row and previous row calculation

  5. 5

    Set first and last row of a column in a dataframe

  6. 6

    Extract first and last row of a dataframe in pandas

  7. 7

    R split each row of a dataframe into two rows

  8. 8

    R split each row of a dataframe into two rows

  9. 9

    R delete last row in dataframe for each group

  10. 10

    Change an element in the last row of a DataFrame

  11. 11

    How to subset R dataframe by starting and ending row value

  12. 12

    Add each column with last value of last column of the row in dataframe R

  13. 13

    Get the first and last row values by unique id in a dataframe

  14. 14

    Python pandas DataFrame from first and last row of csv

  15. 15

    Get the first cell from last row pandas dataframe

  16. 16

    pandas dataframe compare first and last row from each group

  17. 17

    Delete rows with specific values by keeping first and last row of dataframe

  18. 18

    Find value that is a subset to a row in Pandas dataframe

  19. 19

    R divide each column in dataframe by last row value

  20. 20

    R divide each column in dataframe by last row value

  21. 21

    Comparing previous row values of every column in a dataframe

  22. 22

    How to get previous row with condition in a DataFrame of Pandas

  23. 23

    Comparing previous row values in Pandas DataFrame

  24. 24

    Delete rows in dataframe that are all previous to value in row

  25. 25

    Spark Dataframe access of previous calculated row

  26. 26

    Split dataframe to several dataframes

  27. 27

    min for each row with dataframe in R

  28. 28

    Read data into dataframe by row with R

  29. 29

    R: dataframe Selecting the maximum row per ID based on first timestamp

HotTag

Archive