Selecting rows, create separate datasets according to rows

Viewed 36

I have a dataset that looks like this.

# A tibble: 6 x 39
  AssignmentId WorkerId Input.image_url Answer.action1.… Answer.action2.… Answer.comments Answer.gaze1.ga… Answer.gaze2.ga…
  <chr>        <chr>    <chr>                      <int>            <int>           <int>            <int>            <int>
1 30MVJZJNHNI… AW13W4W… 450.png                        0                0              NA                0                0
2 3BDCF01OGYZ… A253S5S… 450.png                        0                1              NA                1                0
3 3U0SRXB7CEA… A3GIXRS… 450.png                        1                1              NA                1                0
4 3D4CH1LGEBY… A1SK0GV… 1141.png                       1                0              NA                1                0
5 3MTMREQS4WN… AK0R311… 1141.png                       1                0               0                0                1
6 3WMOAN2SRC2… A11FQTH… 1141.png                       1                0              NA                0                0

Basically, what I'm trying to do is grab rows according to the Input.image.url variable and create separate datasets. Select first rows of each unique Input.image.url variable and combining them. So there will be three datasets with rows like these.

> head(first_half_2)
# A tibble: 6 x 39
  AssignmentId WorkerId Input.image_url Answer.action1.… Answer.action2.… Answer.comments Answer.gaze1.ga… Answer.gaze2.ga…
  <chr>        <chr>    <chr>                      <int>            <int>           <int>            <int>            <int>
1 3UOUJI6MTEJ… A1OYE3D… 450.png                        1                0              NA                1                0
2 3OVHNO1VE76… ALWYZOT… 1141.png                       0                1             114                0                1
3 3WI0P0II62X… A2BSMM8… 1039.png                       0                1              25                1                0
4 3UXUOQ9OKF2… A1RK4PG… 1067.png                       0                0              NA                0                0
5 3JPSL1DZ5T4… A1RK4PG… 111.png                        0                0              NA                0                0
6 35LDD5557B9… A253S5S… 1141.png                       1                0              NA                1                0

Thanks in advance!!

1 Answers

May be we need to create a sequence column by 'Input.image_url' and split based on it to a list of data.frames

grp <- with(first_half_2, ave(seq_along(Input.image_url), 
      Input.image_url, FUN = seq_along))
lst1 <- split(first_half_2, grp)

Or more compactly with rowid from data.table

library(data.table)
split(first_half_2, rowid(first_half_2$Input.image_url))
Related