Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使字符串变量适应R中的特定特征_R_String - Fatal编程技术网

使字符串变量适应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
我想对Colu
code
执行以下两项操作: 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
from
base 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")