I have a data.frame with the columns: Month, Store and Demand.
我有一个包含列的data.frame:Month,Store和Demand。
Month Store Demand
Jan A 100
Feb A 150
Mar A 120
Jan B 200
Feb B 230
Mar B 320
I need to pivot it around to make a new data.frame or array with columns for each month, store e.g.:
我需要调整它以创建一个新的data.frame或数组,每个月都有列,例如:
Store Jan Feb Mar
A 100 150 120
B 200 230 320
Any help is very much appreciated. I have just started with R.
很感谢任何形式的帮助。我刚开始用R.
3 个解决方案
#1
9
> df <- read.table(textConnection("Month Store Demand
+ Jan A 100
+ Feb A 150
+ Mar A 120
+ Jan B 200
+ Feb B 230
+ Mar B 320"), header=TRUE)
So in all likelihood your Month column is a factor with levels sorted alphabetically (EDIT:)
所以很可能你的月份列是一个按字母顺序排序的因素(编辑:)
> df$Month <- factor(df$Month, levels= month.abb[1:3])
# Just changing levels was not correct way to handle the problem.
# Need to use within a factor(...) call.
> xtabs(Demand ~ Store+Month, df)
Month
Store Jan Feb Mar
A 100 150 120
B 200 230 320
A slightly less obvious method (since the 'I' function returns its argument):
一个稍微不那么明显的方法(因为'I'函数返回其参数):
> with(df, tapply(Demand, list(Store, Month) , I) )
Jan Feb Mar
A 100 150 120
B 200 230 320
#2
5
Welcome to R.
欢迎来到R.
There are usually many ways to get to the same end using R. Another approach would be to use Hadley's reshape package.
通常有很多方法可以使用R来达到同一目的。另一种方法是使用Hadley的重塑包。
# create the data as explained by @Dwin
df <- read.table(textConnection("Month Store Demand
Jan A 100
Feb A 150
Mar A 120
Jan B 200
Feb B 230
Mar B 320"),
header=TRUE)
# load the reshape package from Hadley -- he has created GREAT packages
library(reshape)
# reshape the data from long to wide
cast(df, Store ~ Month)
And for reference, you should check out this great tutorial. http://www.jstatsoft.org/v21/i12/paper
作为参考,你应该看看这个很棒的教程。 http://www.jstatsoft.org/v21/i12/paper
#3
4
If the data are in dat
(and levels set to calendar order), then another base R solution is to use the (incredibly unintuitive) reshape()
function:
如果数据在dat中(并且级别设置为日历顺序),则另一个基本R解决方案是使用(令人难以置信的不直观)reshape()函数:
reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
direction = "wide")
which for the snippet of data gives:
哪个数据片段给出:
> reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
+ direction = "wide")
Store Demand.Jan Demand.Feb Demand.Mar
1 A 100 150 120
4 B 200 230 320
The names can easily be cleaned if you want:
如果您需要,可以轻松清除名称:
> out <- reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
+ direction = "wide")
> names(out)[-1] <- month.abb[1:3]
> out
Store Jan Feb Mar
1 A 100 150 120
4 B 200 230 320
(To get the output above, I read the data in in a similar fashion to that shown in @DWin's Answer, and then ran the following:
(为了获得上面的输出,我以与@ DWin的答案中所示类似的方式读取数据,然后执行以下操作:
dat <- transform(dat, Month = factor(Month, levels = month.abb[1:3]))
where dat
was what I called the data)
其中dat是我所谓的数据)
#1
9
> df <- read.table(textConnection("Month Store Demand
+ Jan A 100
+ Feb A 150
+ Mar A 120
+ Jan B 200
+ Feb B 230
+ Mar B 320"), header=TRUE)
So in all likelihood your Month column is a factor with levels sorted alphabetically (EDIT:)
所以很可能你的月份列是一个按字母顺序排序的因素(编辑:)
> df$Month <- factor(df$Month, levels= month.abb[1:3])
# Just changing levels was not correct way to handle the problem.
# Need to use within a factor(...) call.
> xtabs(Demand ~ Store+Month, df)
Month
Store Jan Feb Mar
A 100 150 120
B 200 230 320
A slightly less obvious method (since the 'I' function returns its argument):
一个稍微不那么明显的方法(因为'I'函数返回其参数):
> with(df, tapply(Demand, list(Store, Month) , I) )
Jan Feb Mar
A 100 150 120
B 200 230 320
#2
5
Welcome to R.
欢迎来到R.
There are usually many ways to get to the same end using R. Another approach would be to use Hadley's reshape package.
通常有很多方法可以使用R来达到同一目的。另一种方法是使用Hadley的重塑包。
# create the data as explained by @Dwin
df <- read.table(textConnection("Month Store Demand
Jan A 100
Feb A 150
Mar A 120
Jan B 200
Feb B 230
Mar B 320"),
header=TRUE)
# load the reshape package from Hadley -- he has created GREAT packages
library(reshape)
# reshape the data from long to wide
cast(df, Store ~ Month)
And for reference, you should check out this great tutorial. http://www.jstatsoft.org/v21/i12/paper
作为参考,你应该看看这个很棒的教程。 http://www.jstatsoft.org/v21/i12/paper
#3
4
If the data are in dat
(and levels set to calendar order), then another base R solution is to use the (incredibly unintuitive) reshape()
function:
如果数据在dat中(并且级别设置为日历顺序),则另一个基本R解决方案是使用(令人难以置信的不直观)reshape()函数:
reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
direction = "wide")
which for the snippet of data gives:
哪个数据片段给出:
> reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
+ direction = "wide")
Store Demand.Jan Demand.Feb Demand.Mar
1 A 100 150 120
4 B 200 230 320
The names can easily be cleaned if you want:
如果您需要,可以轻松清除名称:
> out <- reshape(dat, v.names = "Demand", idvar = "Store", timevar = "Month",
+ direction = "wide")
> names(out)[-1] <- month.abb[1:3]
> out
Store Jan Feb Mar
1 A 100 150 120
4 B 200 230 320
(To get the output above, I read the data in in a similar fashion to that shown in @DWin's Answer, and then ran the following:
(为了获得上面的输出,我以与@ DWin的答案中所示类似的方式读取数据,然后执行以下操作:
dat <- transform(dat, Month = factor(Month, levels = month.abb[1:3]))
where dat
was what I called the data)
其中dat是我所谓的数据)