我在R中创建了一个数据文件,其组织方式如下:
> all_data[3945:3952,]
Date btc_close eth_close vix_close gold_close DEXCHUS
3945 2016-11-01 729.27 10.77 18.56 122.73 828
3946 2016-11-02 742.46 NA 19.32 123.64 826
3947 2016-11-03 687.51 NA 22.08 124.30 827
3948 2016-11-04 702.54 NA 22.51 124.39 824
3949 2016-11-05 704.16 NA NA NA NA
3950 2016-11-06 712.24 NA NA NA NA
3951 2016-11-07 704.02 NA 18.71 122.15 835
3952 2016-11-08 709.15 10.87 18.74 121.64 843如何添加具有3个级别的新列?水平是-1表示减少,0表示不变,1表示增加。此direction列应基于btc_close的前一天值。(注意-会有很多NA的-我想根据只有btc_close数据的行来对数据进行子集)
发布于 2017-08-31 16:42:50
删除NA行之后,您可以这样做。
dat$change <- c(0, sign(diff(dat$btc_close)))在这个例子中,您将得到
dat
Date btc_close eth_close vix_close gold_close DEXCHUS change
3945 2016-11-01 729.27 10.77 18.56 122.73 828 0
3946 2016-11-02 742.46 NA 19.32 123.64 826 1
3947 2016-11-03 687.51 NA 22.08 124.30 827 -1
3948 2016-11-04 702.54 NA 22.51 124.39 824 1
3949 2016-11-05 704.16 NA NA NA NA 1
3950 2016-11-06 712.24 NA NA NA NA 1
3951 2016-11-07 704.02 NA 18.71 122.15 835 -1
3952 2016-11-08 709.15 10.87 18.74 121.64 843 1数据
dat <-
structure(list(Date = structure(1:8, .Label = c("2016-11-01",
"2016-11-02", "2016-11-03", "2016-11-04", "2016-11-05", "2016-11-06",
"2016-11-07", "2016-11-08"), class = "factor"), btc_close = c(729.27,
742.46, 687.51, 702.54, 704.16, 712.24, 704.02, 709.15), eth_close = c(10.77,
NA, NA, NA, NA, NA, NA, 10.87), vix_close = c(18.56, 19.32, 22.08,
22.51, NA, NA, 18.71, 18.74), gold_close = c(122.73, 123.64,
124.3, 124.39, NA, NA, 122.15, 121.64), DEXCHUS = c(828L, 826L,
827L, 824L, NA, NA, 835L, 843L)), .Names = c("Date", "btc_close",
"eth_close", "vix_close", "gold_close", "DEXCHUS"), class = "data.frame", row.names = c("3945",
"3946", "3947", "3948", "3949", "3950", "3951", "3952"))发布于 2017-08-31 16:43:10
我建议采取以下策略。您可以使用ifelse比较表中btc_closed列/属性中的值。请记住添加NA值(如果愿意,则添加0),因为您从第2行开始进行比较。
df <- data.frame(btc_close = c(223, 222, 224, 224, 223, 223, 224), stuff = NA)
df$direction <- c(NA, (sapply(2:nrow(df), (function(i){
ifelse(df$btc_close[i] > df$btc_close[(i-1)], 1,
ifelse(df$btc_close[i] < df$btc_close[(i-1)], -1, 0))}))))https://stackoverflow.com/questions/45986119
复制相似问题