713 行を含むがあり、data.frame
その列の 1 つにitemcode
228 の一意のコードがあります。私の質問は、すべての ID の選択肢を作成する方法ですか?
nrow(test.1)
[1] 713
length(unique(test.1$itemcode))
[1] 228
head(test.1)
itemcode ID
2 1180158001 1
225 1180149701 2
264 1180074301 3
522 1180177701 4
732 1180197201 5
1182 1170015601 6
これが私の試用コードです:
test$ID <- 1:nrow(test)
for (i in unique(test$itemcode))
for (j in 1:length(unique(test$itemcode)))
test$choice[test$itemcode == i] <- j
私の望ましい出力は次のようなものになります
itemcode ID choice 2 1180158001 1 1 225 1180149701 2 2 264 1180074301 3 3 522 1180177701 4 4 732 1180197201 5 5 1182 1170015601 6 6 523 1180177701 7 4
これは機能します。しかし、test.1 が test のサブセットであるとしたら? このコードは、テストから基礎となる値を返します。
test$choice <- as.integer( as.factor( test$itemcode ) )