使字符串变量适应R中的特定特征
我有以下数据:使字符串变量适应R中的特定特征,r,string,R,String,我有以下数据: id code 1 I560 2 K980 3 R30 4 F500 5 650 我想对Colucode执行以下两项操作: i) 选择字母和后面的两个数字 ii)删除不以字母开头的观察结果。因此,最终,数据帧应如下所示: id code 1 I56 2 K98 3 R30 4 F50 在base R中,您可以执行以下操作: subset(transform(df, code = sub('([A-Z]\\d{2}).*', '\\1', code)),
id code
1 I560
2 K980
3 R30
4 F500
5 650
我想对Colucode
执行以下两项操作:
i) 选择字母和后面的两个数字
ii)删除不以字母开头的观察结果。因此,最终,数据帧应如下所示:
id code
1 I56
2 K98
3 R30
4 F50
在base R中,您可以执行以下操作:
subset(transform(df, code = sub('([A-Z]\\d{2}).*', '\\1', code)),
grepl('^[A-Z]', code))
或者使用tidyverse
功能
library(dplyr)
library(stringr)
df %>%
mutate(code = str_extract(code, '[A-Z]\\d{2}')) %>%
filter(str_detect(code, '^[A-Z]'))
# id code
#1 1 I56
#2 2 K98
#3 3 R30
#4 4 F50
带有
substr
frombase R
df1$code <- substr(df1$code, 1, 3)
df1[grepl('^[A-Z]', df1$code),]
# id code
#1 1 I56
#2 2 K98
#3 3 R30
#4 4 F50
df1$code
df1 <- structure(list(id = 1:5, code = c("I56", "K98", "R30", "F50",
"650")), row.names = c(NA, -5L), class = "data.frame")