在多个 vectors/lists 上对数据框进行子集化并为 R 中的每个子集组合应用一个函数
Subsetting a data frame on multiple vectors/lists and applying a function for each subset combination in R
我有一个数据框 (df),其中包含多个列,例如商品、公司、地区、日期、价格。我想应用一些函数或转换,例如使用下面 3 个向量中的 combination/groupings 将价格的平均值添加到每个子集的价格列,以便在应用该函数之前对我的数据框进行子集化。这三个向量包含来自数据框的不同项目、公司和地区,并且也具有不同的长度,例如,不同的项目比公司或地区更多。
数据框示例:
Date | Region | Company | Item | Price
---------------------------------------
7/16 | NW | ABC | Phone| 200
8/16 | NW | ABC | Phone| 200
8/16 | SW | DEF | Food | 100
8/16 | SW | DEF | Food | 50
9/16 | NW | ABC | Tools| 100
9/16 | NW | DEF | Tools| 50
3 个向量如下:
item <- unique(df$item) # 3 different items
company <- unique(df$company) # 2 different companies
region <- unique(df$region) # 2 regions
我正在考虑 运行 一个嵌套的 for 循环并在循环中应用一个函数。这看起来真的很低效,而且我不确定我是否做对了。
我想象的for循环会是这样的...
for (i in seq_along(item))
{
for (j in seq_along(company))
{
for (k in seq_along(region))
{
x <- df[df$item==i & df$company==j & df$region==k,]
x$Price <- x$Price + mean(x$Price)
return(x)
}
}
}
我正在寻找的输出是这样的,因为每次分组并将该组的平均价格添加到 df 的价格列中:
Date | Region | Company | Item | Price
---------------------------------------
7/16 | NW | ABC | Phone| 400
8/16 | NW | ABC | Phone| 400
8/16 | SW | DEF | Food | 175
8/16 | SW | DEF | Food | 125
9/16 | NW | ABC | Tools| 200
9/16 | NW | DEF | Tools| 100
有更好的方法吗?更好的 for 循环或一些 sapply 或 lapply 方法?我不确定如何处理这个问题,因为 3 个向量的长度不同。
group_by
和 mutate
就可以了!
library(dplyr)
data <- data_frame(
Date = c("7/16","8/16","8/16","8/16","9/16","9/16"),
Region = c("NW", "NW", "SW", "SW", "NW", "NW"),
Company = c("ABC", "ABC", "DEF", "DEF", "ABC", "DEF"),
Item = c("Phone", "Phone", "Food", "Food", "Tools", "Tools"),
Price = c(200, 200, 100, 50, 100, 50)
)
data %>%
group_by(Region, Company, Item) %>%
mutate(Price = Price + mean(Price))
输出如下:
Source: local data frame [6 x 5]
Groups: Region, Company, Item [4]
Date Region Company Item Price
<chr> <chr> <chr> <chr> <dbl>
1 7/16 NW ABC Phone 400
2 8/16 NW ABC Phone 400
3 8/16 SW DEF Food 175
4 8/16 SW DEF Food 125
5 9/16 NW ABC Tools 200
6 9/16 NW DEF Tools 100
我有一个数据框 (df),其中包含多个列,例如商品、公司、地区、日期、价格。我想应用一些函数或转换,例如使用下面 3 个向量中的 combination/groupings 将价格的平均值添加到每个子集的价格列,以便在应用该函数之前对我的数据框进行子集化。这三个向量包含来自数据框的不同项目、公司和地区,并且也具有不同的长度,例如,不同的项目比公司或地区更多。
数据框示例:
Date | Region | Company | Item | Price
---------------------------------------
7/16 | NW | ABC | Phone| 200
8/16 | NW | ABC | Phone| 200
8/16 | SW | DEF | Food | 100
8/16 | SW | DEF | Food | 50
9/16 | NW | ABC | Tools| 100
9/16 | NW | DEF | Tools| 50
3 个向量如下:
item <- unique(df$item) # 3 different items
company <- unique(df$company) # 2 different companies
region <- unique(df$region) # 2 regions
我正在考虑 运行 一个嵌套的 for 循环并在循环中应用一个函数。这看起来真的很低效,而且我不确定我是否做对了。
我想象的for循环会是这样的...
for (i in seq_along(item))
{
for (j in seq_along(company))
{
for (k in seq_along(region))
{
x <- df[df$item==i & df$company==j & df$region==k,]
x$Price <- x$Price + mean(x$Price)
return(x)
}
}
}
我正在寻找的输出是这样的,因为每次分组并将该组的平均价格添加到 df 的价格列中:
Date | Region | Company | Item | Price
---------------------------------------
7/16 | NW | ABC | Phone| 400
8/16 | NW | ABC | Phone| 400
8/16 | SW | DEF | Food | 175
8/16 | SW | DEF | Food | 125
9/16 | NW | ABC | Tools| 200
9/16 | NW | DEF | Tools| 100
有更好的方法吗?更好的 for 循环或一些 sapply 或 lapply 方法?我不确定如何处理这个问题,因为 3 个向量的长度不同。
group_by
和 mutate
就可以了!
library(dplyr)
data <- data_frame(
Date = c("7/16","8/16","8/16","8/16","9/16","9/16"),
Region = c("NW", "NW", "SW", "SW", "NW", "NW"),
Company = c("ABC", "ABC", "DEF", "DEF", "ABC", "DEF"),
Item = c("Phone", "Phone", "Food", "Food", "Tools", "Tools"),
Price = c(200, 200, 100, 50, 100, 50)
)
data %>%
group_by(Region, Company, Item) %>%
mutate(Price = Price + mean(Price))
输出如下:
Source: local data frame [6 x 5]
Groups: Region, Company, Item [4]
Date Region Company Item Price
<chr> <chr> <chr> <chr> <dbl>
1 7/16 NW ABC Phone 400
2 8/16 NW ABC Phone 400
3 8/16 SW DEF Food 175
4 8/16 SW DEF Food 125
5 9/16 NW ABC Tools 200
6 9/16 NW DEF Tools 100