I have the data frame
df1 = data.frame(Site=c(rep("A",5),rep("B",7)),Species=sample(1:100, size=12))
and want to filter the dataframe so that only the first 3 rows for each site are return. ie. returning the dataframe
df2=data.frame(Site=c(rep("A",3),rep("B",3)),Species=c(3,84,45,38,39,22))
Any ideas? Thanks
You can easily select the first three rows using
ddply:Using
min(nrow(df), 3)means that if the site only appears twice, we won’t try and select three rows.