Untable 已聚合的数据框
Untable a dataframe that has been aggregated
我在 R 中有以下数据框:
Name Year Apples Bananas
Tom 2012 3 4
John 2012 0 1
John 2013 3 2
John 2014 5 3
Alexa 2012 2 2
Alexa 2013 2 1
我想转换或重塑数据框,以在数据框中表示每个人吃苹果和香蕉的频率。像这样:最好使用 dplyr 或 reshape packages
Name Year. Fruit
Tom 2012 Apples
Tom 2012 Apples
Tom 2012 Apples
Tom 2012 Bananas
Tom 2012 Bananas
Tom 2012 Bananas
John 2012 Bananas
John 2013 Apples
John 2013 Apples
John 2013 Apples
John 2013 Bananas
John 2013 Bananas
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Bananas
John 2014 Bananas
John 2014 Bananas
等等
library( data.table )
#sample data
# mydata <- fread( "Name Year Apples Bananas
# Tom 2012 3 4
# John 2012 0 1
# John 2013 3 2
# John 2014 5 3
# Alexa 2012 2 2
# Alexa 2013 2 1 ")
ans <- melt( setDT( mydata ), id.vars = c("Name","Year"), variable.name = "Fruit" )
ans[ rep(seq(.N), value), !"value"]
输出
# Name Year Fruit
# 1: Tom 2012 Apples
# 2: Tom 2012 Apples
# 3: Tom 2012 Apples
# 4: John 2013 Apples
# 5: John 2013 Apples
# 6: John 2013 Apples
# 7: John 2014 Apples
# 8: John 2014 Apples
# 9: John 2014 Apples
# 10: John 2014 Apples
# 11: John 2014 Apples
# 12: Alexa 2012 Apples
# 13: Alexa 2012 Apples
# 14: Alexa 2013 Apples
# 15: Alexa 2013 Apples
# 16: Tom 2012 Bananas
# 17: Tom 2012 Bananas
# 18: Tom 2012 Bananas
# 19: Tom 2012 Bananas
# 20: John 2012 Bananas
# 21: John 2013 Bananas
# 22: John 2013 Bananas
# 23: John 2014 Bananas
# 24: John 2014 Bananas
# 25: John 2014 Bananas
# 26: Alexa 2012 Bananas
# 27: Alexa 2012 Bananas
# 28: Alexa 2013 Bananas
# Name Year Fruit
我在 R 中有以下数据框:
Name Year Apples Bananas
Tom 2012 3 4
John 2012 0 1
John 2013 3 2
John 2014 5 3
Alexa 2012 2 2
Alexa 2013 2 1
我想转换或重塑数据框,以在数据框中表示每个人吃苹果和香蕉的频率。像这样:最好使用 dplyr 或 reshape packages
Name Year. Fruit
Tom 2012 Apples
Tom 2012 Apples
Tom 2012 Apples
Tom 2012 Bananas
Tom 2012 Bananas
Tom 2012 Bananas
John 2012 Bananas
John 2013 Apples
John 2013 Apples
John 2013 Apples
John 2013 Bananas
John 2013 Bananas
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Apples
John 2014 Bananas
John 2014 Bananas
John 2014 Bananas
等等
library( data.table )
#sample data
# mydata <- fread( "Name Year Apples Bananas
# Tom 2012 3 4
# John 2012 0 1
# John 2013 3 2
# John 2014 5 3
# Alexa 2012 2 2
# Alexa 2013 2 1 ")
ans <- melt( setDT( mydata ), id.vars = c("Name","Year"), variable.name = "Fruit" )
ans[ rep(seq(.N), value), !"value"]
输出
# Name Year Fruit
# 1: Tom 2012 Apples
# 2: Tom 2012 Apples
# 3: Tom 2012 Apples
# 4: John 2013 Apples
# 5: John 2013 Apples
# 6: John 2013 Apples
# 7: John 2014 Apples
# 8: John 2014 Apples
# 9: John 2014 Apples
# 10: John 2014 Apples
# 11: John 2014 Apples
# 12: Alexa 2012 Apples
# 13: Alexa 2012 Apples
# 14: Alexa 2013 Apples
# 15: Alexa 2013 Apples
# 16: Tom 2012 Bananas
# 17: Tom 2012 Bananas
# 18: Tom 2012 Bananas
# 19: Tom 2012 Bananas
# 20: John 2012 Bananas
# 21: John 2013 Bananas
# 22: John 2013 Bananas
# 23: John 2014 Bananas
# 24: John 2014 Bananas
# 25: John 2014 Bananas
# 26: Alexa 2012 Bananas
# 27: Alexa 2012 Bananas
# 28: Alexa 2013 Bananas
# Name Year Fruit