我的数据集如下:
Name Age
C_G06_M_15-35 55-64
C_G07_M_15-35 45-54
C_Pili_M_36-44 35-44
C_G14_M_15-35 55-64
C_Pili_M_15-24 45-54
C_Pili_M_25-35 55-64
C_G05_M_15-35 45-54
C_G01_M_15-35 35-44
C_G11_M_15-35 18-24
C_G20_M_15-35 25-34
C_AL006_A_15-60 45-54
C_ALL_F_15-35 35-44
C_South_M_15-24 25-34
但我只需要在“名称”栏中间添加“G13”,“G10”等字符串,如何选择和替换它?
Name Age
G06 55-64
G07 45-54
Pili 35-44
G14 55-64
Pili 45-54
Pili 55-64
G05 45-54
G01 35-44
G11 18-24
G20 25-34
AL006 45-54
ALL 35-44
South 25-34
答案 0 :(得分:1)
你可以尝试
df1$Name <- sub('[^_]+_([^_]+).*', '\\1', df1$Name)
df1
# Name Age
#1 G06 55-64
#2 G07 45-54
#3 Pili 35-44
#4 G14 55-64
#5 Pili 45-54
#6 Pili 55-64
#7 G05 45-54
#8 G01 35-44
#9 G11 18-24
#10 G20 25-34
#11 AL006 45-54
#12 ALL 35-44
#13 South 25-34
df1 <- structure(list(Name = c("C_G06_M_15-35", "C_G07_M_15-35",
"C_Pili_M_36-44",
"C_G14_M_15-35", "C_Pili_M_15-24", "C_Pili_M_25-35", "C_G05_M_15-35",
"C_G01_M_15-35", "C_G11_M_15-35", "C_G20_M_15-35", "C_AL006_A_15-60",
"C_ALL_F_15-35", "C_South_M_15-24"), Age = c("55-64", "45-54",
"35-44", "55-64", "45-54", "55-64", "45-54", "35-44", "18-24",
"25-34", "45-54", "35-44", "25-34")), .Names = c("Name", "Age"
), class = "data.frame", row.names = c(NA, -13L))