如何过滤即使在R中的一个单元格中值也超过50的列? [英] How to filter columns that have values over 50 even in one cell in R?

查看:79
本文介绍了如何过滤即使在R中的一个单元格中值也超过50的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框有很多列,以至于在过滤时我无法一一指定列名.所以我需要一次过滤所有列.我想将列的值保留为不超过50的数据框.我已经对数据框进行了转置,但这无济于事.我还从数据框中删除了逗号,这也无济于事(我将数字设为整数,并考虑到在这种情况下我必须使用50000 insted of 50).以下是我尝试使用的示例代码.我还以原始格式发布数据框的图片,而不进行转置或删除吗?

I have dataframe that has so many columns that i'm not able to specify column names one by one when filtering. So i need to filter all of the columns at once. And I want to leave the columns to dataframe that doesn't have any values over 50. I have transposed my dataframe but that doesn't help. I have also removed commas from my dataframe and that doesn't help either (i made numbers as integers and took that into consideration that i have to use 50000 insted of 50 in that case). Below is example codes that i have tried to use. I also post picture of my data frame in it's original form without transpose or removing?

在第一个数据集中,采样数据是字符数据.当我更改它在第二个示例中的显示方式时,它是数字,我想过滤50000以下的值

In the first dataset samplw data is character data. When i change it how it is in the second example it is numeric and i want to filter values under 50000

#Removing values over 50kwh
daata <- apply(data8, 2, function(x) filter(x, x<50, preserve = TRUE))

daata <- apply(data, 2, function(x) filter_all(any_vars(x < 50)))

data6 <- data[sapply(data, function(x) max(x, na.rm = T) < 50)]

#example of data set
  xxx_877963267 xxx_9073498356 xxx_07839328
1       24.4560         5.8799       4.7854
2       12.5667         2.5643       0.7867
3       67.3453         7.4573       6.3298
4        0.3456         1.7398       2.3585
5        4.5675         2.3345       2.3556

xxx_7530298011 = c(0, 
    0, 0, 0, 0, 0), xxx_7530298028 = c(0, 0, 0, 0, 0, 0), 
    xxx_7530695034 = c(1700, 1500, 1380, 1680, 1980, 1640), 
    xxx_7534110020 = c(0, 0, 0, 0, 0, 0), xxx_7530293026 = c(1400, 
    1450, 1450, 1500, 1610, 1760)), row.names = c(NA, 6L), class = "data.frame"

