簡體   English   中英

Select 僅來自列表中數據幀的數字列

[英]Select only numeric columns from dataframes in a list

我有以下數據框列表

library(carData)
library(datasets)
l = list(Salaries,iris)

我只想 select 這個數據集列表中的數字列。 已經嘗試使用lapply select_if(is.numeric)進行 lapply,但它不適用於我。

We can use select with where in the newer version of dplyr - loop over the list with map and select the columns of the data.frames

library(purrr)
library(dplyr)
map(l, ~ .x %>%
    select(where(is.numeric)))

或使用base R

lapply(l, Filter, f = is.numeric)

使用lapply兩次這樣的基本R選項:

library(carData)
library(datasets)

l = list(Salaries,iris)
lapply(l, \(x) x[, unlist(lapply(x, is.numeric), use.names = FALSE)])
#> [[1]]
#>     yrs.since.phd yrs.service salary
#> 1              19          18 139750
#> 2              20          16 173200
#> 3               4           3  79750
#> 4              45          39 115000
#> 5              40          41 141500
#> 
#> [[2]]
#>     Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1            5.1         3.5          1.4         0.2
#> 2            4.9         3.0          1.4         0.2
#> 3            4.7         3.2          1.3         0.2
#> 4            4.6         3.1          1.5         0.2
#> 5            5.0         3.6          1.4         0.2

使用reprex v2.0.2創建於 2022-09-25

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM