使用dplyr :: Select s select select s base r grepl和匿名函数

发布于 2025-01-28 16:03:57 字数 1021 浏览 3 评论 0原文

这里有一个非常相似的问题:如何基于基于基于列的列dplyr :: tibble grep

但是,我认为select_ifselect(white())超过。

我知道我可以执行以下操作,并且可以使用:

# select all columns with three characters
mtcars %>% 
  select(
    matches("^[a-zA-Z]{3}$")
  )

但是我也可以使用匿名函数(在此处所有列值而不是名称上)选择列。

mtcars %>% 
  select(
    where(function(x)sum(is.na(x)) == 0)
  )

因此,我认为我可以使用Anonymus函数和grepl来选择列。而且这不起作用:

mtcars %>% 
  select(
    where(
      function(x) grepl("^[a-zA-Z]{3}$", x)
    )
  )

我该如何完成这项工作?我的意思是我总是可以使用匹配助手。但是我只想了解如何使用

select(where())语句在dataframe的名称上使用,而不是列中的所有值。

更新

这起作用:

mtcars %>% 
  select(
      which(grepl("^[a-zA-Z]{3}$", names(.)))
  )

但是我不确定是否没有更好的方法;)

There is a very similar question here: How to select columns based on grep in dplyr::tibble

However I think that the select_if was superseeded with select(where()).

I know that I can do the following and it works:

# select all columns with three characters
mtcars %>% 
  select(
    matches("^[a-zA-Z]{3}
quot;)
  )

But I can also use an anonymus function (here over all the column values and not the names) to select columns.

mtcars %>% 
  select(
    where(function(x)sum(is.na(x)) == 0)
  )

So I thought I could use an anonymus function and grepl to select columns. And this does not work:

mtcars %>% 
  select(
    where(
      function(x) grepl("^[a-zA-Z]{3}
quot;, x)
    )
  )

How could I make this work? I mean I could always use the matches helper. But I would just like to understand how to use

select(where()) statement over the names of the dataframe and not over all the values in a column.

Update

This works:

mtcars %>% 
  select(
      which(grepl("^[a-zA-Z]{3}
quot;, names(.)))
  )

But I am not sure if there isn't a better way;)

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

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

发布评论

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

评论(2

作妖 2025-02-04 16:03:57

您可以只使用grep()select> select(),而没有whene()函数。

mtcars %>% 
  select(grep("^[a-zA-Z]{3}$", names(.)))

您的初始尝试无效,因为在此代码中:

mtcars %>% 
  select(
    where(
      function(x) grepl("^[a-zA-Z]{3}$", x)
    )
  )

x中,其中()函数是变量的值,而不是变量的名称。这就是为什么如果您做过之类的操作(is.numeric)有效的原因 - 因为它可以代替实际值。

You could just use grep() with select() without the where() function.

mtcars %>% 
  select(grep("^[a-zA-Z]{3}
quot;, names(.)))

Your initial attempt didn't work because in this code:

mtcars %>% 
  select(
    where(
      function(x) grepl("^[a-zA-Z]{3}
quot;, x)
    )
  )

the x in the where() function are the values of the variable and not the name of the variable. That's why it works if you did something like where(is.numeric) works - because it is substituting the actual values.

久隐师 2025-02-04 16:03:57

base r选项:

mtcars[grepl("^[a-zA-Z]{3}$", names(mtcars))]

输出:

                     mpg cyl
Mazda RX4           21.0   6
Mazda RX4 Wag       21.0   6
Datsun 710          22.8   4
Hornet 4 Drive      21.4   6
Hornet Sportabout   18.7   8
Valiant             18.1   6
Duster 360          14.3   8
Merc 240D           24.4   4
Merc 230            22.8   4
Merc 280            19.2   6
Merc 280C           17.8   6
Merc 450SE          16.4   8
Merc 450SL          17.3   8
Merc 450SLC         15.2   8
Cadillac Fleetwood  10.4   8
Lincoln Continental 10.4   8
Chrysler Imperial   14.7   8
Fiat 128            32.4   4
Honda Civic         30.4   4
Toyota Corolla      33.9   4
Toyota Corona       21.5   4
Dodge Challenger    15.5   8
AMC Javelin         15.2   8
Camaro Z28          13.3   8
Pontiac Firebird    19.2   8
Fiat X1-9           27.3   4
Porsche 914-2       26.0   4
Lotus Europa        30.4   4
Ford Pantera L      15.8   8
Ferrari Dino        19.7   6
Maserati Bora       15.0   8
Volvo 142E          21.4   4

Base R option:

mtcars[grepl("^[a-zA-Z]{3}
quot;, names(mtcars))]

Output:

                     mpg cyl
Mazda RX4           21.0   6
Mazda RX4 Wag       21.0   6
Datsun 710          22.8   4
Hornet 4 Drive      21.4   6
Hornet Sportabout   18.7   8
Valiant             18.1   6
Duster 360          14.3   8
Merc 240D           24.4   4
Merc 230            22.8   4
Merc 280            19.2   6
Merc 280C           17.8   6
Merc 450SE          16.4   8
Merc 450SL          17.3   8
Merc 450SLC         15.2   8
Cadillac Fleetwood  10.4   8
Lincoln Continental 10.4   8
Chrysler Imperial   14.7   8
Fiat 128            32.4   4
Honda Civic         30.4   4
Toyota Corolla      33.9   4
Toyota Corona       21.5   4
Dodge Challenger    15.5   8
AMC Javelin         15.2   8
Camaro Z28          13.3   8
Pontiac Firebird    19.2   8
Fiat X1-9           27.3   4
Porsche 914-2       26.0   4
Lotus Europa        30.4   4
Ford Pantera L      15.8   8
Ferrari Dino        19.7   6
Maserati Bora       15.0   8
Volvo 142E          21.4   4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文