You can do this (really, very minor change to your code):
t[sapply(t,is.numeric) & !grepl("SystemId",names(t))]
As for Q2, I don't have great suggestions. You could try using replicate
to create a list of random stuff, and then mapply
it with a list of as
functions. For example (untested):
df <- as.data.frame(
mapply(
function(fun, col) fun(col),
list(as.character, as.numeric, as.factor, as.logical, as.numeric),
replicate(5, sample(1:10), simplify=F),
SIMPLIFY=F
),
stringsAsFactors=F
)
names(df) <- paste0("V", 1:ncol(df))
sapply(df, class)
# V1 V2 V3 V4 V5
# "character" "numeric" "factor" "logical" "numeric"