简体   繁体   English

根据 R 中 data.table 中两列的条件过滤范围行的问题

[英]Issue in filtering rows for a range based on conditions from two columns in a data table in R

I have the following data.table (DT) in R. I want to filter the rows for the range from "st_wk" and "end_wk" from the "attrib_wk" column by "Year" and "FIPS_Code".我在 R 中有以下 data.table (DT)。我想通过“年”和“FIPS_代码”从“attrib_wk”列中过滤“st_wk”和“end_wk”范围内的行。

result <- DT[which(DT[,st_wk] == DT[,attrib_wk ]), ]

But, I am unable to get the output shown below using the above code.但是,我无法使用上面的代码获得下面显示的 output。 Can somebody please correct me to achieve the desired output for all the years and FIPS Codes?有人可以纠正我以获得所有年份和 FIPS 代码所需的 output 吗?

Thank you.谢谢。

DT: 
     Year FIPS_Code  attrib_wk rain_3wk   temp_3wk        st_wk        end_wk
     2006      1033     01-03  75.13333    49.41333       05-01         05-04
     2006      1033     01-04  78.83333    49.95333       05-01         05-04
     2006      1033     01-05  82.30000    49.03333       05-01         05-04
     2006      1033     02-01  83.06000    48.57667       05-01         05-04
     2006      1033     02-02  81.65000    44.40667       05-01         05-04
     2006      1033     02-03  80.01667    43.35333       05-01         05-04
     2006      1033     02-04  78.55333    42.79667       05-01         05-04
     2006      1033     02-05  77.32000    44.87333       05-01         05-04
     2006      1033     03-01  76.14333    47.79333       05-01         05-04
     2006      1033     03-02  75.34000    52.99333       05-01         05-04
     2006      1033     03-03  74.73000    56.61000       05-01         05-04
     2006      1033     03-04  75.58000    55.10333       05-01         05-04
     2006      1033     03-05  75.82667    52.63333       05-01         05-04
     2006      1033     04-01  75.67000    54.78667       05-01         05-04
     2006      1033     04-02  75.96333    59.22333       05-01         05-04
     2006      1033     04-03  75.56000    65.33000       05-01         05-04
     2006      1033     04-04  74.84667    68.44000       05-01         05-04
     2006      1033     04-05  73.46667    69.71000       05-01         05-04
     2006      1033     05-01  73.70333    69.04333       05-01         05-04
     2006      1033     05-02  76.00000    67.20667       05-01         05-04
     2006      1033     05-03  77.26333    66.33000       05-01         05-04
     2006      1033     05-04  76.52333    67.79000       05-01         05-04
     2006      1033     05-05  72.71000    72.17000       05-01         05-04
     2006      1033     06-01  69.16333    76.12667       05-01         05-04
     2006      1033     06-02  65.87000    77.30667       05-01         05-04

Output: (for one year: 2006)

 Year    FIPS_Code  attrib_wk rain_3wk   temp_3wk        st_wk        end_wk
 2006      1033     05-01     73.70333    69.04333       05-01         05-04
 2006      1033     05-02     76.00000    67.20667       05-01         05-04
 2006      1033     05-03     77.26333    66.33000       05-01         05-04
 2006      1033     05-04     76.52333    67.79000       05-01         05-04

You can try你可以试试

library(dplyr)图书馆(dplyr)

DT %>% filter(attrib_wk == st_wk)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM