2017-08-16 79 views
0

在R工作室,我正在尋找創建國家名稱的矢量。他們被包圍在我的數據集列1 Countryvec給人因子名追加矢量不給名稱

"Australia Australia ..." 

X只是給俄羅斯,全國36個的名字,國家最終被

1,1,...,2,2,...,4,4.. etc. 

他們也不會爲了,3最終在42和43之間。我如何使這些數字成爲因素?

gdppc=read.xlsx("H:/dissertation/ALL/YAS.xlsx",sheetIndex = 1,startRow = 1) 
countryvec=gdppc[,1] 
country=c() 
for (j in 1:43){ 
    x=rep(countryvec[j],25) 
    country=append(country,x) 
} 

回答

0

您需要獲取水平屬性

set.seed(7) 

v <- factor(letters[rbinom(20, 10, .5)]) 
> c(v) 
[1] 6 4 2 2 3 5 3 6 2 4 2 3 5 2 4 2 4 1 6 3 

> levels(v)[v] 
[1] "h" "e" "c" "c" "d" "f" "d" "h" "c" "e" "c" "d" "f" "c" "e" "c" "e" "a" "h" "d" 

你可能需要修改代碼以在循環中:

x <- rep(levels(countryvec)[countryvec][j], 25) 

或轉換前的環矢量:

countryvec <- levels(countryvec)[countryvec] 
+0

嗨,轉換vec tor之前工作很好,謝謝 –