这是我的密码:
carats <- pull(diamonds %>% distinct(carat) %>% arrange(carat))
get_price_by_category <- function(dataset, x, y) {
print(c(x, typeof(x)))
dataset %>% filter(carat == x) %>% print()
}
carats %>% walk(get_price_by_category, dataset = diamonds, y= "price")当我运行它时,我得到以下结果:
[1] "0.2" "double"
# A tibble: 0 x 10
# … with 10 variables: carat <dbl>, cut <ord>, color <ord>, clarity <ord>,
# depth <dbl>, table <dbl>, price <int>, x <dbl>, y <dbl>, z <dbl>
[1] "0.21" "double"
# A tibble: 0 x 10
# … with 10 variables: carat <dbl>, cut <ord>, color <ord>, clarity <ord>,
# depth <dbl>, table <dbl>, price <int>, x <dbl>, y <dbl>, z <dbl>
[1] "0.22" "double"
# A tibble: 0 x 10
# … with 10 variables: carat <dbl>, cut <ord>, color <ord>, clarity <ord>,
# depth <dbl>, table <dbl>, price <int>, x <dbl>, y <dbl>, z <dbl>
[1] "0.23" "double"
# A tibble: 0 x 10
# … with 10 variables: carat <dbl>, cut <ord>, color <ord>, clarity <ord>,
# depth <dbl>, table <dbl>, price <int>, x <dbl>, y <dbl>, z <dbl>
...因此,很明显,我的函数识别正在传递的值,并且变量类型等效于正在对其进行筛选的列的类型。但是,同样明显的是,它并没有按照过滤器(克拉== x)函数的需要来处理它。
如果我通过将walk()更改为for()循环并显式地传递克拉的值来更改代码:
carats <- pull(diamonds %>% distinct(carat) %>% arrange(carat))
get_price_by_category <- function(dataset, x, y) {
print(c(x, typeof(x)))
dataset %>% filter(carat == x) %>% print()
}
for (c in carats) {
get_price_by_category(diamonds, c, y= "price")
}结果是一样的。
但是,如果保留for()循环,但将函数中的代码更改为引用c而不是引用x(即使在参数列表中将其作为x保留),则得到所需的结果:
carats <- pull(diamonds %>% distinct(carat) %>% arrange(carat))
get_price_by_category <- function(dataset, x, y) {
print(c(c, typeof(c)))
dataset %>% filter(carat == c) %>% print()
}
for (c in carats) {
get_price_by_category(diamonds, c, y= "price")
}结果:
[1] "0.2" "double"
# A tibble: 12 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.2 Premium E SI2 60.2 62 345 3.79 3.75 2.27
2 0.2 Premium E VS2 59.8 62 367 3.79 3.77 2.26
3 0.2 Premium E VS2 59 60 367 3.81 3.78 2.24
4 0.2 Premium E VS2 61.1 59 367 3.81 3.78 2.32
5 0.2 Premium E VS2 59.7 62 367 3.84 3.8 2.28
6 0.2 Ideal E VS2 59.7 55 367 3.86 3.84 2.3
7 0.2 Premium F VS2 62.6 59 367 3.73 3.71 2.33
8 0.2 Ideal D VS2 61.5 57 367 3.81 3.77 2.33
9 0.2 Very Good E VS2 63.4 59 367 3.74 3.71 2.36
10 0.2 Ideal E VS2 62.2 57 367 3.76 3.73 2.33
11 0.2 Premium D VS2 62.3 60 367 3.73 3.68 2.31
12 0.2 Premium D VS2 61.7 60 367 3.77 3.72 2.31
[1] "0.21" "double"
# A tibble: 9 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
2 0.21 Very Good E VS2 63.2 54 386 3.82 3.78 2.4
3 0.21 Premium E VS2 60.5 59 386 3.87 3.83 2.33
4 0.21 Premium E VS2 59.6 56 386 3.93 3.89 2.33
5 0.21 Premium D VS2 61.6 59 386 3.82 3.78 2.34
6 0.21 Premium D VS2 60.6 60 386 3.85 3.81 2.32
7 0.21 Premium D VS2 59.1 62 386 3.89 3.86 2.29
8 0.21 Premium D VS2 58.3 59 386 3.96 3.93 2.3
9 0.21 Premium E SI2 61.9 56 394 3.84 3.82 2.37
...因此,我可以将它作为x留在参数列表中,只要我在实际的函数代码中将它作为c引用,仍然可以得到所需的结果,这使我意识到,不知怎么的,我的调用中的值并没有像我期望的那样传递给我的函数。但我不明白为什么这不起作用,或者我需要做些什么才能让它发挥作用。
发布于 2019-07-18 04:19:55
这里有两个问题,当您看到问题2时,我认为您应该真正地看看问题1,这样您就不会被另一个bug咬了。
abs(carat-0.2) < 1e-8 )来“环顾它”。(你可以用它来做一些比较,但你不会真正知道它什么时候会咬你,直到一些奇怪的事情发生,你才会深入挖掘。)要做到这一点,您应该了解数据本身的一些情况,以便正确地通知您对1e-8的选择。x被看作是dataset$x,而不是函数参数.尝试使用不在数据集中的参数名称。
get_price_by_category <-函数( dataset,a,b,tol = 1e-8) { print(c(x,typeof(X) dataset %>% filter(abs(carat - a) < tol) %>% print() } carats %>% head(n=2) %>% walk(get_price_by_category,dataset=菱形,B=“价格”)#1 " 0.2“”双“##A级: 12×10 #克拉深表价格x y z#1 0.2 SI2 60.2 62 345 3.79 3.75 2.27 #2 0.2 VS2 59.8 62 367 3.79 3。77 2.26 #3 0.2高级E VS2 59 60 367 3.81 3.78 2.24 #4 0.2高级E VS2 61.1 59 367 3.81 3.78 2.32 #5 0.2 VS2 59.7 62 367 3.84 3.8 2.28 #6 0.2理想E VS2 59.7 55 3673.86 3.84 2.3 #7 0.2高级F VS2 62.6 59 367 3.73 3.71 2.33 #8 0.2理想D VS2 61.5 367 3.81 3.77 2.33 #9 0.2很好的E VS2 63.4 367 3.74 3.71 2.36 # 10理想E VS2 62.2 57 367 3.76 3.73 2.33 # 11 0.2高级D VS2 62.3 60 367 3.73 3.68 2.31 2.31 # 12 0.2高级D VS2 61.7 60 367 3.77 3.72 2.31#1 "0.2“##A table :9×10克拉切割颜色清晰度深度表价格x y z##1 0.21高级E SI1 59.8 61 326 3.89 3.84 2.31 #2 0.21非常好的E VS2 63.2 54 386 3.78 2.4 #3 0.21高级E VS2 60.5 59 386 3.87 3.83 2.33 #4 0.21高级E VS2 59.6 56 386 3.933.89 2.33 #5 0.21高级D VS2 61.6 59 386 3.82 3.78 2.34 #6 0.21高级D VS2 60.6 60 386 3.85 3.81 2.32 #7 0.21高级D VS2 59.1 62 386 3.86 3.86 2.29 #8 0.21高级D VS2 58.3 59 386 3.96 3.93 2.3 #9 0.21高级E SI2 61.9 56 394 3.84 3.82 2.37N.B.我要再说一遍:tolerance的选择对于这个函数的逻辑准确性至关重要。如果您处理的单元通常在1s或1000 s中,有8位或更少的有效位数,那么1e-8就可以了。如果您正在处理高精度的数字,您可能需要更接近.Machine$double.eps (在我的64位计算机上,它是2.22e-16,有关此变量组件的详细信息,请参阅?.Machine )。我没有理由不默认,但这就是为什么我说“选择1e-8是正确的”-只有你知道你的数据,如果没有实际差异超过1/1000,那么你可以使用tol=1e-3。
https://stackoverflow.com/questions/57086740
复制相似问题