r - R data transformation: create new rows based on numbers in different columns
问题描述
I am searching for a solution how to transform the following data frame using dplyr:
item <- c('A','B','C')
one <- c(2, 1, 2)
two <- c(1,1,2)
data <- data.frame(item,one,two)
data
item | one | two |
---|---|---|
A | 2 | 1 |
B | 1 | 1 |
C | 2 | 2 |
Now, the column "one" contains the number of ratings of the value 1, the column "two" the number of ratings of the value 2. My ideal data frame after transformation would look like this:
item | rating |
---|---|
A | 1 |
A | 1 |
A | 2 |
B | 1 |
B | 2 |
C | 1 |
C | 1 |
C | 2 |
C | 2 |
Any idea how I could get to this output (it doesn't have to be dplyr)? I know how to use pivot_longer of the tidyr package but that doesn't solve the problem of repeating the number of rows...
解决方案
library(dplyr)
library(tidyr) # pivot_longer
nums <- c(one = 1, two = 2, three = 3)
data %>%
pivot_longer(-item) %>%
group_by(item) %>%
summarize(rating = rep(name, times = value)) %>%
ungroup() %>%
mutate(rating = nums[rating])
# # A tibble: 9 x 2
# item rating
# <chr> <dbl>
# 1 A 1
# 2 A 1
# 3 A 2
# 4 B 1
# 5 B 2
# 6 C 1
# 7 C 1
# 8 C 2
# 9 C 2
I had to define nums
because I couldn't find (in my haste) an easy way to convert "one"
to 1
in a programmatic way. You'll need to make sure it goes out at least as far as you need; I added three=3
for demonstration, if you truly only have one
and two
then you should be good as-is.
(Related to that topic: Convert written number to number in R)
推荐阅读
- sql - 选择上方或下方附加行的结果
- java - 在我的 java 函数中实现搜索
- php - php查询函数重复数据
- build - Netlify - 我的构建可执行文件的权限被拒绝
- spring-boot - PDF Excel 导出客户端和服务器端
- azure - 日志在 Azure 自定义日志中不可见
- ruby-on-rails - Rails Factory_Bot 和 reflect_on_association?
- php - PHP PDO 不工作,仍然可以 sql 注入
- python-3.x - UnboundLocalError:分配前引用的局部变量“img_files”
- python-3.x - psycopg2.OperationalError:无法连接到服务器:连接被拒绝(0x0000274D/10061)