根据原籍国添加小时数

时间:2016-05-04 09:17:41

标签: r countries

所以我有一个列显示每个Facebook帖子的原籍国。我也有时间创建帖子。我想做的是为每个阿拉伯帖子增加3个小时。然后我想为俄罗斯邮政增加5个小时。我们说我有12个国家,需要添加12个不同的时区。

语言是R。

allCountries <- c("Arabia" , "Argentina", "Brazil", "Global", "India","Malaysia",
                          "Mexico","Poland","Sweden","Turkey","MaxTurkey","Ukraine","US")

1 个答案:

答案 0 :(得分:1)

鉴于

(df <- data.frame(datetime=rep(Sys.time(), 3), origin=c("arabian", "russian", "us")))
#              datetime  origin
# 1 2016-05-04 11:27:52 arabian
# 2 2016-05-04 11:27:52 russian
# 3 2016-05-04 11:27:52      us
offset <- c("arabian"=3, "russian"=5)

然后

transform(df, datetime=datetime + offset[origin]*60*60)
#              datetime  origin
# 1 2016-05-04 14:26:12 arabian
# 2 2016-05-04 16:26:12 russian
# 3                <NA>      us

transform(df, datetime=datetime + ifelse(origin %in% names(offset), offset[df$origin]*60*60, 0))
#              datetime  origin
# 1 2016-05-04 14:27:52 arabian
# 2 2016-05-04 16:27:52 russian
# 3 2016-05-04 11:27:52      us