我有一个这样的数据:

   Response Prompt Prompt.Label Comments
1    Friend    who          si3      3_a
2         1    was       si4_en      3_a
3         1    was      si4_nag      3_a
4    Family    who          si3      3_b
5         1    was      si4_nag      3_b
6         1    was       si4_en      3_b
7     other    who          si3      4_a
8         1    was       si4_sp      4_a
9     other    who          si3      4_b
10        1    was       si4_en      4_b
11   Friend    who          si3      5_a
12        1    was       si4_en      5_a
13   Family    who          si3      5_b
14        1    was       si4_en      5_b
data<-structure(list(Response = c("Friend", "1", "1", "Family", "1", 
"1", "other", "1", "other", "1", "Friend", "1", "Family", "1"
), Prompt = c("who", "was", "was", "who", "was", "was", "who", 
"was", "who", "was", "who", "was", "who", "was"), Prompt.Label = c("si3", 
"si4_en", "si4_nag", "si3", "si4_nag", "si4_en", "si3", "si4_sp", 
"si3", "si4_en", "si3", "si4_en", "si3", "si4_en"), Comments = c("3_a", 
"3_a", "3_a", "3_b", "3_b", "3_b", "4_a", "4_a", "4_b", "4_b", 
"5_a", "5_a", "5_b", "5_b")), class = "data.frame", row.names = c(NA, 
-14L))

我希望reshape 数据有两列,包括源和目标.当"Prompt"=="Who"时,"Source"的值应该来自"Response";如果"Comments"列的每个值的"Prompt.Label"的值都是"Prompt.Label",则"Target"应该是"Prompt.Label"的值.例如,对于第一行,Friend代表"源",si4_en代表"目标".

所需数据应为:

  Source  Target
1 Friend  si4_en
2 Friend si4_nag
3 Family si4_nag
4 Family  si4_en
5  other  si4_sp
6  other  si4_en
7 Friend  si4_en
8 Family  si4_en

推荐答案

以下是获得所需输出的一种方法:

library(dplyr)
data %>% 
  group_by(cumsum(Response!=1)) %>% 
  mutate(Response = ifelse(Response==1, first(Response), Response)) %>% 
  filter(row_number() !=1) %>%
  ungroup() %>% 
  select(Response, Target=Prompt.Label)
  Response Target 
  <chr>    <chr>  
1 Friend   si4_en 
2 Friend   si4_nag
3 Family   si4_nag
4 Family   si4_en 
5 other    si4_sp 
6 other    si4_en 
7 Friend   si4_en 
8 Family   si4_en

R相关问答推荐

替换字符的所有实例,但仅限于匹配字符串中

r带有参考年的两年移动平均线

根据列中的数字移动单元格位置

插入指示行之间时间间隔的新行

将一个载体的值相加,直到达到另一个载体的值

将复杂的组合列表转换为数据框架

使用整齐的计算(curl -curl )和杂音

有效识别长载体中的高/低命中

使用带有OR条件的grepl过滤字符串

当我们有多个反斜杠和/特殊字符时使用Gsubing

在R函数中使用加号

KM估计的差异:SvyKm与带权重的调查

在纵向数据集中创建新行

如果COLSUM为>;0,则COLNAME为向量

当每个变量值只能 Select 一次时,如何从数据框中 Select 两个变量的组合?

如何筛选截止年份之前最后一个测量年度的所有观测值以及截止年份之后所有年份的所有观测值

以任意顺序提取具有多个可能匹配项的组匹配项

使用函数从R中的列中删除标高

是否从列中删除★符号?

条形图中的条形图没有try 赋予它们的 colored颜色