行列互轉(zhuǎn)問(wèn)題
先前總結(jié)過(guò)用MySQL解決行列互轉(zhuǎn)問(wèn)題的基本思路:
http://www.reibang.com/p/5e1fdd34a739 [2022-10-07-mysql學(xué)習(xí)]行列互轉(zhuǎn)問(wèn)題
最近打算認(rèn)真學(xué)學(xué)tidyR地淀,發(fā)現(xiàn)用R解決這個(gè)問(wèn)題非常高效。
示例數(shù)據(jù)
id | revenue | month |
---|---|---|
1 | 8000 | Jan |
2 | 9000 | Jan |
3 | 1000 | Feb |
1 | 7000 | Feb |
1 | 6000 | Mar |
數(shù)據(jù)集department
行轉(zhuǎn)列
new_department<-spread(department,key=month,value=revenue)
new_department
運(yùn)行結(jié)果:
id | Feb | Jan | Mar |
---|---|---|---|
1 | 7000 | 8000 | 6000 |
2 | NA | 9000 | NA |
3 | 1000 | NA | NA |
列轉(zhuǎn)行
gather(new_department,key=month,value=revenue,-id)
運(yùn)行結(jié)果:
id | month | revenue |
---|---|---|
1 | Feb | 7000 |
2 | Feb | NA |
3 | Feb | 1000 |
1 | Jan | 8000 |
2 | Jan | 9000 |
3 | Jan | NA |
1 | Mar | 6000 |
2 | Mar | NA |
3 | Mar | NA |
通過(guò)na.rm參數(shù)去除NA值:
gather(new_department,key=month,value=revenue,-id,na.rm=TRUE)
運(yùn)行結(jié)果:
id | month | revenue |
---|---|---|
1 | Feb | 7000 |
3 | Feb | 1000 |
1 | Jan | 8000 |
2 | Jan | 9000 |
1 | Mar | 6000 |
附注
示例數(shù)據(jù)原本的形式又被稱為整潔格式或長(zhǎng)數(shù)據(jù)消略,而行轉(zhuǎn)列后的數(shù)據(jù)則被稱為寬松格式或?qū)挃?shù)據(jù)觅够,前者是與tidy R適配度更高的數(shù)據(jù)格式颂斜,但后者也有結(jié)構(gòu)清晰巩割、便于記錄等優(yōu)點(diǎn)莫杈。