r - How to remove a column and increase count of rows based on the value of the columns
问题描述
I'm relatively new to R. I have a data frame with 939 rows that looks like this:
cuisine | Number.of.order | Customer_id |
---|---|---|
Fastfood | 2 | 1 |
cakes | 3 | 1 |
Western | 4 | 2 |
Chinese | 5 | 3 |
What i want is to get rid of the Numbers.of.order column and add the values inside the column as addition rows.
Something like this:
cuisine | Customer_id |
---|---|
Fastfood | 1 |
Fastfood | 1 |
cakes | 1 |
cakes | 1 |
cakes | 1 |
Western | 2 |
Western | 2 |
Western | 2 |
Western | 2 |
Chinese | 3 |
Chinese | 3 |
Chinese | 3 |
Chinese | 3 |
Chinese | 3 |
Im doing this so that I can convert it into a transaction dataframe for association rules.
Any help would be greatly appreciated!!
解决方案
base R
一个选项rep
out <- df1[rep(seq_len(nrow(df1)), df1$Number.of.order), -2]
row.names(out) <- NULL
-输出
out
cuisine Customer_id
1 Fastfood 1
2 Fastfood 1
3 cakes 1
4 cakes 1
5 cakes 1
6 Western 2
7 Western 2
8 Western 2
9 Western 2
10 Chinese 3
11 Chinese 3
12 Chinese 3
13 Chinese 3
14 Chinese 3
推荐阅读
- android - 如何从画布中的许多形状中选择一种形状并在 android 画布中移动该特定形状?
- node.js - 新笔记本电脑和本地连接开始失败
- mysql - 外键约束不兼容
- amazon-athena - Athena 查询数组列
- c++ - C ++中的再次播放选项不适用于猜数字游戏
- java - Java:评估一组设置变量中的变量是否已经具有相同的值
- image-processing - DCMTK: min-max-window vs min-max-window-n vs histogram-window
- flutter - 在 iPad 上使用 Navigation.of.push 时如何修复侧边栏
- sympy - Sympy:如何简化包含 exp(I*x) 的表达式
- flutter - 应该将 Bloc 模式用于颤动文本字段,如果是的话如何?