Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 为什么在使用动态变量名时,按1添加实际上是按2添加?_R_Apache Spark_Dplyr_Sparklyr - Fatal编程技术网

R 为什么在使用动态变量名时,按1添加实际上是按2添加?

R 为什么在使用动态变量名时,按1添加实际上是按2添加?,r,apache-spark,dplyr,sparklyr,R,Apache Spark,Dplyr,Sparklyr,当我运行下面的代码时,我希望Sepal_Width_2列的值是Sepal_Width+1,但实际上它是Sepal_Width+2。有什么好处 require(dplyr) require(sparklyr) Sys.setenv(SPARK_HOME='/usr/lib/spark') sc <- spark_connect(master="yarn") # for this example these variables are hard coded # but

当我运行下面的代码时,我希望Sepal_Width_2列的值是Sepal_Width+1,但实际上它是Sepal_Width+2。有什么好处

require(dplyr)
require(sparklyr)

Sys.setenv(SPARK_HOME='/usr/lib/spark')
sc <- spark_connect(master="yarn")

# for this example these variables are hard coded
# but in my actual code these are named dynamically
sw_name <- as.name('Sepal_Width')
sw2 <- "Sepal_Width_2"
sw2_name <- as.name(sw2)

ir <- copy_to(sc, iris)

print(head(ir %>% mutate(!!sw2 := sw_name))) # so far so good
# Source: spark<?> [?? x 6]
# Sepal_Length Sepal_Width Petal_Length Petal_Width Species Sepal_Width_2
# <dbl>       <dbl>        <dbl>       <dbl> <chr>           <dbl>
# 5.1         3.5          1.4         0.2 setosa            3.5
# 4.9         3            1.4         0.2 setosa            3  
# 4.7         3.2          1.3         0.2 setosa            3.2
# 4.6         3.1          1.5         0.2 setosa            3.1
# 5           3.6          1.4         0.2 setosa            3.6
# 5.4         3.9          1.7         0.4 setosa            3.9

print(head(ir %>% mutate(!!sw2 := sw_name) %>% mutate(!!sw2 := sw2_name + 1))) # i guess 2+2 != 4?
# Source: spark<?> [?? x 6]
# Sepal_Length Sepal_Width Petal_Length Petal_Width Species Sepal_Width_2
# <dbl>       <dbl>        <dbl>       <dbl> <chr>           <dbl>
# 5.1         3.5          1.4         0.2 setosa            5.5
# 4.9         3            1.4         0.2 setosa            5  
# 4.7         3.2          1.3         0.2 setosa            5.2
# 4.6         3.1          1.5         0.2 setosa            5.1
# 5           3.6          1.4         0.2 setosa            5.6
# 5.4         3.9          1.7         0.4 setosa            5.9
require(dplyr)
需要(年)
Sys.setenv(SPARK_HOME='/usr/lib/SPARK')
sc[?x 6]
#萼片长度萼片宽度花瓣长度花瓣宽度种萼片宽度2
#                                   
#5.1 3.5 1.4 0.2 setosa 5.5
#4.9 3 1.4 0.2刚毛5
#4.7 3.2 1.3 0.2 setosa 5.2
#4.6 3.1 1.5 0.2 setosa 5.1
#5.3.6 1.4 0.2 setosa 5.6
#5.4 3.9 1.7 0.4 setosa 5.9
我的用例要求我使用上面提到的动态变量命名。在本例中,这相当愚蠢(与直接使用变量相比),但在我的用例中,我在数百个不同的spark表中运行相同的函数。它们在列数和每列是什么(来自某些机器学习模型的输出)方面都有相同的“模式”,但名称不同,因为每个表包含不同模型的输出。这些名称是可预测的,但由于它们不同,因此我按照您在这里看到的动态构造它们,而不是硬编码它们


Spark似乎知道如何在名称硬编码时将2和2相加,但当名称是动态的时,它会突然崩溃。

您可能误用了
as.name
,这导致
Sparkyr
误解您的输入

请注意,仅处理本地表时出现的代码错误:


sw_name我不确定哪个软件包是罪魁祸首(Sparkyr,dplyr,R,谁知道),但当我从3.6.3/Sparkyr 1.5升级到R 4.0.2/Sparkyr 1.7.0时,这个问题已经解决了。

增加0.5可以增加1,但这个行为很奇怪……谢谢你的建议。语法是合法的,在Sparkyr中“起作用”,因为它可以运行,但是奇怪的行为仍然存在。我将尝试不同的sym和!!看看会发生什么。感谢您链接非标准评估指南。很清楚,但我会努力克服的。