使用虚拟变量从另一个变量

时间:2017-04-04 10:44:59

标签: r if-statement

我有一个包含18个变量的数据集,我正在使用r。我的数据如下所示:( Decision_date是第一个变量)

1 2003-07-09 00:00:00.0      Austria  Agriculture and Rural Development
2 2002-03-20 00:00:00.0      Austria  Agriculture and Rural Development
3 2004-07-07 00:00:00.0      Austria  Agriculture and Rural Development
4 2003-10-06 00:00:00.0      Austria  Agriculture and Rural Development
5 2004-07-07 00:00:00.0      Austria  Agriculture and Rural Development
6 2003-10-06 00:00:00.0      Austria  Agriculture and Rural Development
# ... with 15 more variables: Title <chr>, Decision_type <chr>, Active_infringement_cases <chr>,
#   Not_communicated <chr>, dir_number <chr>, delegating_dir <dbl>, implementing_dir <dbl>, closure <dbl>,
#   let <int>, ro <int>, referral <int>, let2 <int>, ro2 <int>, sanction <dbl>, withdrawal <dbl>

复制数据

structure(list(Decision_date = c("2003-07-09 00:00:00.0", "2002-03-20 00:00:00.0", 
"2004-07-07 00:00:00.0", "2003-10-06 00:00:00.0", "2004-07-07 00:00:00.0", 
"2003-10-06 00:00:00.0", "2003-12-16 00:00:00.0", "2003-10-06 00:00:00.0", 
"2004-07-07 00:00:00.0", "2003-10-06 00:00:00.0"), Member_state = c("Austria", 
"Austria", "Austria", "Austria", "Austria", "Austria", "Austria", 
"Austria", "Austria", "Austria"), Policy_area___Department_in_charge = c("Agriculture and Rural Development", 
"Agriculture and Rural Development", "Agriculture and Rural Development", 
"Agriculture and Rural Development", "Agriculture and Rural Development", 
"Agriculture and Rural Development", "Agriculture and Rural Development", 
"Agriculture and Rural Development", "Agriculture and Rural Development", 
"Agriculture and Rural Development"), Title = c("CODE RELATIF A L'EXERCICE DES PROFESSIONS ARTISANALES, COMMERCIALES ET INDUSTRIELLES", 
"CODE RELATIF A L'EXERCICE DES PROFESSIONS ARTISANALES, COMMERCIALES ET INDUSTRIELLES", 
"PRODUITS DE CACAO ET DE CHOCOLAT DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION", 
"PRODUITS DE CACAO ET DE CHOCOLAT DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION", 
"DIRECTIVE 2001/110/CE DU CONSEIL DU 20 D<U+00C9>CEMBRE 2001 RELATIVE AU MIEL", 
"DIRECTIVE 2001/110/CE DU CONSEIL DU 20 D<U+00C9>CEMBRE 2001 RELATIVE AU MIEL", 
"DIR 2001/111/CE DU CONSEIL DU 20/12/01 RELATIVE <U+00C0> CERTAINS SUCRES DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION HUMAINE", 
"DIR 2001/111/CE DU CONSEIL DU 20/12/01 RELATIVE <U+00C0> CERTAINS SUCRES DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION HUMAINE", 
"JUS DE FRUITS ET <U+00C0> CERTAINS PRODUITS SIMILAIRES DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION HUMAINE", 
"JUS DE FRUITS ET <U+00C0> CERTAINS PRODUITS SIMILAIRES DESTIN<U+00C9>S <U+00C0> L'ALIMENTATION HUMAINE"
), Decision_type = c("Closing of the case", "Formal notice Art. 258 TFEU", 
"Closing of the case", "Formal notice Art. 258 TFEU", "Closing of the case", 
"Formal notice Art. 258 TFEU", "Closing of the case", "Formal notice Art. 258 TFEU", 
"Closing of the case", "Formal notice Art. 258 TFEU"), Active_infringement_cases = c("No", 
"No", "No", "No", "No", "No", "No", "No", "No", "No"), Not_communicated = c("No", 
"No", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"), 
    dir_number = c("", "", "", "", "2001/110", "2001/110", "2001/111", 
    "2001/111", "", ""), delegating_dir = c(0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), implementing_dir = c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0), closure = c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0), let = c(0L, 
    1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L)), .Names = c("Decision_date", 
"Member_state", "Policy_area___Department_in_charge", "Title", 
"Decision_type", "Active_infringement_cases", "Not_communicated", 
"dir_number", "delegating_dir", "implementing_dir", "closure", 
"let"), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

我创建了许多新的虚拟变量。例如,我的一个虚拟变量名为&#34; let&#34;。

let1 <- ifelse(infringements$Decision_type == "Formal notice Art. 258            
TFEU", yes = TRUE, no = FALSE)
let2 <- ifelse(infringements$Decision_type == "Formal notice Art. 106   
TFEU", yes = TRUE, no = FALSE)
let3 <- ifelse(infringements$Decision_type == "Formal notice Art. 258  
TFEU + Press release", yes = TRUE, no = FALSE)
let4 <- ifelse(infringements$Decision_type == "Formal notice Art. 260      
TFEU", yes = TRUE, no = FALSE)
 let5 <- ifelse(infringements$Decision_type ==  "Formal notice Art. 260     
 TFEU + Press release", yes = TRUE, no = FALSE)
 table(let2)

 infringements$let <- let1 + let2 + let3 + let4 + let5

如果&#34;让&#34; is = to 1,我希望它从另一个名为&#34; Decision_date&#34;的变量中提取信息。采用以下格式:

2003-07-09 00:00:00.0

我有这样的事情:

 subSet <- infringements[infringements$let == 1,] 
infringements$let_date <- infringements$Decision_date[infringements$let           
 == 1]

但是我得到以下错误术语:

  

subSet&lt; - 侵权[侵权$ let == 1,]   错误:变量必须为长度1或288825。   问题变量:&#39; Decision_date&#39;,&#39; Member_state&#39;,&#39; Policy_area ___ Department_in_charge&#39;,&#39; Title&#39;,&#39; Decision_type&#39;,&# 39; Active_infringement_cases&#39;,&#39; Not_communicated&#39;,&#39; delegating_dir&#39;,&#39; implement_dir&#39;,&#39;关闭&#39;,&#39;让&#39; ;,&#39; ro&#39;,&#39;推荐&#39;,&#39; let2&#39;,&#39; ro2&#39;,&#39;制裁&#39;,&#39; ;戒断&#39;   侵权$ let_date&lt; - 侵权$ Decision_date [侵权$ let == 1]   $<-.data.frame*tmp*,&#34; let_date&#34;,值= c(&#34; 2002-03-20 00:00:00.0&#34;,:     替换有19255行,数据有45165

换句话说:我想要一个完整的数据集,它看起来像这样:

let            let_date
1              2003-07-09 00:00:00.0
1              2004-07-09 00:00:00.0
1              2005-07-09 00:00:00.0

任何帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

我也是R的新手。我过去常常按照其他语言的方式来做。也许这会对你有所帮助:

if(infringements$let == 1) {
  infringements$let_date <- infringements$Decision_date
}  


修改digEmAllRonak Shah提供了良好的解决方案方法。如果我找对你,这应该为你做到:

infringements$let_date <- infringements$Decision_date[infringements$let == 1]

<强> EDIT2 : 使用上面可重现的代码,我设法使用以下代码行:

df <- data.frame(infringements$let[infringements$let == 1], infringements$Decision_date[infringements$let == 1])
相关问题