来自其他data.table的列按一个data.table排序的列

时间:2018-03-21 12:36:54

标签: r data.table

我有两个data.tables A:

contract.name contract.start contract.end  price 
Q1-2019       2019-01-01     2019-04-01    10
Q2-2019       2019-04-01     2019-07-01    12
Q3-2019       2019-07-01     2019-10-01    11
Q4-2019       2019-10-01     2020-01-01    13

和B:

contract delivery.begin  delivery.end  bid   ask
Q2-2018  2018-04-01      2018-06-30    9.8   10.5
Q3-2018  2018-07-01      2018-09-30    11.5  12.1
Q4-2018  2018-10-01      2018-12-31    10.5  11.3
Q1-2019  2019-01-01      2019-03-31    12.8  13.5

我想要一个带有B的出价值的向量,这个向量由来自A的contract.name值排序,如下所示:

bid = c(12.8, 0, 0, 0)

3 个答案:

答案 0 :(得分:1)

library(data.table)

DT.A <- data.table(structure(list(contract.name = structure(1:4, .Label = c("Q1-2019",
"Q2-2019", "Q3-2019", "Q4-2019"), class = "factor"), contract.start = structure(1:4, .Label = c("2019-01-01",
"2019-04-01", "2019-07-01", "2019-10-01"), class = "factor"),
    contract.end = structure(1:4, .Label = c("2019-04-01", "2019-07-01",
    "2019-10-01", "2020-01-01"), class = "factor"), price = c(10L,
    12L, 11L, 13L)), .Names = c("contract.name", "contract.start",
"contract.end", "price"), class = "data.frame", row.names = c(NA,
-4L)))


DT.B <- data.table(structure(list(contract = structure(c(2L, 3L, 4L, 1L), .Label = c("Q1-2019",
"Q2-2018", "Q3-2018", "Q4-2018"), class = "factor"), delivery.begin = structure(1:4, .Label = c("2018-04-01",
"2018-07-01", "2018-10-01", "2019-01-01"), class = "factor"),
    delivery.end = structure(1:4, .Label = c("2018-06-30", "2018-09-30",
    "2018-12-31", "2019-03-31"), class = "factor"), bid = c(9.8,
    11.5, 10.5, 12.8), ask = c(10.5, 12.1, 11.3, 13.5)), .Names = c("contract",
"delivery.begin", "delivery.end", "bid", "ask"), class = "data.frame", row.names = c(NA,
-4L)))

# Get vector of contract names
orderVals <- DT.A$contract.name

# Key table B by contract
setkey(DT.B, contract)

# Extract rows from table B with the specified key values
output <- DT.B[.(orderVals)]

# Change the values where there was no match from NA to 0
output[is.na(bid), bid := 0]

# Get desired vector
output$bid

答案 1 :(得分:1)

希望这有帮助!

df1 %>%
  left_join(df2, by=c("contract.name"="contract")) %>%
  select(bid) %>%
  replace_na(list(bid=0)) %>%
  as.character()

输出为:

"c(12.8, 0, 0, 0)"

示例数据:

df1 <- structure(list(contract.name = c("Q1-2019", "Q2-2019", "Q3-2019", 
"Q4-2019"), contract.start = c("2019-01-01", "2019-04-01", "2019-07-01", 
"2019-10-01"), contract.end = c("2019-04-01", "2019-07-01", "2019-10-01", 
"2020-01-01"), price = c(10L, 12L, 11L, 13L)), .Names = c("contract.name", 
"contract.start", "contract.end", "price"), class = "data.frame", row.names = c(NA, 
-4L))

df2 <- structure(list(contract = c("Q2-2018", "Q3-2018", "Q4-2018", 
"Q1-2019"), delivery.begin = c("2018-04-01", "2018-07-01", "2018-10-01", 
"2019-01-01"), delivery.end = c("2018-06-30", "2018-09-30", "2018-12-31", 
"2019-03-31"), bid = c(9.8, 11.5, 10.5, 12.8), ask = c(10.5, 
12.1, 11.3, 13.5)), .Names = c("contract", "delivery.begin", 
"delivery.end", "bid", "ask"), class = "data.frame", row.names = c(NA, 
-4L))

答案 2 :(得分:1)

你可以这样做:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="div_1">
  <span id="span_1">Span 1 text</span>
  <span id="span_2">Span 2 text</span>
  <span id="span_3">Span 3 text</span>
  <span id="span_4">Span 4 text</span>  
</div>

或(没有library("data.table") A <- fread( "contract.name contract.start contract.end price Q1-2019 2019-01-01 2019-04-01 10 Q2-2019 2019-04-01 2019-07-01 12 Q3-2019 2019-07-01 2019-10-01 11 Q4-2019 2019-10-01 2020-01-01 13") B <- fread( "contract delivery.begin delivery.end bid ask Q2-2018 2018-04-01 2018-06-30 9.8 10.5 Q3-2018 2018-07-01 2018-09-30 11.5 12.1 Q4-2018 2018-10-01 2018-12-31 10.5 11.3 Q1-2019 2019-01-01 2019-03-31 12.8 13.5") setnames(B, "contract", "contract.name") A[B, on="contract.name", bid:=bid][, ifelse(is.na(bid), 0, bid)] # > A[B, on="contract.name", bid:=bid][, ifelse(is.na(bid), 0, bid)] # [1] 12.8 0.0 0.0 0.0 的变体):

ifelse()