The following code can be copy/pasted on a R
script:
select_dataframe_rows = function(ds, sel) {
cnames = colnames(ds)
rnames = rownames(ds)
ds = data.frame(ds[sel,])
colnames(ds) = cnames
rownames(ds) = rnames[sel]
return (ds)
}
df = data.frame(value = c(3,6,2,5,8,9,1,4,7))
rownames(df) = sprintf("Entry %02d", 1:nrow(df))
printf("I have the following dataframe:\n")
df
printf("This is a subset of it:\n")
df2 = select_dataframe_rows(df, c(2, 5))
df2
This is the output:
# I have the following dataframe:
# value
# Entry 01 3
# Entry 02 6
# Entry 03 2
# Entry 04 5
# Entry 05 8
# Entry 06 9
# Entry 07 1
# Entry 08 4
# Entry 09 7
# This is a subset of it:
# value
# Entry 02 6
# Entry 05 8
My question is: can be the content inside function: sort_dataframe_by_varname()
be simplified?
I think that implementation is a bit tricky and for sure should be a better implementation for this common use case.
Be aware that one requirement is to keep the row names.