首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从CET时间到当地时间

从CET时间到当地时间
EN

Stack Overflow用户
提问于 2015-07-21 15:11:47
回答 2查看 26关注 0票数 0

我有这些数据

代码语言:javascript
复制
> dff_all[1:10,c(2,3)]
   cet_hour_of_registration country_id
1                        20         SE
2                        12         SE
3                        11         SE
4                        15         GB
5                        12         SE
6                        14         BR
7                        23         MX
8                        13         SE
9                         1         BR
10                        9         SE

我想用本地时间创建一个变量$hour。对话内容如下,从CET到本地时间的变化如下

FI+1. MX-7.英国-1。BR-5

我试着用嵌套的IF来做。没能挺过来。

EN

回答 2

Stack Overflow用户

发布于 2015-07-21 15:33:36

代码语言:javascript
复制
#Create a data lookup table
country_id <- c("FI", "MX", "UK", "BR", "SE")
time_diff <- c(1,-7,-1,-5, 0)

df <- data.frame(country_id, time_diff)


#this is a substitute data frame for your data. 
hour_reg <- c(20,12,11,15,5)

dff_all <- data.frame(country_id, hour_reg)

#joing the tables with dplyr function -> or with base join (double check join type for your needs)
library(dplyr)
new_table <- join(dff_all, df)

#make new column
mutate(new_table, hour = hour_reg - time_diff)



  #output
 country_id hour_reg time_diff hour
1         FI       20         1   19
2         MX       12        -7   19
3         UK       11        -1   12
4         BR       15        -5   20
5         SE        5         0    5
票数 0
EN

Stack Overflow用户

发布于 2015-07-21 15:46:45

基本包:

代码语言:javascript
复制
# A variation of the example provided by vinchinzu

# Original table
country_id <- c("FI", "MX", "UK", "BR", "SE", "SP", "RE")
hour_reg <- c(20, 12, 11, 15, 5, 3, 7)
df1 <- data.frame(country_id, hour_reg)

# Lookup table
country_id <- c("FI", "MX", "UK", "BR", "SE")
time_diff <- c(1, -7, -1, -5, 0)
df2 <- data.frame(country_id, time_diff)

# We merge them and calculate a new column
full <- merge(df1, df2, by = "country_id", all.x = TRUE)
full$hour <- full$hour - full$time_diff
full

输出,如果在查找表中没有该国家,我们将得到NA:

代码语言:javascript
复制
  country_id hour_reg time_diff hour
1         BR       15        -5   20
2         FI       20         1   19
3         MX       12        -7   19
4         RE        7        NA   NA
5         SE        5         0    5
6         SP        3        NA   NA
7         UK       11        -1   12

如果我们想显示没有NA的所有行:

代码语言:javascript
复制
full[complete.cases(full), ]

若要将NA替换为零:

代码语言:javascript
复制
full[is.na(full)] <- 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31543015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档