我在R中有一个数据帧,如下所示:
variable1 .... variable2
ab1_2 ......... 123
cde_1 .......... 456
fgh_1 .......... 789
ab1_1 ......... 012
fgh_2 .......... 345
我正在尝试创建一个新列,该新列基于variable1的前三个字符分配一个值(或变量)。像这样:
variable1 .... variable2 .... variable3
ab1_2 ......... 123 ............. 1
cde_1 .......... 456 ............. 2
fgh_1 .......... 789 .......... 3
ab1_1 ......... 012 ............. 1
fgh_2 .......... 345 ............. 3
有人对我如何做到这一点有任何提示吗?
谢谢您的帮助。
卡尔文
答案 0 :(得分: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)