![](/img/trans.png)
[英]Reshape from wide to long with multiple columns that have different naming patterns
[英]Reshape long to wide where most columns have multiple values
我有如下數據:
IDnum zipcode City County State
10011 36006 Billingsley Autauga AL
10011 36022 Deatsville Autauga AL
10011 36051 Marbury Autauga AL
10011 36051 Prattville Autauga AL
10011 36066 Prattville Autauga AL
10011 36067 Verbena Autauga AL
10011 36091 Selma Autauga AL
10011 36703 Jones Autauga AL
10011 36749 Plantersville Autauga AL
10011 36758 Uriah Autauga AL
10011 36480 Atmore Autauga AL
10011 36502 Bon Secour Autauga AL
我有一個郵政編碼列表,它們包含的城市以及它們所在的縣/州。IDnum =縣和州的數字值,總和。 列表具有您現在看到的格式,我需要將其從長到寬/從垂直到水平的形狀進行重塑,其中IDnum變量成為唯一標識符,所有其他可能的值組合都變成寬變量。
IDnum zip1 city1 county1 state1 zip2 city2 county2
10011 36006 Billingsley Autauga AL 36022 Deatsville Autauga
這只是數據集的示例,它涵蓋了美國的每個郵政編碼,並包含更多變量。 我見過其他與此類似的問題和答案,但是幾乎沒有每一列中都有多個值的地方。
SPSS和STATA中有一些命令將以這種方式重塑數據,在SPSS中,我可以運行“將數據從原數據集中的11個變量轉換為大約1750個”的“從結構/案例到變量”命令,一個縣有超過290個郵政編碼,並且可以復制其他大多數變量都超過290次。 這將創建許多空白,但是我需要將其重塑為一個非常長的水平文件。
我看過reshape和reshape2,並且掛在“默認為長度”錯誤消息上。 我確實進行了分類工作,但這會創建一個變量,該變量是所有值的列表,而不是為每個值創建變量。
melted_dupes <- melt(zip_code_list_dupes, id.vars= c("IDnum"))
HRZ_dupes <- dcast(melted_dupes, IDnum ~ variable, fun.aggregate = list)
我嘗試過tidyr和dplyr,但是在語法上迷路了。 令我感到有些驚訝的是,沒有一個命令的數據類似於其他軟件包中內置命令的數據,這使我想起了,而我只是沒有弄清楚。
任何幫助表示贊賞。
在通過IDnum
添加連續計數后,可以使用基本函數reshape
來執行此IDnum
。 假設您的數據存儲在名為df
的data.frame
:
df2 <- within(df, count <- ave(rep(1,nrow(df)),df$IDnum,FUN=cumsum))
提供連續計數的新列,名稱為“時間”。 現在我們可以reshape
為寬幅
reshape(df2,direction="wide",idvar="IDnum",timevar="count")
IDnum zipcode.1 City.1 County.1 State.1 zipcode.2 City.2 County.2 State.2 zipcode.3 City.3 County.3 State.3 zipcode.4 City.4 County.4 State.4 1 10011 36006 Billingsley Autauga AL 36022 Deatsville Autauga AL 36051 Marbury Autauga AL 36051 Prattville Autauga AL
(輸出被截斷,一直到zipcode.12等)
可能有一種更有效的方法,但是請嘗試以下方法。 我使用了自己的(示例)數據集,與您的數據集非常相似。 逐步運行該過程以查看其工作方式,因為您必須修改代碼中的某些內容。
library(dplyr)
library(tidyr)
# get example data
dt = data.frame(id = c(1,1,1,2,2),
zipcode = c(4,5,6,7,8),
city = c("A","B","C","A","C"),
county = c("A","B","C","A","C"),
state = c("A","B","C","A","C"))
dt
# id zipcode city county state
# 1 1 4 A A A
# 2 1 5 B B B
# 3 1 6 C C C
# 4 2 7 A A A
# 5 2 8 C C C
# get maximum number of rows for a single id
# this will help you get the wide format
max_num_rows = max((dt %>% count(id))$n)
# get names of columns to reshape
col_names = names(dt)[-1]
dt %>%
group_by(id) %>%
mutate(nrow = paste0("row",row_number())) %>%
unite_("V",col_names) %>%
spread(nrow, V) %>%
unite("z",matches("row")) %>%
separate(z, paste0(col_names, sort(rep(1:max_num_rows, ncol(dt)-1))), convert=T) %>%
ungroup()
# # A tibble: 2 × 13
# id zipcode1 city1 county1 state1 zipcode2 city2 county2 state2 zipcode3 city3 county3 state3
# * <dbl> <int> <chr> <chr> <chr> <int> <chr> <chr> <chr> <int> <chr> <chr> <chr>
# 1 1 4 A A A 5 B B B 6 C C C
# 2 2 7 A A A 8 C C C NA <NA> <NA> <NA>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.