根据R [复制]中的前3个字符进行分类

问题描述 投票:0回答:1

我在R中有一个数据框,如下所示:


variable1 .... variable2

ab1_2 ......... 123

cde_1 .......... 456

fgh_1 .......... 789

ab1_1 ......... 012

fgh_2 .......... 345


我正在尝试创建一个新列,根据variable1的前3个字符分配值(或变量)。所以像这样:


variable1 .... variable2 .... variable3

ab1_2 ......... 123 ............. 1

cde_1 .......... 456 ............. 2

fgh_1 .......... 789 ............. 3

ab1_1 ......... 012 ............. 1

fgh_2 .......... 345 ............. 3


有没有人有关于如何实现这一目标的任何提示?

提前谢谢你的帮助。

卡尔文

r
1个回答
0
投票

看看这个,让我知道 -

#This is your table, original_data, I made so I can test my code
original_data<-data.frame(Variable1=c("ab1_2","cde_1","fgh_1","ab1_1","fgh_2"),variable2=c(123,456,789,012,345))

#This extracts the first 3 letter and creates a new column with only these 3 letters
original_data<-cbind(original_data,join_column=substr(sample$Variable1,start = 1,stop=3))

#This is the data that contains which 3 letters correspond to which number
join_data<-data.frame(join_column=c("ab1","cde","fgh"),assigned_number=c(1,2,3))

#Finally you join and Voila!
final_data<-merge(original_data,join_data,all.x = TRUE)
© www.soinside.com 2019 - 2024. All rights reserved.