mardi 8 décembre 2015

Sample k rows from data frame and create identifier for which rows are selected in R

My question relates to this famous question. I want to select rows from a data frame

k=3
df = data.frame(matrix(rnorm(20), nrow=10))
selected.rows = df[sample(nrow(df), k), ]

But instead of a new data.frame with k rows, I want a column vector in df indicating which rows are selected. I obviously cannot use

sample(c(0,1),nrow(df),TRUE)

Because then k cannot be specified. But something along these lines should give a solution.




Aucun commentaire:

Enregistrer un commentaire