Subset and recombine dataframes using data.table package or other solutions [R]

后端 未结 1 1151
野性不改
野性不改 2021-01-14 03:13

I am quite new to R and have a question about subset and recombine between two dataframe using range value of one of the variable. So i have my two dataframes like this :

相关标签:
1条回答
  • 2021-01-14 03:56

    This isn't possible in data.table nicely. It's FR#203 to implement. You could try package xts as I think that has this operation.

    One long and clunky way (untested) in data.table is as follows. Say your first table is P and the 2nd table containing the ranges is R.

    setkey(P,x)
    # sort by x and mark as sorted so future queries can use binary search on P
    
    from = P[J(R$min_x),which=TRUE]
    # Lookup each min_x in the key of P, returning the location. J stands for Join.
    
    to = P[J(R$max_x),which=TRUE]
    # Lookup each max_x in the key of P, returning the location.
    
    len = to-from+1
    # vectorized for each item the length to[i]-from[i]+1
    
    i = unlist(mapply("seq.int",from,to,SIMPLIFY=FALSE))
    # for each item the sequence from[i]:to[i], then concat them all into one vector
    
    cbind(rep(R$id,len), P[i])
    # use len to expand the items of R to match what they match to in P
    
    0 讨论(0)
提交回复
热议问题