如何基于R数据帧或数据表中其他列中的值将值分配给列

时间:2019-06-26 18:34:29

标签: r dataframe datatable recode

希望您能在这里为我提供帮助。我找到了很多基本的重新编码答案,但是没有一个我可以适应我的问题。 问题是:对于所有行,我想为该行的parent_concept列与ID列匹配的每一行将Level_eng_1列设置为colum verb_e的值。

我试图调整stackoverflow上现有的众多解决方案,但没有成功。

这里是数据。

structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), verb_e = c("act", "change", "move", "support", "regulate", 
"interact", "structure", "communicate", "time", "make", "decrease", 
"increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
"separate", "transmit"), Level_eng_1 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 19L), class = "data.frame")

所需的输出是:

structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), verb_e = c("act", "change", "move", "support", "regulate", 
"interact", "structure", "communicate", "time", "make", "decrease", 
"increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
"separate", "transmit"), Level_eng_1 = c("act", "change", "move", 
"act", "act", "act", "act", "act", "change", "change", "change", 
"change", "change", "change", "move", "move", "move", "move", 
"move")), row.names = c(NA, 19L), class = "data.frame")

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助

library(dplyr)
input %>% 
    mutate(Level_eng_1 = verb_e[match(Parent_Concept, ID)], 
           Level_eng_1 = case_when(is.na(Level_eng_1) ~ verb_e, TRUE  ~Level_eng_1))
#    ID Parent_Concept         verb_e Level_eng_1
#1   1              0            act         act
#2   2              0         change      change
#3   3              0           move        move
#4  11              1        support         act
#5  12              1       regulate         act
#6  13              1       interact         act
#7  14              1      structure         act
#8  16              1    communicate         act
#9  20              2           time      change
#10 21              2           make      change
#11 22              2       decrease      change
#12 23              2       increase      change
#13 24              2         modify      change
#14 25              2      orientate      change
#15 30              3         motion        move
#16 31              3        inhibit        move
#17 32              3 bring_together        move
#18 33              3       separate        move
#19 34              3       transmit        move

数据

input <-  structure(list(ID = c(1, 2, 3, 11, 12, 13, 14, 16, 20, 21, 22, 
23, 24, 25, 30, 31, 32, 33, 34), Parent_Concept = c(0L, 0L, 0L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), verb_e = c("act", "change", "move", "support", "regulate", 
"interact", "structure", "communicate", "time", "make", "decrease", 
"increase", "modify", "orientate", "motion", "inhibit", "bring_together", 
"separate", "transmit"), Level_eng_1 = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, 
19L), class = "data.frame")