减去sparkR中一列中的所有元素
Subtract all elements in a column in sparkR
我在 sparkR 中有 DataFrame 'res'。 'res' 包含 ID 和日期。所以第一个条目看起来像这样 'ID' = 1 2 3 ... and 'date' = "2012-6-5", "2013-5-5", "2015-10-11" 。 ..
我想创建一个新数据集,其中所有 'dates' 都减去“2010-01-01”。如何才能做到这一点?
如果我只想用整数减去 DataFrame 中的所有元素,我会遇到完全相同的问题。
我在 sparkR 中试过这个
newres <- withColumn(res, "subtract", res$date - as.Date("2010-01-01") )
这个 运行 但是当我输入 head(newres) 我得到一个 error:mesage: "returnstatus==0 is not True."
在您之前的问题 (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
我希望这能奏效,因为你说你在整数减法方面也有问题......唯一的 "problem" 这个解决方案是 R 中的时间转换:现在你只能使用适合的数据帧完全是为了纪念您的 R 环境。
我在 sparkR 中有 DataFrame 'res'。 'res' 包含 ID 和日期。所以第一个条目看起来像这样 'ID' = 1 2 3 ... and 'date' = "2012-6-5", "2013-5-5", "2015-10-11" 。 ..
我想创建一个新数据集,其中所有 'dates' 都减去“2010-01-01”。如何才能做到这一点? 如果我只想用整数减去 DataFrame 中的所有元素,我会遇到完全相同的问题。
我在 sparkR 中试过这个
newres <- withColumn(res, "subtract", res$date - as.Date("2010-01-01") )
这个 运行 但是当我输入 head(newres) 我得到一个 error:mesage: "returnstatus==0 is not True."
在您之前的问题 (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
我希望这能奏效,因为你说你在整数减法方面也有问题......唯一的 "problem" 这个解决方案是 R 中的时间转换:现在你只能使用适合的数据帧完全是为了纪念您的 R 环境。