这是我的数据框

df2 <- structure(list(Code = c("ICB-9_label_1", "1", "2", "3", 
"4", "5", "1", "ICB-10_label_2", "3", "4", "5", 
"1", "2", "3", "3", "5", "1", "2", 
"3", "4", "5", "1", "2", "3", "4", 
"5", "1", "2", "3", "4", "5", "1", 
"2", "3", "4", "5", "1", "2", "3", 
"4", "5", "1", "2", "3", "4", "5", 
"1"), Description = c("description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here", "description here", "description here", "description here", 
"description here")), row.names = c(NA, -47L), class = c("tbl_df", 
"tbl", "data.frame"))

这是一张桌子的样子:

 Code             Description
ICB-9_label_1     description here          
1                 description here          
2                 description here          
3                 description here          
4                 description here          
5                 description here          
1                 description here          
ICB-10_label_2    description here          
3                 description here          
4                 description here

我想创建第三列"标签".它会一直显示"ICB\u 9\u label\u 1",直到它找到"ICB\u 10\u label\u 2"的行号,然后该列会一直显示"ICB\u 10\u label\u 2".我不想覆盖第一列中的数字,因为1、2、3、4、5值很重要.

推荐答案

有多种方法可以做到这一点.一个选项是提取具有"label"的行,而其他行返回NA,然后使用fill将NA元素更改为以前的非NA值

library(dplyr)
library(tidyr)
library(stringr)
df2 <- df2 %>% 
  mutate(Labels = str_extract(Code, '.*label.*')) %>% 
  fill(Labels, .direction = 'downup') 

-输出

df2
# A tibble: 47 × 3
   Code           Description      Labels        
   <chr>          <chr>            <chr>         
 1 ICB-9_label_1  description here ICB-9_label_1 
 2 1              description here ICB-9_label_1 
 3 2              description here ICB-9_label_1 
 4 3              description here ICB-9_label_1 
 5 4              description here ICB-9_label_1 
 6 5              description here ICB-9_label_1 
 7 1              description here ICB-9_label_1 
 8 ICB-10_label_2 description here ICB-10_label_2
 9 3              description here ICB-10_label_2
10 4              description here ICB-10_label_2
# … with 37 more rows

或将base Rgrepcumsum一起使用

transform(df2, Labels = grep('label', Code, 
       value = TRUE)[cumsum(grepl('label', Code))])

R相关问答推荐

在R中创建一个包含转换和转换之间的时间的列

在"gt"表中添加第二个"groupname_col",而不连接列值

如何动态更新selectizeInput?

将向量组合到一个数据集中,并相应地命名行

在R中使用download. file().奇怪的URL?

计算时间段的ECDF(R)

R中插入符号训练函数的中心因子和尺度因子预测

在R中使用Scale_y_Break后更改y轴标签

R中的时间序列(Ts)函数计数不正确

无法正确设置动态创建的Quarto标注的格式

如何在R中描绘#符号?

在R中按行按列范围查找最大值的名称

将全局环境变量的名称分配给列表中的所有元素

如何平滑或忽略R中变量的微小变化?

减go R中列表的所有唯一元素对

手动指定从相同数据创建的叠加图的 colored颜色

在R中,如何从一系列具有索引名的变量快速创建数据帧?

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

在子图内和子图之间对齐行数不均匀的表格罗布对

如何 suppress 条形图中的零条?