第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

一次重塑多個(gè)值

一次重塑多個(gè)值

交互式愛情 2019-09-24 16:47:21
我有一個(gè)很長的數(shù)據(jù)集,我想擴(kuò)大范圍,我很好奇是否有一種方法可以使用R中的reshape2或tidyr包一步完成所有這些工作。數(shù)據(jù)框df如下所示:id  type    transactions    amount20  income       20          10020  expense      25          9530  income       50          30030  expense      45          250我想得到這個(gè):id  income_transactions expense_transactions    income_amount   expense_amount20       20                           25                 100             9530       50                           45                 300             250我知道我可以通過例如reshape2來實(shí)現(xiàn)這一目標(biāo):dcast(df, id ~  type, value.var="transactions")但是,是否有一種方法可以一次處理“交易”和“金額”變量,從而一次重塑整個(gè)df?理想情況下,使用新的更合適的列名?
查看完整描述

2 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

在“ reshape2”中,您可以使用recast(盡管根據(jù)我的經(jīng)驗(yàn),這不是眾所周知的功能)。


library(reshape2)

recast(mydf, id ~ variable + type, id.var = c("id", "type"))

#   id transactions_expense transactions_income amount_expense amount_income

# 1 20                   25                  20             95           100

# 2 30                   45                  50            250           300

您還可以使用基數(shù)R reshape:


reshape(mydf, direction = "wide", idvar = "id", timevar = "type")

#   id transactions.income amount.income transactions.expense amount.expense

# 1 20                  20           100                   25             95

# 3 30                  50           300                   45            250

或者,你可以melt和dcast,像這樣的(這里“data.table”):


library(data.table)

library(reshape2)

dcast.data.table(melt(as.data.table(mydf), id.vars = c("id", "type")), 

                 id ~ variable + type, value.var = "value")

#    id transactions_expense transactions_income amount_expense amount_income

# 1: 20                   25                  20             95           100

# 2: 30                   45                  50            250           300

在dcast.data.table“ data.table”(1.9.8)的更高版本中,您將可以直接執(zhí)行此操作。如果我正確理解的話,@ Arun嘗試實(shí)現(xiàn)的內(nèi)容將是在無需首先melt獲取數(shù)據(jù)的情況下進(jìn)行重塑,這就是當(dāng)前發(fā)生的情況recast,本質(zhì)上是melt+ dcast操作序列的包裝。


而且,為徹底起見,這里是tidyr方法:


library(dplyr)

library(tidyr)

mydf %>% 

  gather(var, val, transactions:amount) %>% 

  unite(var2, type, var) %>% 

  spread(var2, val)

#   id expense_amount expense_transactions income_amount income_transactions

# 1 20             95                   25           100                  20

# 2 30            250                   45           300                  50


查看完整回答
反對(duì) 回復(fù) 2019-09-24
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

使用data.table v1.9.6 +,我們可以value.var同時(shí)轉(zhuǎn)換多個(gè)列(并在中使用多個(gè)聚合函數(shù)fun.aggregate)。請(qǐng)查看?dcast更多信息以及示例部分。


require(data.table) # v1.9.6+

dcast(dt, id ~ type, value.var=names(dt)[3:4])

#    id transactions_expense transactions_income amount_expense amount_income

# 1: 20                   25                  20             95           100

# 2: 30                   45                  50            250           300


查看完整回答
反對(duì) 回復(fù) 2019-09-24
  • 2 回答
  • 0 關(guān)注
  • 583 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)