0

I would like to keep a certain number of rows per variable: For example assuming I have the following data set (the actual data set is rather large):

part<-c(1,1,1,2,2,2,3,3)
choice<-c(6,7,15,42,1,1,4,5)
data<-cbind(part,choice)
des_data<-data[c(1,2,4,5,7,8),]

      part choice
[1,]    1      6
[2,]    1      7
[3,]    1     15
[4,]    2     42
[5,]    2      1
[6,]    2      1
[7,]    3      4
[8,]    3      5

I would like to keep only the first 2 observations per "part"(icipant).

So that the final data looks like this:

      part choice
[1,]    1      6
[2,]    1      7
[3,]    2     42
[4,]    2      1
[5,]    3      4
[6,]    3      5

How can I do it?

Thanks!

YefR
  • 369
  • 3
  • 12

2 Answers2

1
library(dplyr)
data %>%
  as.data.frame %>% #(your example code creates a matrix)
  group_by(part) %>%
  slice(1:2)
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
0

An option with data.table

library(data.table)
as.data.table(data)[, .SD[1:2], part]
#    part choice
#1:    1      6
#2:    1      7
#3:    2     42
#4:    2      1
#5:    3      4
#6:    3      5
akrun
  • 874,273
  • 37
  • 540
  • 662