R:确定数据框中数值变量的优雅方法

发布于 2024-11-16 17:01:39 字数 287 浏览 4 评论 0原文

这是我用来在数据框中查找数字变量的代码:

Data <- iris
numericvars <- NULL
for (Var in names(Data)) {
    if(class(Data[,Var]) == 'integer' | class(Data[,Var]) == 'numeric') {
        numericvars <- c(numericvars,Var)
    }
}
numericvars

有没有一种不那么循环的方法来做到这一点?

Here's the code I use to find numeric variables in a data frame:

Data <- iris
numericvars <- NULL
for (Var in names(Data)) {
    if(class(Data[,Var]) == 'integer' | class(Data[,Var]) == 'numeric') {
        numericvars <- c(numericvars,Var)
    }
}
numericvars

Is there a less loopy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

山人契 2024-11-23 17:01:39

这是一个非常简单的单行代码 sapply

sapply(Data, is.numeric)
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#         TRUE         TRUE         TRUE         TRUE        FALSE

# is.numeric should pick up integer columns too
Data$Species <- as.integer(Data$Species)
sapply(Data, is.numeric)
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#         TRUE         TRUE         TRUE         TRUE         TRUE

This is a pretty simple one-liner with sapply:

sapply(Data, is.numeric)
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#         TRUE         TRUE         TRUE         TRUE        FALSE

# is.numeric should pick up integer columns too
Data$Species <- as.integer(Data$Species)
sapply(Data, is.numeric)
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
#         TRUE         TRUE         TRUE         TRUE         TRUE
じее 2024-11-23 17:01:39

这有点严格:

R> sapply(colnames(iris), function(x) inherits(iris[,x], c("numeric","integer")))
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
        TRUE         TRUE         TRUE         TRUE        FALSE 
R> 

This is a little tighter:

R> sapply(colnames(iris), function(x) inherits(iris[,x], c("numeric","integer")))
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
        TRUE         TRUE         TRUE         TRUE        FALSE 
R> 
我不是你的备胎 2024-11-23 17:01:39

使用 sapply()lapply() 在这里似乎合乎逻辑:

sapply(iris, function(x) class(x) %in% c("integer","numeric"))

这给出了:

> sapply(iris, function(x) class(x) %in% c("integer","numeric"))
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
        TRUE         TRUE         TRUE         TRUE        FALSE

值得注意的是,在您的循环中,您正在增长 numericvars循环每次迭代的向量;在 R 中,这是一个很大的禁忌!它强制 R 每次都复制并扩展向量。事先分配足够的存储并填充对象;在这里,这意味着创建 numericvars ,然后

numericvars <- character(length = ncol(iris))

在循环中做

nams <- names(iris)
for(i in seq_len(ncol(iris))) {
    if(class(iris[, i]) == 'integer' | class(iris[, i]) == 'numeric') {
        numericvars[i] <- nams[i]
    }
}

更多的工作,但效率更高,尽管只有当迭代次数变大时您才会看到它。

The use of sapply() or lapply() seems logical here:

sapply(iris, function(x) class(x) %in% c("integer","numeric"))

which gives:

> sapply(iris, function(x) class(x) %in% c("integer","numeric"))
Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
        TRUE         TRUE         TRUE         TRUE        FALSE

Worth noting that in your loop, you are growing the numericvars vector at each iteration of the loop; in R, that is a big no-no! It forces R to copy and expand the vector each time. Allocate sufficient storage before hand and fill in the object; here that would mean creating numericvars as

numericvars <- character(length = ncol(iris))

then in the loop doing

nams <- names(iris)
for(i in seq_len(ncol(iris))) {
    if(class(iris[, i]) == 'integer' | class(iris[, i]) == 'numeric') {
        numericvars[i] <- nams[i]
    }
}

A little bit more work, but far more efficient, though you'll only see it when the number of iterations becomes larger.

她如夕阳 2024-11-23 17:01:39

plyr 中还有 colwise()numcolwise()catcolwise()colwise() 将对向量进行操作的函数转换为对数据帧按列进行操作的函数。 numcolwisecatcolwise 分别提供仅对数字变量和离散变量进行操作的版本。

library(plyr)
colwise(is.numeric)(Data)

> colwise(is.numeric)(Data)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1         TRUE        TRUE         TRUE        TRUE   FALSE

There's also colwise(), numcolwise() and catcolwise() in plyr. colwise() turns a function that operates on a vector into a function that works column-wise on a dataframe. numcolwise and catcolwise provide versions that operate only on numeric and discrete variables respectively.

library(plyr)
colwise(is.numeric)(Data)

> colwise(is.numeric)(Data)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1         TRUE        TRUE         TRUE        TRUE   FALSE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文