r - Creating a function to extract n groups from a dataset -
i searching way extract first n groups of rows dataset.
set.seed(1) data <- data.frame(personid= sample(1:10, 25,replace=true), value=rnorm(25))
if want extract first 4 groups, ie. rows personid=3,4,6,10
personid value 1 3 -0.2894616 2 4 -0.2992151 3 6 -0.4115108 4 10 0.2522234 5 3 -0.8919211 ----------------------
i used unique:
data[data$personid %in% unique(data$personid)[1:4],] personid value 1 3 -0.2894616 2 4 -0.2992151 3 6 -0.4115108 4 10 0.2522234 5 3 -0.8919211 7 10 -1.2375384 11 3 0.8041895 14 4 1.0857694 18 10 -0.2357066 19 4 -0.5428883 21 10 -0.6494716 22 3 0.7267507 25 3 -0.4295131
Comments
Post a Comment