xxx_7530293026 = c(1400, 1450, 1450, 1500, 1610, 
    1760, 1950, 1970, 2100, 2130, 2230, 2260, 2280, 2150, 2180, 
    2050, 2150, 1840, 1740, 1660, 1610, 1920, 3710, 2280, 2390, 
    2360, 2210, 2340, 2390, 2210, 2310, 2360, 2340, 2180, 2340, 
    2310, 2340, 2100, 2340, 2360, 2260, 2150, 2360, 2340, 2440, 
    2130, 4270, 3010, 2980, 2940, 2970, 3060, 3030, 3060, 3100, 
    3150, 3050, 3100, 3160, 3010, 3110, 3070, 3150, 3150, 3190, 
    3310, 3220, 3250, 3330, 3410, 3300, 2120, 1400, 1440, 1530, 
    1790, 2900, 5740, 6680, 6720, 6850, 6330, 6740, 6650, 6480, 
    5410, 3340, 2950, 2790, 2510, 2160, 1830, 1610, 1510, 3120, 
    1850, 1880, 1960, 2120, 1940, 2050, 1990, 2180, 2140, 1990, 
    2050, 1700, 1900, 1770, 1830, 1680, 1610, 1830, 1720, 1850, 
    1660, 1610, 1180, 2510, 1180, 1180, 1220, 1220, 1270, 1350, 
    1480, 1640, 1660, 1770, 1790, 1880, 1900, 1920, 1810, 1830, 
    1720, 1810, 1550, 1460, 1400, 1350, 1350, 3400, 2310, 1770, 
    2250, 2250, 2010, 2290, 2180, 2200, 2530, 2140, 2200, 2400, 
    1900, 1550, 2090, 1940, 2050, 1770, 1790, 2120, 1900, 1990, 
    1990, 2270, 1900, 1700, 1420, 1660, 1720, 1750, 1440, 1850, 
    1570, 1750, 1570, 1880, 1420, 1660, 1550, 1460, 1330, 1640, 
    1400, 1640, 1440, 1570, 1610, 3120, 1920, 2010, 1990, 1850, 
    1960, 2010, 1850, 1940, 1990, 1960, 1830, 1960, 1940, 1960, 
    1770, 1960, 1990, 1900, 1810, 1990, 1960, 2050, 1790, 5910, 
    2150, 2370, 2160, 2330, 2190, 2360, 2320, 2310, 2470, 2390, 
    2400, 2480, 2510, 2400, 2440, 2510, 2450, 2500, 2480, 2460, 
    2320, 2310, 2200, 2200, 2100, 2260, 2060, 1970, 1980, 1950, 
    1870, 1920, 1880, 1880, 2040, 1860, 1830, 2330, 2780, 3100, 
    2900, 2650, 2790, 2690, 2450, 2380, 2260, 1950, 2010, 2020, 
    2090, 2080, 2140, 2240, 2300, 2320, 2280, 2440, 2440, 2450, 
    2270, 2420, 2330, 2340, 2520, 2520, 2660, 2280, 2410, 2180, 
    2390, 2470, 2610, 2600, 2630, 2750, 2800, 2770, 2850, 2890, 
    2900, 2500, 2810, 2790, 2790, 2580, 1890, 1840, 1780, 1800, 
    1660, 1760, 1740, 1740, 1760, 1820, 1790, 1810, 1820, 1740, 
    1850, 1740, 1770, 1720, 1560, 1740, 1820, 1930, 1590, 1710, 
    1890, 1930, 1950, 2070, 2120, 2200, 2310, 2290, 2300, 2350, 
    2450, 2460, 2480, 2470, 2490, 2590, 2570, 2550, 2620, 2620, 
    2550, 2520, 2510, 2490, 2470, 2380, 2430, 2460, 2530, 2470, 
    2390, 2450, 2510, 2450, 2490, 2360, 2370, 2470, 2360, 2400, 
    2370, 2360, 2420, 2490, 2440, 2420, 2460, 2440, 2560, 2530, 
    2530, 2660, 2750, 2710, 2700, 2790, 2810, 2460, 2720, 2730, 
    2520, 2610, 2320, 2220, 2270, 2140, 2220, 2130, 2300, 2220, 
    2320, 2340, 2310, 2450, 2490, 2450, 2530, 2650, 2630, 2640, 
    2720, 2790, 2870, 2880, 2870, 3050, 2890, 2820, 2870, 2980, 
    2990, 2910, 2990, 3060, 3060, 2980, 3090, 3070, 3100, 3130, 
    3120, 3390, 3220, 3210, 3220, 3400, 3240, 3330, 3260, 3480, 
    3320, 3240, 3270, 3410, 3130, 3160, 2990, 3180, 2920, 2800, 
    2660, 2750, 2630, 2470, 2480, 2410, 2250, 2380, 2370, 2380, 
    2400, 2380, 2430, 2420, 2490, 2420, 2520, 2490, 2660, 2610, 
    2670, 2730, 2880, 2840, 2790, 2870, 3040, 2950, 3050, 3060, 
    3410, 3160, 3270, 3280, 3430, 3350, 3380, 3370, 3590, 3370, 
    3480, 3620, 3640, 3530, 3620, 3670, 3570, 3610, 3610, 3650, 
    3630, 3680, 3730, 3850, 3750, 3770, 3870, 4050, 3940, 3990, 
    4090, 4170, 4100, 4180, 4250, 4160, 4230, 4090, 4120, 3870, 
    3830, 3810, 3880, 3710, 3730, 3910, 3700, 3690, 3700, 3810, 
    3740, 3570, 3410, 3440, 3190, 3090, 3080, 3190, 3040, 2980, 
    2950, 2870, 2950, 2850, 2600, 2570, 2450, 2360, 2340, 2160, 
    2360, 2150, 2240, 2320, 2120, 2280, 2090, 2280, 2260, 2310, 
    2480, 2490, 2550, 2740, 2820, 3040, 3040, 3060, 3100, 3200, 
    3180, 3140, 3370, 3170, 3230, 3180, 3360, 3190, 3210, 3190, 
    3340, 3100, 3030, 2900, 2980, 2770, 2630, 2660, 2570, 2580, 
    2480, 2400, 2370, 2530, 2390, 2390, 2450, 2510, 2540, 2500, 
    2470, 2490, 2640, 2530, 2570, 2540, 2680, 2650, 2690, 2570, 
    2620, 2770, 2690, 2680, 2740, 2870, 2790, 2760, 2800, 3030, 
    2890, 2940, 3010, 3180, 3100, 3130, 3140, 3330, 3150, 3280, 
    3250, 3560, 3400, 3430, 3480, 3680, 3490, 3540, 3580, 3650, 
    3470, 3500, 3520, 3710, 3570, 3670, 3720, 3870, 3760, 3770, 
    3840, 4010, 3760, 3740, 3850, 3680, 3600, 3510, 3650, 3780, 
    3420, 3460, 3480, 3300, 3500, 3230, 3290, 3270, 3460, 3380, 
    3430, 3400, 3190, 3000, 3060, 2940, 2920, 3130, 2780, 2840, 
    2750, 2740, 3010, 2820, 2710, 2650, 2590, 2820, 2690, 2530, 
    2500, 2450, 2590, 2480, 2440, 2420, 2610, 2440, 2390, 2530, 
    2470, 2370, 2430, 2410, 2290, 2430, 2330, 2330, 2340, 2230, 
    2180, 2190, 2100, 2010, 2160, 1940, 1960, 1960, 2040, 1960, 
    1900, 1940, 1950, 1990, 2010, 2130, 2000, 2090, 2010, 2040, 
    2060, 2080, 2160, 2180, 2230, 2240, 2280, 2290, 2280, 2330, 
    2400, 2340, 2380, 2390, 2400, 2340, 2340, 2360, 2400)), class = "data.frame", row.names = c(NA, 
-744L))

推荐答案

如果您不介意软件包:

library(data.table)
library(hutils)
library(hutilscpp)

library(nycflights13)  # for sample data


flights_dt <- as.data.table(flights)  # for select_which
select_which(flights_dt, function(x) is.numeric(x) && !which_first(x > 50))
#>         month day hour
#>      1:     1   1    5
#>      2:     1   1    5
#>      3:     1   1    5
#>      4:     1   1    5
#>      5:     1   1    6
#>     ---               
#> 336772:     9  30   14
#> 336773:     9  30   22
#> 336774:     9  30   12
#> 336775:     9  30   11
#> 336776:     9  30    8

# flights <- as.data.frame(flights) or  <- as_tibble(flights)  as desired

reprex软件包(v0.3.0)创建于2020-09-09 sup>

Created on 2020-09-09 by the reprex package (v0.3.0)

这篇关于如何过滤即使在R中的一个单元格中值也超过50的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