对于大型数据集,我想从数据帧中提取所有值为数字的列。
#generate mixed data
dat <- matrix(rnorm(100), nrow = 20)
df <- data.frame(letters[1 : 20], dat)
我想的是:
numdat <- df[,df == "numeric"]
然而,这让我没有变量。下面给出了一个错误。
dat <- df[,class == "numeric"]
Error in class == "numeric" :
comparison (1) is possible only for atomic and list types
我该怎么办?
使用sapply
numdat <- df[,sapply(df, function(x) {class(x)== "numeric"})]