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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -