我在sparkR有DataFrame 'res‘。“‘res”包含ID和日期。第一个条目看起来是'ID‘=12,3.“日期”= "2012-6-5“、"2013-5-5”、"2015-10-11“.
我想要创建一个新的数据集,其中所有的‘日期’被减去"2010-01-01“。这是如何做到的呢?如果我只想用整数减去DataFrame中的所有元素,我就会遇到同样的问题。
在sparkR我试过这个
newres <- withColumn(res, "subtract", res$date - as.Date("2010-01-01") )这是运行,但是当我输入head(newres)时,会得到一个错误:mesage:"returnstatus==0不是真“。
发布于 2015-08-18 05:35:32
在前面的问题(Convert string to date in sparkR)中,我看到类型转换不是在R中执行的问题,而不是SparkR中的问题。在我的设置中,我可以将所有内容转换为R中的整数,并在SparkR中进行如下减法:
df <- data.frame(user_id=c(1,1,2,2),
time=c("2015-7-10","2015-8-04","2015-8-8","2015-7-10"))
df$time <- as.Date(df$time)
df$time <- as.numeric(df$time)
date <- as.numeric(as.Date("2010-01-01"))
res <- createDataFrame(sqlContext, df)
newRes <- withColumn(res, "subtract",res$time - date)
collect(newRes)这给了我
user_id time subtract
1 1 16626 2016
2 1 16651 2041
3 2 16655 2045
4 2 16626 2016我希望这有用,因为你说你也有整数减法的问题.这个解决方案的唯一“问题”是R中的时间转换:现在您只能使用完全适合于记忆R环境的DataFrames。
https://stackoverflow.com/questions/32049717
复制相似问题