Need to vectorize solution that using nested loops (transform data frame from long to wide format)

浪子不回头ぞ 提交于 2020-01-06 21:45:08

问题


I have following data frame and need to do transformation from long into wide format:

symbol side price
1      A    B     1
2      A    S     2
3      B    B     3
4      C    B     4
5      B    S     5

Explanation: for every symbol must exists two lines with side B and side S. Need to find these lines and transform them into wide format:

[symbol, first-comed side (B or S), price of side B, price of side S]

If one line exists but another is missing, then put NA value to appropriate price value. For example if line with side B exists, but with side S is missing, then put NA to price of side S.

Output results must be following:

  symbol side price_B price_S
1      A    B      1      2
2      B    B      3      5
3      C    B      4     NA

For symbols A and B we have lines with sides A and B so we transform them without NA's. Side B was first in order then we put only B side to "side" column. For symbol C we have only side B but not side S so we put NA value to the "price_S" column.

How to vectorize it?


回答1:


reshape gets the prices:

prices <- reshape(x, direction='wide', idvar='symbol', timevar='side', v.names='price', sep='')
prices
##   symbol priceB priceS
## 1      A      1      2
## 3      B      3      5
## 4      C      4     NA

aggregate gets the first price:

first <- aggregate(side ~ symbol, data=x, FUN=head, n=1)
first
##   symbol side
## 1      A    B
## 2      B    B
## 3      C    B

merge puts them together:

merge(first, prices)
##   symbol side priceB priceS
## 1      A    B      1      2
## 2      B    B      3      5
## 3      C    B      4     NA


来源:https://stackoverflow.com/questions/23975941/need-to-vectorize-solution-that-using-nested-loops-transform-data-frame-from-lo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!