R 为什么不是';在内部工作时是否发生变异?

R 为什么不是';在内部工作时是否发生变异?,r,dplyr,mutate,case-when,R,Dplyr,Mutate,Case When,我一直在R与战争的相关数据集有关争端和战争。 我一直在尝试变异一个“stateadem”变量,它告诉我们stateadem是否基于民主 在他们的身份证号码上 为了做到这一点,我使用了一个很长的case_when语句。 然而,当我查看数据帧时,与Russia作为州a的一些争议在stateadem中显示为1! 我很不明白为什么会这样。 我有dplyr 8.5 install.packages("dplyr") ... library(dplyr) df<-read.dta13("dyads1.

我一直在R与战争的相关数据集有关争端和战争。 我一直在尝试变异一个“stateadem”变量,它告诉我们stateadem是否基于民主 在他们的身份证号码上

为了做到这一点,我使用了一个很长的case_when语句。 然而,当我查看数据帧时,与Russia作为州a的一些争议在stateadem中显示为1! 我很不明白为什么会这样。 我有dplyr 8.5

install.packages("dplyr")
...
library(dplyr)
df<-read.dta13("dyads1.dta",
               nonint.factors = T,
               generate.factors = T)

df_use <- df_use %>% 
  mutate(stateadem = case_when(is.na(statea) ~ -1,
                               statea == 2 ~ 1,
                               statea == 20 ~ 1,
                               statea == 40 ~ 0,
                               statea == 41 ~ 0,
                               ((statea == 42)&(year>1980)) ~ 1,
                               ((statea == 42)&(year<=1980)) ~ 1,
                               statea == 51 ~ 1,
                               statea == 52 ~ 1,
                               statea == 53 ~ 1,
                               statea == 54 ~ 1,
                               statea == 55 ~ 1,
                               statea == 56 ~ 1,
                               statea == 57 ~ 1,
                               statea == 58 ~ 1,
                               ((statea == 70)&(year>2000)) ~ 1,
                               ((statea == 70)&(year <= 2000))~0,
                               statea == 80 ~ 1,
                               ((statea == 90)&(year>1997)) ~ 1,
                               ((statea == 90)&(year <= 1997))~0,
                               ((statea == 91)&(year>1982)) ~ 1,
                               ((statea == 91)&(year <= 1982))~0,
                               ((statea == 92)&(year>1991)) ~ 1,
                               ((statea == 92)&(year <= 1991))~0,
                               ((statea == 93)&(year>1991)) ~ 1,
                               ((statea == 93)&(year <= 1991))~0,
                               statea == 94 ~ 1,
                               ((statea == 95)&(year>1991)) ~ 1,
                               ((statea == 95)&(year <= 1991))~0,
                               ((statea == 100)&(year>1959)) ~ 1,
                               ((statea == 100)&(year <= 1959))~0,
                               ((statea == 101)&(year<1959)) ~ 0,
                               ((statea == 101)&(year >= 1959)&(year<2002))~1,
                               ((statea == 101)&(year >= 2002))~0,
                               ((statea == 110)&(year<1994)) ~ 0,
                               ((statea == 110)&(year >=1994))~1,
                               statea == 115 ~ 0,
                               ((statea == 130)&(year<1981)) ~ 0,
                               ((statea == 130)&(year>=1981)&(year<2005)) ~ 1,
                               ((statea == 130)&(year>= 2005)) ~ 1,
                               ((statea == 135)&(year<1981)) ~ 0,
                               ((statea == 135)&(year>=1981)&(year<1991))~1,
                               ((statea == 135)&(year>=1991)&(year<2000))~0,
                               ((statea == 135)&(year>=2000))~1,
                               ((statea == 140)&(year<1985))~0,
                               ((statea == 140)&(year>=1985))~1,
                               ((statea == 145)&(year<1984))~0,
                               ((statea == 145)&(year>=1984))~1,
                               ((statea == 150)&(year<1994))~0,
                               ((statea == 150)&(year>=1994))~1,
                               ((statea == 155)&(year<1991))~0,
                               ((statea == 155)&(year>=1991))~1,
                               ((statea == 160)&(year<1985))~0,
                               ((statea == 160)&(year>=1985))~1,
                               ((statea == 165)&(year<1954)) ~ 0,
                               ((statea == 165)&(year>=1954)&(year<1971))~1,
                               ((statea == 165)&(year>=1971)&(year<1987))~0,
                               ((statea == 165)&(year>=1987))~1,
                               ((statea = 200)&(year > 1884)) ~ 1,
                               ((statea = 200)&(year < 1884)) ~ 0,
                               statea == 205 ~ 1,
                               statea == 210 ~ 1,
                               statea == 211 ~ 1,
                               statea == 212 ~ 1,
                               ((statea == 220)&(year<1956)) ~ 1,
                               ((statea == 220)&(year>=1956)&(year<=1970)) ~ 1,
                               ((statea == 220)&(year>1970)) ~ 1,
                               statea == 225 ~ 1,
                               ((statea == 230)&(year<1980)) ~ 0,
                               ((statea == 230)&(year>=1980)) ~ 1,
                               ((statea == 235)&(year<1976)) ~ 0,
                               ((statea == 235)&(year>=1976)) ~ 1,
                               statea == 240 ~ 1,
                               statea == 245 ~ 1,
                               ((statea == 255)&(year > 1990)) ~ 1,
                               ((statea == 255)&(year <= 1990)) ~ 0,
                               statea == 260 ~ 1,
                               statea == 265 ~ 0,
                               statea == 267 ~ 1,
                               statea == 269 ~ 1,
                               statea == 271 ~ 1,
                               statea == 273 ~ 0,
                               statea == 275 ~ 0,
                               statea == 280 ~ 0,
                               ((statea == 290)&(year>1990)) ~ 1,
                               ((statea == 290)&(year<=1990)) ~ 0,
                               statea ==300 ~ 0,
                               statea == 305 ~ 1,
                               ((statea == 310)&(year > 1990)) ~ 1,
                               ((statea == 310)&(year <= 1990)) ~ 0,
                               ((statea == 315)&(year <=1990)&(year>1950)) ~ 0,
                               ((statea == 315)&(year <=1950)) ~ 1,
                               ((statea == 315)&(year >1990)) ~ 1,
                               statea == 316 ~ 1,
                               statea == 317 ~ 1,
                               ((statea == 325)&(year <1948)) ~ 0,
                               ((statea == 325)&(year >=1948)) ~ 1,
                               statea == 327 ~ 0,
                               statea == 329 ~ 0,
                               statea == 332 ~ 0,
                               statea == 337 ~ 1,
                               statea == 338 ~ 0,
                               ((statea == 339)&(year > 2000)) ~ 1,
                               ((statea == 339)&(year <= 2000)) ~ 0,
                               statea == 343 ~ 1,
                               ((statea == 344)&(year>2000)) ~ 1,
                               ((statea == 344)&(year <= 2000)) ~ 0,
                               statea == 345 ~ 0,
                               statea == 346 ~ 0,
                               ((statea == 350)&(year >= 1976)) ~ 1,
                               ((statea == 350)&(year < 1950)) ~ 1,
                               ((statea == 350)&(year >= 1950)&(year<1976)) ~ 0,
                               statea == 352 ~ 1,
                               ((statea == 355)&(year<1991)) ~ 0,
                               ((statea == 355)&(year >= 1991)) ~ 1,
                               ((statea == 359)&(year>= 1994)) ~ 1,
                               ((statea == 359)&(year< 1994)) ~ 0,
                               ((statea == 360)&(year<1991)) ~ 0,
                               ((statea == 360)&(year >= 1991)) ~ 1,
                               statea == 365 ~ 0,
                               statea == 366 ~ 1,
                               statea == 367 ~ 1,
                               statea == 368 ~ 1,
                               statea == 369 ~ 1,
                               statea == 370 ~ 0,
                               statea == 371 ~ 0,
                               ((statea == 372)&(year > 2003 ))~1,
                               ((statea == 372)&(year <= 2003 ))~0,
                               statea == 373 ~ 0,
                               statea == 375 ~ 1,
                               statea == 380 ~ 1,
                               statea == 385 ~ 1,
                               statea == 390 ~ 1,
                               statea == 395 ~ 1,
                               statea == 404 ~ 0,
                               statea == 411 ~ 0,
                               ((statea == 420)&(year>1994)) ~ 0,
                               ((statea == 420)&(year<=1994)) ~ 1,
                               ((statea == 432)&(year<1992)) ~ 0,
                               ((statea == 432)&(year >= 1992)) ~ 1,
                               ((statea == 433)&(year<2000)) ~ 0,
                               ((statea == 433)&(year >= 2000)) ~ 1,
                               ((statea == 434)&(year<1991)) ~ 0,
                               ((statea == 434)&(year >= 1991)) ~ 1,
                               statea ==435 ~ 0,
                               statea == 436 ~ 0,
                               statea == 437 ~ 0,
                               statea == 438 ~ 0,
                               statea == 439 ~ 0,
                               statea == 450 ~ 0,
                               statea == 451 ~ 0,
                               ((statea == 452)&(year<2005)) ~ 0,
                               ((statea == 452)&(year>=2005)) ~ 1,
                               statea == 461  ~ 0,
                               statea == 471 ~ 0,
                               statea == 475 ~ 0,
                               statea == 481 ~ 0,
                               statea == 482 ~ 0,
                               statea == 483 ~ 0,
                               statea == 484 ~ 0,
                               statea == 490 ~ 0,
                               statea == 500 ~ 0,
                               ((statea == 501)&(year<=2002)) ~ 0,
                               ((statea == 501)&(year >2002)) ~ 1,
                               statea == 510 ~ 0,
                               statea == 511 ~ 0,
                               ((statea == 516)&(year<=2006)) ~ 0,
                               ((statea == 516)&(year >2006)) ~ 1,
                               statea ==517 ~ 0,
                               ((statea == 520)&(year<=1966)) ~ 1,
                               ((statea == 520)&(year >1966)) ~ 0,
                               statea == 522 ~ 0,
                               statea == 530 ~ 0,
                               statea == 531 ~ 0,
                               statea == 540 ~ 0,
                               statea == 541 ~ 0,
                               statea ==551 ~ 0,
                               statea == 552 ~ 0,
                               statea == 553 ~ 0,
                               ((statea == 560)&(year<1994)) ~ 0,
                               ((statea == 560)&(year>=1994)) ~ 1,
                               statea == 565 ~ 1,
                               ((statea == 570)&(year<1995)) ~ 0,
                               ((statea == 570)&(year>=1995)) ~ 1,
                               statea == 571 ~ 1,
                               statea == 572 ~ 0,
                               ((statea == 580)&(year<1991)) ~ 0,
                               ((statea == 580)&(year >= 1991)) ~ 1,
                               statea == 600 ~ 0,
                               statea == 615 ~ 0,
                               statea == 616 ~ 0,
                               statea == 620 ~ 0,
                               statea == 625 ~ 0,
                               statea == 626 ~ 0,
                               statea == 630 ~ 0,
                               ((statea == 640)&
                                 ((year == 1954)|(year==1955)|(year == 1956)|(year==1957)|(year == 1958)|(year==1960)|
                                    (year==1961)|(year = 1971)|(year==1972)|(year==1980)|(year==1982)|(year==1983)|(year==1984)))~0,
                               statea == 640 ~ 1,
                               statea == 645 ~ 0,
                               statea == 651 ~ 0,
                               statea == 652 ~ 0,
                               ((statea == 660)&(year<=2005)) ~ 0,
                               ((statea == 660)&(year>2005)) ~ 1,                               
                               statea == 663 ~ 0,
                               statea == 666 ~ 1,
                               statea == 670 ~ 0,
                               statea == 678 ~ 0,
                               statea == 679 ~ 0,
                               statea == 680 ~ 0,
                               statea == 690 ~ 0,
                               statea == 692 ~ 0,
                               statea == 694 ~ 0,
                               statea == 696 ~ 0,
                               statea == 698 ~ 0,
                               statea == 700 ~ 0,
                               statea == 701 ~ 0,
                               statea == 702 ~ 0,
                               ((statea == 703)&(year<=2010)) ~ 0,
                               statea == 704 ~ 0,
                               statea == 705 ~ 0,
                               statea == 710 ~ 0,
                               ((statea == 713)&(year<=1991)) ~ 0,
                               ((statea == 713)&(year>1991)) ~ 1,          
                               ((statea == 730)&(year<=1991)) ~ 0,
                               ((statea == 730)&(year>1991)) ~ 1,          
                               statea == 731 ~ 0,
                               ((statea == 732)&(year<=1991)) ~ 0,
                               ((statea == 732)&(year>1991)) ~ 1,
                               ((statea == 740)&(year<=1950)) ~ 0,
                               ((statea == 740)&(year>1950)) ~ 1,  
                               statea == 750 ~ 1,
                               ((statea == 770)&(year<1971)) ~ 0,
                               ((statea == 770)&(year >= 1971)&(year<1977)) ~ 1,
                               ((statea == 770)&(year >= 1977)&(year<1990))~0,
                               ((statea == 770)&(year >= 1990)&(year<2000))~1,
                               ((statea == 770)&(year >= 2000)&(year<=2009))~0,
                               ((statea == 770)&(year>2009))~1,
                               ((statea == 771)&(year<1972)) ~ 1,
                               ((statea == 771)&(year >= 1972)&(year<=1990))~0,
                               ((statea == 771)&(year > 1990)&(year<=2005))~1,
                               ((statea == 771)&(year>2005))~0,
                               ((statea == 775)&(year<=1962)) ~ 1,
                               ((statea == 775)&(year>1962)) ~ 0,
                               ((statea == 780)&(year<=1984)) ~ 1,
                               ((statea == 780)&(year>1984)) ~ 0,
                               statea == 790 ~ 0,
                               ((statea == 800)&(year<1991)) ~ 0,
                               ((statea == 800)&(year >= 1991)&(year<2005))~1,
                               ((statea == 800)&(year>=2005))~0,
                               statea == 811 ~ 0,
                               ((statea == 812)&(year<=1960)) ~ 1,
                               ((statea == 812)&(year>1960)) ~ 0,
                               statea == 816 ~ 0,
                               statea == 817 ~ 0,
                               ((statea == 820)&(year<=1969)) ~ 1,
                               ((statea == 820)&(year>1969)) ~ 0,
                               statea == 830 ~ 0,
                               ((statea == 840)&(year<=1990)) ~ 0,
                               ((statea == 840)&(year>1990)) ~ 1,
                               ((statea ==850)&(year< 2000)) ~ 0,
                               ((statea == 850)&(year>=2000)) ~ 1,
                               statea == 900 ~ 1,
                               statea == 910 ~ 0,
                               statea == 920 ~ 1,
                               statea == 940 ~ 1,
                               statea == 986 ~ 1,
                               TRUE ~ -1
  ))                       

