我有一个ID长度为18的向量
mydata$ID=rep(c("WT1","WT2", "WT3", "KO1", "KO2", "KO3"), each=3)
[1] "WT1" "WT1" "WT1" "WT2" "WT2" "WT2" "WT3" "WT3" "WT3" "KO1" "KO1" "KO1" "KO2" "KO2" "KO2" "KO3"
[17] "KO3" "KO3"
我想创建一个具有相同元素顺序的新向量,但需要单独使用,如下所示:
WT1 WT2 WT3 KO1 KO2 KO3
我试过了:
mydata$ID=factor(unique(mydata$ID))
但它返回:
[1] WT1 WT2 WT3 KO1 KO2 KO3 WT1 WT2 WT3 KO1 KO2 KO3 WT1 WT2 WT3 KO1 KO2 KO3
答案 0 :(得分:2)
让我们保持这个简单:
unique(mydata$ID)
[1] "WT1" "WT2" "WT3" "KO1" "KO2" "KO3"
答案 1 :(得分:-1)
安装sqldf包。然后做
newVector <- sqldf('select distinct(ID) from mydata');