如果R数据帧中的列包含用下划线分隔的字符串值,并且扩展了也包含公用值的列值的大小,则明智的做法是一次从所有值中删除下划线符号以及常见。这将帮助我们正确读取数据,并使分析变得容易。为此,我们可以使用gsub函数
请看以下数据帧-
set.seed(191) ID<-c("ID_1","ID_2","ID_3","ID_4","ID_5","ID_6","ID_7","ID_8","ID_9","ID_10","ID_11","ID_12","ID_13","ID_14","ID_15","ID_16","ID_17","ID_18","ID_19","ID_20") Salary<-sample(20000:50000,20) df1<-data.frame(ID,Salary) df1
输出结果
ID Salary 1 ID_1 33170 2 ID_2 22747 3 ID_3 42886 4 ID_4 22031 5 ID_5 45668 6 ID_6 32584 7 ID_7 34779 8 ID_8 20471 9 ID_9 38689 10 ID_10 29660 11 ID_11 49664 12 ID_12 24284 13 ID_13 36537 14 ID_14 37693 15 ID_15 30265 16 ID_16 36004 17 ID_17 48247 18 ID_18 20750 19 ID_19 27400 20 ID_20 20553
从列ID中的ID值中删除下划线之前和包括下划线在内的所有内容-
df1$ID<-gsub("^.*\\_","",df1$ID) df1
输出结果
ID Salary 1 1 48769 2 2 26002 3 3 37231 4 4 24437 5 5 43311 6 6 47494 7 7 21029 8 8 28069 9 9 41108 10 10 29363 11 11 23371 12 12 25898 13 13 42434 14 14 22210 15 15 48969 16 16 21640 17 17 36175 18 18 21210 19 19 43374 20 20 29367
让我们看另一个例子-
Group<-c("GRP_1","GRP_2","GRP_3","GRP_4","GRP_5","GRP_6","GRP_7","GRP_8","GRP_9","GRP_10","GRP_11","GRP_12","GRP_13","GRP_14","GRP_15","GRP_16","GRP_17","GRP_18","GRP_19","GRP_20") Ratings<-sample(0:10,20,replace=TRUE) df2<-data.frame(Group,Ratings) df2
输出结果
Group Ratings 1 GRP_1 6 2 GRP_2 9 3 GRP_3 7 4 GRP_4 10 5 GRP_5 10 6 GRP_6 9 7 GRP_7 9 8 GRP_8 3 9 GRP_9 2 10 GRP_10 0 11 GRP_11 3 12 GRP_12 7 13 GRP_13 6 14 GRP_14 10 15 GRP_15 1 16 GRP_16 3 17 GRP_17 10 18 GRP_18 2 19 GRP_19 9 20 GRP_20 0
从组Group的GRP值中删除下划线之前和包括下划线在内的所有内容-
df2$Group<-gsub("^.*\\_","",df2$Group) df2
输出结果
Group Ratings 1 1 4 2 2 8 3 3 7 4 4 0 5 5 10 6 6 10 7 7 5 8 8 4 9 9 3 10 10 7 11 11 4 12 12 4 13 13 3 14 14 10 15 15 7 16 16 2 17 17 3 18 18 8 19 19 9 20 20 5