install.packages(“dplyr”)
...
图书馆(dplyr)
df1980))~1,
((statea==42)及(year2000)),
((州=70)和(1997年))~1,
((州=90)和(1982年))~1,
((statea==91)和(1991年))~1,
((statea==92)和(1991年))~1,
((statea==93)和(1991年))~1,
((statea==95)和(1959年))~1,
((statea==100)和(1884年))~1,
((州a=200)和(年份<1884))~0,
statea==205~1,
statea==210~1,
statea==211~1,
statea==212~1,
((statea=220)和(year=1956)和(year=1970))~1,
statea==225~1,
((州=230)和(年份=1980))~1,
((statea=235)和(year=1976))~1,
statea==240~1,
statea==245~1,
((statea==255)和(年份>1990))~1,
((statea==255)和(1990年))~1,
((statea==290)和(1990年))~1,
((statea==310)和(2000年))~1,
((statea==339)和(2000年))~1,
((statea=344)和(year=1976))~1,
((州=350)和(年份<1950))~1,
((州=350)和(年份>=1950)和(年份=1994))~1,
((statea==359)和(1994年以前))~0,
((statea=360)和(year=1991))~1,
statea==365~0,
statea==366~1,
statea==367~1,
statea==368~1,
statea==369~1,
statea==370~0,
statea==371~0,
((statea==372)和(年份>2003))~1,
((statea==372)和(1994年))~0,

