欲樞轉result
列df
水平創建與一個單獨的行的數據組爲每 region
,state
,county
組合,其中列由year
然後city
排序。粘貼值
我也想找出新的數據通過region
,state
和county
設置每一行和刪除四個results
列之間的空白。下面的代碼完成了所有這些,但我懷疑它不是非常有效。
有沒有辦法做到這一點與reshape2
沒有創建每個組的唯一標識符和每組內的編號觀察?有沒有辦法使用apply來代替for循環來從矩陣中去除空白區域? (矩陣的使用方式不同於數學或編程結構。)我意識到這是兩個不同的問題,也許我應該單獨發佈每個問題。
鑑於我可以達到預期的效果,並且只是希望改進代碼,我不知道是否應該發佈此代碼,但我希望能夠學習。感謝您的任何建議。
df <- read.table(text= "
region state county city year result
1 1 1 1 1 1
1 1 1 2 1 2
1 1 1 1 2 3
1 1 1 2 2 4
1 1 2 3 1 4
1 1 2 4 1 3
1 1 2 3 2 2
1 1 2 4 2 1
1 2 1 1 1 0
1 2 1 2 1 NA
1 2 1 1 2 0
1 2 1 2 2 0
1 2 2 3 1 2
1 2 2 4 1 2
1 2 2 3 2 2
1 2 2 4 2 2
2 1 1 1 1 9
2 1 1 2 1 9
2 1 1 1 2 8
2 1 1 2 2 8
2 1 2 3 1 1
2 1 2 4 1 0
2 1 2 3 2 1
2 1 2 4 2 0
2 2 1 1 1 2
2 2 1 2 1 4
2 2 1 1 2 6
2 2 1 2 2 8
2 2 2 3 1 3
2 2 2 4 1 3
2 2 2 3 2 2
2 2 2 4 2 2
", header=TRUE, na.strings=NA)
desired.result <- read.table(text= "
region state county results
1 1 1 1234
1 1 2 4321
1 2 1 0.00
1 2 2 2222
2 1 1 9988
2 1 2 1010
2 2 1 2468
2 2 2 3322
", header=TRUE, colClasses=c('numeric','numeric','numeric','character'))
# redefine variables for package reshape2 creating a unique id for each
# region, state, county combination and then number observations in
# each of those combinations
library(reshape2)
id.var <- df$region*100000 + df$state*1000 + df$county
obsnum <- sequence(rle(id.var)$lengths)
df2 <- dcast(df, region + state + county ~ obsnum, value.var = "result")
# remove spaces between columns of results matrix
# with a for-loop. How can I use apply to do this?
x <- df2[,4:(4+max(obsnum)-1)]
# use a dot to represent a missing observation
x[is.na(x)] = '.'
x.cat = numeric(nrow(x))
for(i in 1:nrow(x)) {
x.cat[i] = paste(x[i,], collapse="")
}
df3 <- cbind(df2[,1:3],x.cat)
colnames(df3) <- c("region", "state", "county", "results")
df3
df3 == desired.result
編輯:
馬修倫德伯格的下面的答案是優秀的。之後,我意識到我還需要創建一個輸出數據集,其中上面的四個結果列包含數字,有理數,並用空格分隔。所以,我已經發布了一個明顯的方式來做到這一點,這改變了馬修的答案。我不知道這是否是可以接受的協議,但是新的方案似乎與原始文章緊密相關,因此我認爲我不應該發佈新的問題。
謝謝你傑出的答案。後來我意識到我還需要一個輸出數據集,其中四個結果列是數字的,並由空格分隔。我無法修改你的答案,但我靠近了,並在此發佈了代碼。 –