因此,我尝试对数据进行模拟,并且数据集包含日期变量。我能够为这些变量生成随机日期。所有日期都在x到y的时间范围内。但是,我需要确保对于每个观察,第3列中的日期都在第5列中的日期之前,依此类推。
这是我用来为每个变量生成日期的代码:
file, err := ioutil.ReadFile("path of genesis")
jsonpriv, err := cdc.MarshalJSON(file)
if err != nil {
return err
}
private.alloc = append(private.alloc, jsonpriv)
genstate, err := codec.MarshalJSONIndent(cdc, privategenesis)
if err != nil {
return err
}
err = ioutil.WriteFile("path to file", genstate, 0644)
if err != nil {
return err
}
return nil
}
genesis file
"coinbase": "0x0000000000000000000000000000000000000000",
"alloc": {
"1d3eaaeade1f26209bd612e18f4786f0ea983eff": {
"balance":
"0x200000000000000000000000000000000000000000000000000000000000000"
},
"201098c849e08993d0b739e1072f43f8063b8e6c": {
"balance":
"0x200000000000000000000000000000000000000000000000000000000000000"
},
"8fff96ef79c64b4693c497f03ccab86ca312b0db": {
"balance":
"0x200000000000000000000000000000000000000000000000000000000000000"
}
},
所以我的问题是如何遍历每一行并确保日期按升序排列?
谢谢:))
答案 0 :(得分:0)
喜欢吗?如果这是您的目标,那么我可以注释代码,以便于理解。另外,将来,当您发布问题时,最好在代码中包含可复制的示例。
set.seed(123)
df1 = data.frame(id = 1:50,
date_1 = sample(seq(as.Date('2011/01/01'), as.Date('2018/01/01'), by="day"),50),
var_1 = sample(1:50, 50),
date_2 = sample(seq(as.Date('2011/01/01'), as.Date('2018/01/01'), by="day"),50),
var_2 = sample(1:50, 50),
date_3 = sample(seq(as.Date('2011/01/01'), as.Date('2018/01/01'), by="day"),50),
var_3 = sample(1:50, 50))
date_pos <- grepl("date", names(df1))
df1[date_pos] <- t(apply(df1[date_pos], 1, sort))
head(df1)
id date_1 var_1 date_2 var_2 date_3 var_3
1 1 2012-09-02 3 2013-01-05 43 2015-03-15 13
2 2 2013-05-01 22 2016-07-08 25 2017-09-26 11
3 3 2013-11-11 39 2014-06-02 19 2015-03-18 29
4 4 2014-08-08 6 2017-03-06 12 2017-09-04 50
5 5 2013-10-25 26 2014-05-18 6 2017-07-29 25
6 6 2011-04-27 10 2017-02-25 18 2017-03-23 36