((statea==420)和(year试着给我举一个可复制的例子,或者有人可以帮助你

或者至少说明哪个ID代表俄罗斯,或者“错误”数据框的打印

不管怎么说,这种情况非常严重,很容易出错

我认为,要解决您的问题,可以尝试以下方法:

  • 创建一个列表,名字是“statea”,价值观是这个国家民主生活的年代
  • 我为我的数据是1980年到2020年的案例创建了一个虚假的我的清单

  • 创建一个空的新列,valor对于第一个实例并不重要

  • 现在,您将进入一个代表每个国家的表格,检查年份,如果年份是否在my_列表(代表民主)中,则将其归为isDem

  • 我希望这能奏效

    my_list <- list(`51` = c(1980:1985, 2000:2020),
                   `52` = c(1980:2020),
                   `54` = c(1980:2000, 2007:2020))
    
    df$isDem <- NA
    
    for(country in names(my_list)){
      my_country = as.numeric(country) # This because the name of a list is a character
    
      df <- df %>%
        dplyr::mutate(isDem = case_when((statea == my_country) & (year %in% my_list[[country]]) ~ 1,
                                         (statea == my_country) & !(year %in% my_list[[country]]) ~ 0,
                                          TRUE ~ isDem
                                )
                      )
    }
    

    我的清单听起来像是你的
    案例中的一些情况,当
    陈述是错误的。但是我们没有办法检查这些情况,因为我们不知道正确的人口统计/非人口统计指标应该是什么。作为一个可能错误的例子,看看案例6和7:
    ((statea==42)和(year>1980))~1
    和`(statea==42)和(年份)