2017-03-22 96 views
4

我是一個新手。我有一列和許多行的數據集。我想將此列轉換爲5列。例如我的數據集是這樣的:將一列轉換爲多列

Column 
---- 
City 
Nation 
Area 
Metro Area 
Urban Area 
Shanghai 
China 
24,000,000 
1230040 
4244234 
New york 
America 
343423 
23423434  
343434 
Etc 

輸出應該是這樣的

City | Nation | Area | Metro City | Urban Area 
----- ------- ------ ------------ ----------- 
Shangai China 2400000 1230040  4244234 
New york America 343423 23423434 343434 

第5行中的數據集(城市,國家,地區等)的需要是5列的名稱,我希望剩下的數據在這5列中填充。請幫忙。

+0

但之後你只剩下3行。你的預期產量將如何? – Sotos

+0

城市和國家的名單繼續。對於那個很抱歉。讓我編輯問題並顯示我的輸出應該如何。 – Abrar

+0

您是否也許錯誤地將數據讀入了R?你是否保證數據每5行更改一次或者可能丟失數據? – A5C1D2H2I1M1N2O1R2T1

回答

4

我要出門的肢體和的數據你,在從網址:https://en.wikipedia.org/wiki/List_of_largest_cities

如果是這種情況,我建議你實際上嘗試重新讀取數據(不知道你是如何將數據轉化爲R的),因爲這可能會讓你的生活更輕鬆。

這裏有一種方法在讀取數據:

library(rvest) 

URL <- "https://en.wikipedia.org/wiki/List_of_largest_cities" 
XPATH <- '//*[@id="mw-content-text"]/table[2]' 

cities <- URL %>% 
    read_html() %>% 
    html_nodes(xpath=XPATH) %>% 
    html_table(fill = TRUE) 

下面介紹一下當前數據的樣子。仍然需要進行清理(請注意,其中一些曾在合併單元格名稱從「行跨度」和種類列):

head(cities[[1]]) 
##  City  Nation Image  Population  Population       Population 
## 1      Image City proper Metropolitan area       Urban area[7] 
## 2 Shanghai  China  24,256,800[8]  34,750,000[9]       23,416,000[a] 
## 3 Karachi Pakistan  23,500,000[10] 25,400,000[11]       25,400,000 
## 4 Beijing  China  21,516,000[12] 24,900,000[13]       21,009,000 
## 5 Dhaka Bangladesh  16,970,105[14]  15,669,000 18,305,671[15][not in citation given] 
## 6 Delhi  India  16,787,941[16]  24,998,000      21,753,486[17] 

從那裏,清理可能是這樣的:

cities <- cities[[1]][-1, ] 
names(cities) <- c("City", "Nation", "Image", "Pop_City", "Pop_Metro", "Pop_Urban") 
cities["Image"] <- NULL 
head(cities) 
cities[] <- lapply(cities, function(x) type.convert(gsub("\\[.*|,", "", x))) 
head(cities) 
#  City  Nation Pop_City Pop_Metro Pop_Urban 
# 2 Shanghai  China 24256800 34750000 23416000 
# 3 Karachi Pakistan 23500000 25400000 25400000 
# 4 Beijing  China 21516000 24900000 21009000 
# 5 Dhaka Bangladesh 16970105 15669000 18305671 
# 6 Delhi  India 16787941 24998000 21753486 
# 7 Lagos Nigeria 16060303 13123000 21000000 
str(cities) 
# 'data.frame': 163 obs. of 5 variables: 
# $ City  : Factor w/ 162 levels "Abidjan","Addis Ababa",..: 133 74 12 41 40 84 66 148 53 102 ... 
# $ Nation : Factor w/ 59 levels "Afghanistan",..: 13 41 13 7 25 40 54 31 13 25 ... 
# $ Pop_City : num 24256800 23500000 21516000 16970105 16787941 ... 
# $ Pop_Metro: int 34750000 25400000 24900000 15669000 24998000 13123000 13520000 37843000 44259000 17712000 ... 
# $ Pop_Urban: num 23416000 25400000 21009000 18305671 21753486 ... 
+0

你是超級聰明的人:-O我從那個確切的網站獲取數據,並且我也使用了rvest。但是我得到的輸出全部在一行中。 data =「https://en.wikipedia.org/wiki/List_of_largest_cities」 data = read_html(data)data = data%>% html_nodes(「td,th」)%>% html_text()這就是我寫了並獲得了數據。但它全部在一個專欄中。 – Abrar

+0

@Abrar,希望它有幫助。我看到你使用了'html_text()',但你正在試圖刮一張桌子。任何理由? – A5C1D2H2I1M1N2O1R2T1

+0

是的,再次感謝您的幫助。我正在嘗試抓取文本,並在我提取表格時忘記將其更改爲html_table()。 – Abrar

5

這裏是一個內襯(考慮到你的column是性格,即df$column <- as.character(df$column)

setNames(data.frame(matrix(unlist(df[-c(1:5),]), ncol = 5, byrow = TRUE)), c(unlist(df[1:5,]))) 

#  City Nation  Area Metro_Area Urban_Area 
#1 Shanghai China 24,000,000 1230040 4244234 
#2 New_york America  343423 23423434  343434 
+0

非常感謝。這工作像一個魅力。 – Abrar