compare values in R when within a string of letters

血红的双手。 提交于 2019-12-25 16:59:17

问题


I need to check if the values of two columns fulfil certain conditions, but the value in one column is within a string of letters.

If the value of CURRENT_ID is equal to the value of CURRENT_TEXT_1 or CURRENT_TEXT_2 plus 2, when CURRENT_TEXT_1 or CURRENT_TEXT_2 are equal to DISPLAY_BOUNDARY, then I need in the OUTPUT column a value of 1, otherwise a value of zero.

Here are some example lines of my datafile (df) and the output I would like to obtain:

 PARTICIPANT     ITEM   CONDITION      CURRENT_TEXT_1               CURRENT_TEXT_2                 CURRENT_ID            OUTPUT
 ppt01          1         1            DISPLAY_BOUNDARY 1 the       iaRegion 4 rd 0 x width 333    7                     0
 ppt01          3         1            iaRegion 2 rd 0 x width 1    DISPLAY_BOUNDARY 9 a           11                    1
 ppt01          4         2            DISPLAY_BOUNDARY 2 aware     iaRegion 6 rd 0 x width 768    3                     0
 ppt01          6         3            DISPLAY_BOUNDARY 3 door      iaRegion 8 rd 0 x width 534    4                     0
 ppt01          9         4            DISPLAY_BOUNDARY 6 in        iaRegion 9 rd 0 x width 924    5                     0
 ppt01          48        5            DISPLAY_BOUNDARY 6 the       iaRegion 10 rd 0 x width 712   8                     1
 ppt02          3         4            iaRegion 14 rd 0 x width 756 DISPLAY_BOUNDARY 15 put        17                    1
 ppt02          7         5            iaRegion 1 rd 0 x width 334  DISPLAY_BOUNDARY 1 where       3                     1
 ppt02          8         6            DISPLAY_BOUNDARY 3 At        iaRegion 2 rd 0 x width 215    5                     1
 ppt02          35        2            iaRegion 3 rd 0 x width 524  DISPLAY_BOUNDARY 1 outside     2                     0
 ppt03          10        1            iaRegion 11 rd 0 x width 190 DISPLAY_BOUNDARY 2 school      4                     1
 ppt03          56        1            DISPLAY_BOUNDARY 8 blue      iaRegion 11 red 0 x width 383  9                     0

My attempt is:

df$OUTPUT <- ifelse(df$CURRENT_ID==((grepl("DISPLAY_BOUNDARY",df$CURRENT_TEXT_1)|grepl("DISPLAY_BOUNDARY",df$CURRENT_TEXT_2))+2, 1, 0)

But I don't know how to extract the value associated with DISPLAY_BOUNDARY. Any help would be appreciated.


回答1:


Something like this, perhaps...

#extract any relevant numeric values from ct1 and ct2
ct1 <- as.numeric(gsub("DISPLAY_BOUNDARY ([0-9]+).*","\\1",df$CURRENT_TEXT_1))  
ct2 <- as.numeric(gsub("DISPLAY_BOUNDARY ([0-9]+).*","\\1",df$CURRENT_TEXT_2))

#use mapply to check each row and return logical value as numeric
df$OUTPUT <- as.numeric(mapply(function(id,x1,x2) id %in% c(x1+2,x2+2),
                               as.numeric(df$CURRENT_ID),ct1,ct2))


来源:https://stackoverflow.com/questions/44289648/compare-values-in-r-when-within-a-string-of-letters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!