的多個熔體我想重塑以下data.tabledata.table使用模式
library(data.table)
myfun <- function() sample(c(NA,round(runif(9)*10)),prob=c(0.2,rep(0.1,9)))
cheeze <- myfun()
bottle <- myfun()
df <- as.data.table(data.frame(ID=LETTERS[1:10],
bottle_qty=bottle,
bottle_price=bottle*c(1,3,5),
cheeze_qty=cheeze,
cheeze_price=cheeze*c(5,4,2),
cheeze_cam = 1*(cheeze>4) ,
cheeze_brie = 1*(cheeze<=4),
bottle_wine = 1*(bottle>5),
bottle_beer = 1*(bottle<=5))
)
# ID bottle_qty bottle_price cheeze_qty cheeze_price cheeze_cam cheeze_brie
# 1: A 7 7 9 45 1 0
# 2: B 4 12 6 24 1 0
# 3: C NA NA NA NA NA NA
# 4: D 7 7 2 10 0 1
# 5: E 3 9 9 36 1 0
# 6: F 9 45 4 8 0 1
# 7: G 6 6 3 15 0 1
# 8: H 2 6 6 24 1 0
# 9: I 5 25 8 16 1 0
# 10: J 7 7 3 15 0 1
# bottle_wine bottle_beer
# 1: 1 0
# 2: 0 1
# 3: NA NA
# 4: 1 0
# 5: 0 1
# 6: 1 0
# 7: 1 0
# 8: 0 1
# 9: 0 1
# 10: 1 0
爲以下:
| ID | type | qty | price |
| A | cheeze_cam | 9 | 45 |
| A | bottle_wine | 7 | 7 |
| B | bottle_beer | 4 | 12 |
| B | cheeze_cam | 6 | 24 |
編輯 這是充滿期望的輸出。
| ID | type | qty | price |
|----+-------------+-----+-------|
| A | bottle_wine | 7 | 7 |
| A | cheeze_cam | 9 | 45 |
| B | bottle_beer | 4 | 12 |
| B | cheeze_cam | 6 | 24 |
| C | bottle_wine | NA | NA |
| C | cheeze_brie | NA | NA |
| D | bottle_wine | 7 | 7 |
| D | cheeze_brie | 2 | 10 |
| E | bottle_beer | 3 | 9 |
| E | cheeze_cam | 9 | 36 |
| F | bottle_wine | 9 | 45 |
| F | cheeze_brie | 4 | 8 |
| G | bottle_wine | 6 | 6 |
| G | cheeze_brie | 3 | 15 |
| H | bottle_beer | 2 | 6 |
| H | cheeze_cam | 6 | 24 |
| I | bottle_beer | 5 | 25 |
| I | cheeze_cam | 8 | 16 |
| J | bottle_wine | 7 | 7 |
| J | cheeze_brie | 3 | 15 |
但找不到x對象。請幫忙嗎?
嘗試'熔體(熔融(DF,測量=模式( 「數量$」, 「價格:$」),value.name = C( '數量', '價格'),可變.name =「var」,na.rm = TRUE),id.var = c('ID','var','qty','price'),na.rm = TRUE)[order(ID)]' – akrun
不錯,謝謝。實際上你最初的建議非常有趣。我建立它來產生這個'熔化(df,id.var =「ID」,measure = patterns(「cheeze_qty $」,「cheeze_price $」),na.rm = TRUE)'。但是lapply似乎不能立即工作 – DJJ
@akrun解決方案中有哪些不起作用?我不明白「lapply」問題來自哪裏......你能否確定你的問題? – cderv