R中大数据帧中的数据帧格式更改 [英] dataframe format change in large dataframes in R
本文介绍了R中大数据帧中的数据帧格式更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将我的长格式data.frame(5列: person_id item_id item_type性别 item_trans
)变宽格式,使得每个人
只占用data.frame的一行。
I'm trying to make my long format data.frame (5 columns: "person_id" "item_id" "item_type" "gender" "item_trans"
) into wide format such that each person
occupies only one row of the data.frame.
我尝试了以下操作,但未成功,在 BASE R 中我的代码是否有快速解决方案?
I have tried the following without success, is there a quick fix to my code in BASE R?
dat <- read.csv('https://raw.githubusercontent.com/izeh/n/master/n.csv', stringsAsFactors = F)
reshape(dat, dir = 'wide', idvar = c('person_id','gender'), timevar = 'item_type')
推荐答案
根据OP的说明,可能是
Based on the OP's clarification, it could be
out <- reshape(dat[setdiff(names(dat), 'item_type')], idvar = c('person_id', 'gender'), direction = 'wide', timevar = 'item_id')
dim(out)
#[1] 2000 16006
out[1:3, c(1:3, 16000:16006)]
# person_id gender item_trans.1 item_trans.15998 item_trans.15999 item_trans.16000 item_trans.16001 item_trans.16002 item_trans.16003
#1 1 MALE 5.091636 NA NA NA NA NA NA
#32 2 MALE NA NA NA NA NA NA NA
#64 3 FEMALE NA NA NA NA NA NA NA
# item_trans.16004
#1 NA
#32 NA
#64 NA
这篇关于R中大数据帧中的数据帧格式更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文