[英]Adding data in rows to columns in R
I have the following table: 我有下表:
wk Brand Retail_price
1 1 a 1.2
2 2 a 1.3
3 1 c 1.4
4 2 c 1.5
5 1 d 1.6
6 2 d 1.7
I am trying to get the retail price of other brands during the same week as columns. 我正在尝试获取与专栏在同一周内其他品牌的零售价。 I want to get to:
我想去:
wk Brand Retail_price Retail_price_a Retail_price_c Retail_price_d
1 1 a 1.2 NA 1.4 1.6
2 2 a 1.3 NA 1.5 1.7
3 1 c 1.4 1.2 NA 1.6
4 2 c 1.5 1.3 NA 1.7
5 1 d 1.6 1.2 1.4 NA
6 2 d 1.7 1.3 1.5 NA
I have tried looping through the data to manually add columns for each brand. 我尝试遍历数据以手动为每个品牌添加列。 Turned out to be very inefficient.
原来是非常低效的。
I am new to R. I am thinking of something equivalent to pd.pivot
in python, create a new df
and then join the two. 我是R的新手。我正在考虑与python中的
pd.pivot
等效的东西,创建一个新的df
,然后将两者pd.pivot
。
How can I do this in R? 我如何在R中做到这一点? Is there a better way of doing this?
有更好的方法吗?
I think you need a combination of reshaping (long-to-wide) and merge. 我认为您需要重塑(从长到宽)和合并的组合。 Here's an example using
dplyr
and tidyr
: 这是使用
dplyr
和tidyr
的示例:
# data
x <- data.frame(
wk = c(1L, 2L, 1L, 2L, 1L, 2L),
Brand = c("a", "a", "c", "c", "d", "d"),
Price = c(1.2, 1.3, 1.4, 1.5, 1.6, 1.7),
stringsAsFactors = FALSE)
library(dplyr)
library(tidyr)
x2 <- spread(x, Brand, Price, sep = "_") %>%
left_join(x, by = "wk")
x2
# wk Brand_a Brand_c Brand_d Brand Price
# 1 1 1.2 1.4 1.6 a 1.2
# 2 1 1.2 1.4 1.6 c 1.4
# 3 1 1.2 1.4 1.6 d 1.6
# 4 2 1.3 1.5 1.7 a 1.3
# 5 2 1.3 1.5 1.7 c 1.5
# 6 2 1.3 1.5 1.7 d 1.7
You can then work on removing same-brand cells if needed. 然后,如果需要,您可以删除相同品牌的单元。
In data.table
: 在
data.table
:
library(data.table)
xDT <- setDT(copy(x))
merge(xDT, dcast(xDT, wk ~ Brand), by = c("wk"))
# or #
xDT[dcast(xDT, wk ~ Brand), on = c("wk")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.