到现在为止,当我想将一个函数仅应用于数字列时,我使用了
library(tidyverse)
map(diamonds[map_lgl(diamonds, is.numeric)], range, na.rm = TRUE)
现在我想知道这是否是map_if()
不那么笨重的情况。我试过的是:
map_if(diamonds, is.numeric, range, na.rm = TRUE)
但出于某种原因,现在为所有列计算range
,而不仅仅是数字列。
我做错了什么或者我是否误解了map_if()
的目的?如果是后者,我会使用map_if()
进行什么样的算法?
答案 0 :(得分:2)
我们可以使用summarise_if
diamonds %>%
summarise_if(is.numeric, funs(list(range(.)))) %>%
unnest
# A tibble: 2 x 7
# carat depth table price x y z
# <dbl> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
# 1 0.200 43.0 43.0 326 0 0 0
#2 5.01 79.0 95.0 18823 10.7 58.9 31.8
map_if
将应用于numeric
列上的函数并取消其他列,以便我们从这些列中获取原始值。 summarise_if
将根据条件对数据集进行子集化,并在这些列上应用函数
答案 1 :(得分:-1)
我刚刚在我的网站上运行你的代码并且工作正常:
map_if( diamonds, is.numeric, range, na.rm = TRUE)
$carat
[1] 0.20 5.01
$cut
[1] Ideal Premium Good Premium Good Very Good Very Good Very Good Fair Very Good Good Ideal Premium
[14] Ideal Premium Premium Ideal Good Good Very Good Good Very Good Very Good Very Good Very Good Very Good
[27] Premium Very Good Very Good Very Good Very Good Very Good Very Good Very Good Very Good Good Good Good Very Good
[40] Ideal Ideal Ideal Good Good Good Premium Very Good Good Very Good Very Good Very Good Ideal
[53] Ideal Premium Premium Ideal Premium Very Good Very Good Good Ideal Premium Ideal Ideal Premium
[66] Ideal Ideal Very Good Premium Premium Very Good Very Good Premium Premium Good Very Good Very Good Very Good
[79] Very Good Very Good Very Good Very Good Ideal Ideal Good Premium Premium Premium Premium Premium Ideal
[92] Fair Ideal Very Good Very Good Good Good Fair Very Good Premium Very Good Premium Ideal Premium
[105] Ideal Ideal Premium Ideal Ideal Ideal Ideal Ideal Premium Very Good Ideal Ideal Premium
[118] Ideal Ideal Ideal Ideal Ideal Very Good Fair Fair Premium Premium Very Good Fair Fair
[131] Ideal Very Good Ideal Very Good Very Good Premium Very Good Premium Ideal Ideal Premium Premium Very Good
[144] Very Good Ideal Good Very Good Very Good Very Good Ideal Premium Ideal Premium Premium Very Good Ideal
[157] Ideal Premium Premium Ideal Premium Very Good Very Good Ideal Ideal Very Good Very Good Ideal Ideal
[170] Good Ideal Premium Very Good Ideal Ideal Good Very Good Very Good Premium Ideal Ideal Ideal
[183] Ideal Ideal Good Ideal Very Good Premium Very Good Good Good Ideal Premium Premium Premium
[196] Premium Premium Premium Ideal Premium Very Good Premium Premium Good Fair Premium Very Good Ideal
[209] Very Good Ideal Very Good Premium Ideal Ideal Ideal Premium Ideal Ideal Very Good Very Good Ideal
[222] Good Premium Very Good Ideal Premium Premium Fair Premium Ideal Very Good Very Good Very Good Ideal
[235] Ideal Very Good Very Good Ideal Good Good Ideal Fair Premium Good Good Premium Premium
[248] Very Good Ideal Ideal Ideal Good Premium Premium Premium Fair Ideal Premium Ideal Premium
[261] Premium Very Good Ideal Premium Premium Ideal Premium Premium Premium Ideal Very Good Very Good Good
[274] Ideal Ideal Very Good Very Good Premium Ideal Good Premium Premium Premium Premium Premium Good
[287] Very Good Very Good Premium Premium Premium Ideal Ideal Ideal Ideal Ideal Fair Premium Fair
[300] Very Good Ideal Very Good Ideal Ideal Very Good Good Premium Very Good Ideal Ideal Very Good Premium
[313] Ideal Ideal Fair Ideal Ideal Premium Ideal Premium Good Good Premium Premium Premium
[326] Very Good Ideal Premium Premium Very Good Very Good Ideal Ideal Good Premium Premium Premium Premium
[339] Premium Premium Very Good Ideal Very Good Very Good Very Good Very Good Ideal Very Good Ideal Ideal Ideal
[352] Ideal Fair Premium Premium Ideal Fair Premium Ideal Fair Good Very Good Very Good Ideal
[365] Very Good Very Good Premium Very Good Very Good Fair Very Good Ideal Very Good Very Good Premium Premium Fair
[378] Very Good Very Good Ideal Good Good Very Good Very Good Fair Fair Very Good Very Good Good Very Good
[391] Premium Premium Ideal Premium Very Good Ideal Premium Premium Premium Very Good Good Ideal Very Good
[404] Good Ideal Very Good Very Good Very Good Ideal Ideal Ideal Ideal Ideal Ideal Ideal Very Good
[417] Very Good Ideal Ideal Ideal Very Good Very Good Ideal Fair Premium Ideal Premium Very Good Good
[430] Premium Ideal Premium Very Good Very Good Premium Ideal Ideal Ideal Ideal Fair Fair Premium
[443] Premium Fair Premium Very Good Ideal Good Premium Ideal Ideal Premium Ideal Ideal Premium
[456] Ideal Premium Premium Ideal Premium Ideal Good Premium Premium Premium Premium Premium Ideal
[469] Ideal Premium Premium Premium Fair Very Good Ideal Good Good Ideal Ideal Premium Ideal
[482] Premium Good Premium Premium Premium Very Good Premium Premium Premium Premium Premium Ideal Ideal
[495] Ideal Premium Premium Premium Ideal Good Ideal Premium Premium Ideal Good Ideal Ideal
[508] Premium Premium Ideal Very Good Good Premium Ideal Fair Ideal Premium Ideal Good Ideal
[521] Premium Premium Premium Very Good Premium Premium Fair Premium Good Premium Premium Very Good Premium
[534] Very Good Premium Ideal Very Good Good Premium Ideal Ideal Ideal Premium Premium Good Ideal
[547] Ideal Premium Premium Very Good Very Good Ideal Ideal Ideal Ideal Premium Very Good Ideal Ideal
[560] Premium Ideal Very Good Very Good Premium Premium Ideal Premium Good Very Good Very Good Very Good Ideal
[573] Ideal Ideal Ideal Ideal Ideal Very Good Ideal Ideal Very Good Fair Ideal Ideal Ideal
[586] Ideal Ideal Ideal Premium Very Good Good Premium Ideal Ideal Very Good Fair Premium Premium
[599] Premium Premium Premium Very Good Premium Premium Premium Very Good Ideal Ideal Ideal Premium Premium
[612] Premium Ideal Ideal Ideal Ideal Premium Premium Premium Premium Very Good Very Good Ideal Premium
[625] Ideal Premium Premium Premium Very Good Very Good Ideal Ideal Very Good Ideal Premium Fair Ideal
[638] Premium Premium Ideal Premium Very Good Very Good Very Good Very Good Very Good Ideal Very Good Ideal Ideal
[651] Ideal Very Good Very Good Ideal Good Ideal Very Good Very Good Good Premium Ideal Very Good Ideal
[664] Fair Good Ideal Ideal Good Premium Premium Premium Very Good Ideal Premium Very Good Ideal
[677] Fair Good Very Good Very Good Ideal Ideal Very Good Very Good Ideal Very Good Premium Ideal Very Good
[690] Ideal Ideal Ideal Very Good Premium Good Ideal Premium Premium Premium Very Good Very Good Premium
[703] Premium Fair Fair Good Fair Premium Premium Very Good Good Premium Fair Fair Fair
[716] Ideal Ideal Ideal Ideal Fair Ideal Ideal Ideal Good Good Good Good Very Good
[729] Ideal Good Ideal Ideal Ideal Ideal Ideal Ideal Ideal Ideal Ideal Premium Premium
[742] Premium Ideal Premium Premium Premium Ideal Ideal Good Premium Fair Premium Premium Good
[755] Very Good Ideal Premium Premium Ideal Very Good Very Good Premium Premium Premium Very Good Premium Ideal
[768] Ideal Premium Good Fair Fair Ideal Premium Premium Ideal Premium Fair Premium Very Good
[781] Very Good Ideal Premium Ideal Premium Ideal Ideal Premium Fair Premium Premium Very Good Very Good
[794] Very Good Premium Very Good Ideal Very Good Premium Premium Premium Fair Premium Good Ideal Premium
[807] Premium Premium Premium Premium Ideal Premium Premium Premium Premium Premium Premium Ideal Premium
[820] Ideal Ideal Very Good Premium Very Good Very Good Ideal Ideal Premium Very Good Ideal Ideal Premium
[833] Premium Ideal Ideal Ideal Ideal Premium Ideal Fair Ideal Ideal Premium Very Good Premium
[846] Ideal Ideal Ideal Ideal Premium Ideal Premium Ideal Ideal Ideal Ideal Premium Premium
[859] Very Good Good Premium Ideal Ideal Good Very Good Ideal Very Good Ideal Good Ideal Ideal
[872] Very Good Very Good Ideal Good Very Good Very Good Ideal Ideal Fair Fair Premium Good Fair
[885] Fair Premium Premium Premium Premium Premium Premium Ideal Ideal Ideal Premium Fair Ideal
[898] Fair Fair Fair Premium Premium Very Good Ideal Ideal Good Good Very Good Very Good Very Good
[911] Ideal Ideal Very Good Premium Premium Premium Ideal Ideal Ideal Fair Ideal Very Good Very Good
[924] Ideal Very Good Premium Very Good Ideal Premium Fair Premium Ideal Good Fair Fair Very Good
[937] Premium Ideal Fair Fair Ideal Ideal Very Good Premium Ideal Very Good Very Good Very Good Good
[950] Very Good Ideal Ideal Ideal Premium Premium Ideal Ideal Premium Good Ideal Very Good Ideal
[963] Ideal Ideal Ideal Good Ideal Fair Very Good Very Good Ideal Ideal Very Good Ideal Good
[976] Very Good Premium Very Good Ideal Ideal Very Good Very Good Ideal Very Good Very Good Very Good Ideal Ideal
[989] Ideal Ideal Premium Very Good Ideal Very Good Premium Ideal Ideal Good Premium Premium
[ reached getOption("max.print") -- omitted 52940 entries ]
Levels: Fair < Good < Very Good < Premium < Ideal
$color
[1] E E E I J J I H E H J J F J E E I J J J I E H J J G I J D F F F E E D F E H D I I J D D H F H H E H F G I E D I J I I I I D D D I G
[67] I G G E D H H H H F E D D E E D E I E G H H H I E E G E G E F F E H D E G G I G G I F E F E I G F F F G E F D E F F F F H D H H H D
[133] D E H E F F H G G G D F D H G D D E D I D G F D F G G D G H F G F H F G F E D D J E E I F G E E E E G G G G D F F F F E E E E E E E
[199] E F E E E E H F E F H E F G F G G F H H H D G E E D D D D G F F F H F F D H I I D G F E H F E E E J G E G G E E H J G I F F F I E E
[265] E E F F H F G E E I H E E I F F D I H G I E H E D D D F D G H H G F E H I E F D G I E I G G H G I G G F F F H F F F F G J G F E F F
[331] E G E G F F G G G G G D E E E D F F H D D D I F F E E F G F G G H F F F J H F G G E D F D F F F F D F G F D J I G F E F I I I I I I
[397] H H H H H H H I H E E F H H I I G F G E F I G E F F D J E F G E I E E E D F G H H H H J F H E E G I G D F H H E I E E F E E E E J E
[463] E E E H F H E E F E H G H F F G E E E E E E E D E E E E E E F I F D D D D J D E E D E D D E F D D I E E H E D I E F E G H G H J H E
[529] E I E D E I F F D D H H H D H F E E F E E E H F F F I G E E E E G F I G G H F J I E D D F H F G H D F I J G E F F E E E F H E E E F
[595] H F F F F G F E F F G E F F F F F F F G E G G F G F H F F F D F H F D D F F F F F G D F G I F E E E F D G F E F G E E I I H E E H H
[661] D F H H H E E F E G G H E D G D D E E E F J H F D F H F E I G E I F G D D E I H E H E J J J H E F F J D E I G H F G F I E E H G E E
[727] E G E E I I I I I I G H H G F G G F J J J I I I H F F E G E I E G F F E E E E J E D D E G D D D F G D F E F J E D F D G G I F E D D
[793] F D F F H E H E E E E I D G F D D D D D D J J H E F E E E I D D D E H I H D H H F F F F F F G F I G E E D E G G H F E E G H F H E I
[859] E E J I G D D I G D E D D D E E G F E E I H I J J I I J F D F F G F F F G H H J I H E E H E G H F F F D I F F E F F G G J F F J D H
[925] E H H F F F F D H H I F I D E D E E D E I G E F H F E E F F E F F F F G E F E E D D H H F G G F H G G G G E G F G F G G G G F D E D
[991] E I I D E D E F D J
[ reached getOption("max.print") -- omitted 52940 entries ]
Levels: D < E < F < G < H < I < J
$clarity
[1] SI2 SI1 VS1 VS2 SI2 VVS2 VVS1 SI1 VS2 VS1 SI1 VS1 SI1 SI2 SI2 I1 SI2 SI1 SI1 SI1 SI2 VS2 VS1 SI1 SI1 VVS2
[27] VS1 VS2 VS2 VS1 VS1 VS1 VS1 VS1 VS1 VS1 VS1 SI1 VS2 SI2 SI2 SI1 VS2 VS1 SI2 SI1 SI2 SI2 VS2 SI2 SI1 VS1
[53] SI1 VS2 VS2 SI2 SI2 SI1 SI1 SI1 VS1 SI1 SI1 SI1 SI2 VVS2 VVS1 SI1 SI1 VVS1 VVS1 SI1 SI1 SI1 SI1 VVS2 VVS2 VVS2
[79] VVS2 VVS1 VVS1 VVS1 VVS2 SI2 VVS1 VVS1 VVS1 VVS1 VVS2 SI1 SI1 SI2 VS2 VS2 SI2 VS2 VS1 SI2 SI1 SI1 SI1 SI1 SI1 VS2
[105] VS1 SI1 SI1 VVS2 SI2 VVS2 SI2 SI2 VS2 SI1 VS2 VS2 SI2 VS2 VS2 SI2 SI2 SI1 VS2 VS2 VS2 VS2 SI1 VVS2 SI2 SI2
[131] VS2 SI1 SI1 VS2 VS1 VVS1 VS1 VS1 SI1 VVS1 VS2 VS2 VS2 VS1 SI2 VVS2 VS1 SI1 SI1 SI1 SI2 S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 VVS2 VS1 VS1 VS1 VVS2 SI2 SI1 VS1 SI1 SI2 VS1 VS2 VS1 VS1 VS2 VVS1 VS1 SI1 SI1 SI2
[911] VS2 SI1 SI1 IF VS1 VS1 SI2 SI2 SI2 VS1 VS1 SI2 SI1 VVS1 VS2 I1 IF VVS1 VS2 SI1 VS2 SI1 SI2 SI2 SI2 SI1
[937] SI1 SI2 SI1 SI2 SI1 SI1 SI1 VS1 VS1 VVS2 VS2 VS1 SI2 VS1 VVS2 VVS2 SI1 VS2 SI1 SI1 SI1 SI1 SI1 VVS1 VS2 VS2
[963] VS2 VS2 SI1 VS2 SI2 I1 VS1 SI2 VVS2 VS2 VVS1 SI2 SI2 SI1 SI1 SI2 SI2 SI1 VS2 VS1 VVS2 VS1 VS2 VS2 VVS1 SI1
[989] SI2 SI2 SI1 I1 I1 VVS2 SI1 VVS2 SI1 VS1 VS2 SI2
[ reached getOption("max.print") -- omitted 52940 entries ]
Levels: I1 < SI2 < SI1 < VS2 < VS1 < VVS2 < VVS1 < IF
$depth
[1] 43 79
$table
[1] 43 95
$price
[1] 326 18823
$x
[1] 0.00 10.74
$y
[1] 0.0 58.9
$z
[1] 0.0 31.8
我建议将R以及您使用的软件包升级到最新版本。如果那不起作用,请告诉我。