0

我在 sparkR 中有 DataFrame 'res'。'res' 包含 ID 和日期。所以第一个条目看起来像这样 'ID' = 1 2 3 ... 和 'date' = "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) 时出现错误:消息:“returnstatus==0 is not True”。

4

1 回答 1

1

在您之前的问题(在 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 .

于 2015-08-18T05:35:32.053 回答