2
我有一个购物车的数据,看起来像下面的示例数据框:收集多列与tidyr
sample_df<-data.frame(
clientid=1:10,
ProductA=c("chair","table","plate","plate","table","chair","table","plate","chair","chair"),
QuantityA=c(1,2,1,1,1,1,2,3,1,2),
ProductB=c("table","doll","shoes","","door","","computer","computer","","plate"),
QuantityB=c(3,1,2,"",2,"",1,1,"",1)
)
#sample data frame
clientid ProductA QuantityA ProductB QuantityB
1 1 chair 1 table 3
2 2 table 2 doll 1
3 3 plate 1 shoes 2
4 4 plate 1
...
10 10 chair 2 plate 1
我想将其转换成不同的格式,这将是这样的:
#ideal data frame
clientid ProductNumber Product Quantity
1 1 A chair 1
2 1 B table 3
3 2 A table 2
4 2 B doll 1
...
11 6 A chair 1
...
17 10 A chair 2
18 10 B plate 1
我试图
library(tidyr)
sample_df_gather<- sample_df %>% select(clientid, ProductA, ProductB)
%>% gather(ProductNumber, value, -clientid) %>% filter(!is.na(value))
#this gives me
clientid ProductNumber value
1 1 ProductA chair
2 2 ProductB table
3 3 ProductA plate
4 4 ProductB plate
...
不过,我不知道该怎么数量添加到数据帧。另外,在实际的数据框架中,还有更多的栏目,例如标题,价格,我希望将其转换为理想的数据框架。有没有办法将数据转换为理想的格式?
对于QuantityB,你真的不想用“”......试试NA。 – Frank
'reshape(sample_df,dir ='long',vary = list(c(2,4),c(3,5)))'给了我20行或是错误的 – rawr
谢谢@Frank!这里提供的重塑功能解决了我的问题。 @aosmith,是的,在我问这个问题之前,我已经检查过它,但仍然无法找到一种方法将我转换为理想的数据框架。 –