为什么我的函数遍历行而不是列?

发布于 2025-01-10 22:31:29 字数 4169 浏览 0 评论 0原文

我编写了以下函数,

compareColumns <- function(x) {
  
  if (is.numeric(x)) {
    paste("range", x, "=", range(x)) 
  } else if (is.integer(x)) {
    paste("table", x, "=", table(x)) 
  } else if (is.logical(x)) {
    paste("table", x, "=", table(x))
  } else if (is.character(x) & length(unique(x)) <= 15) {
    paste("table", x, "=", table (x))
  } else {
    paste("count unique", x, "=", length(unique(x)))
  }
    
}

我希望输出是如果我对每一列手动执行所有这些操作(例如

apply(df, 2, length)

输出:

Column1                   Column2                   Column3             Column4                   
2115                      2115                      2115                2115 

length(df$Column1)

< em>outputs

2115                

相反,我得到

apply(df, 2, compareColumns)

outputs:

Column1                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   [1,] "count unique row1 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [2,] "count unique row2 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [3,] "count unique row3 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [4,] "count unique row4 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [5,] "count unique row5 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [6,] "count unique row6 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [7,] "count unique row7 = 212"

我哪里出错了?为什么我的函数不能仅应用于列?

I've written the following function

compareColumns <- function(x) {
  
  if (is.numeric(x)) {
    paste("range", x, "=", range(x)) 
  } else if (is.integer(x)) {
    paste("table", x, "=", table(x)) 
  } else if (is.logical(x)) {
    paste("table", x, "=", table(x))
  } else if (is.character(x) & length(unique(x)) <= 15) {
    paste("table", x, "=", table (x))
  } else {
    paste("count unique", x, "=", length(unique(x)))
  }
    
}

I want the output to be the one I would get if I were doing all of those operations manually to each column such as

apply(df, 2, length)

outputs:

Column1                   Column2                   Column3             Column4                   
2115                      2115                      2115                2115 

or

length(df$Column1)

outputs

2115                

instead I get

apply(df, 2, compareColumns)

outputs:

Column1                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   [1,] "count unique row1 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [2,] "count unique row2 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [3,] "count unique row3 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [4,] "count unique row4 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [5,] "count unique row5 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [6,] "count unique row6 = 212"                                                                                                                                                                                                                                                                                                                                                                                                                       
   [7,] "count unique row7 = 212"

Where am I going wrong? Why won't my function apply to columns only?

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

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

发布评论

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

评论(1

柠檬色的秋千 2025-01-17 22:31:29

使用lapply而不是apply,因为apply转换为matrix,因此只能有一个类型< /code> - 即,如果有一个列是 character,它会将整个列转换为 character 类,从而与 apply 获得相同的输出> - 即它不能满足任何条件,从而进入 else 条件

lapply(df, compareColumns)

另外,当我们使用 x 进行粘贴时,即列值、长度 将与行数相同


检查可重现的示例

data(iris)
apply(iris, 2, compareColumns)
lapply(iris, compareColumns)

Use lapply instead of apply as apply converts to matrix and thus can have only a single type - i.e. if there is a single column that is character, it converts the whole into character class and thus got the same output with apply - i.e. it couldn't satisfy any of the conditions and thus goes into the else condition

lapply(df, compareColumns)

Also, as we are pasteing with x i.e. the column values, the length of the output will be the same as the number of rows


Check with a reproducible example

data(iris)
apply(iris, 2, compareColumns)
lapply(iris, compareColumns)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文