我正在处理数据框data
,其结构与下面的数据类似。
Gender Age Number
1 Female 55-59 years 5
2 Female 65+ years 10
3 Male 25-29 years 4
4 Male 40-44 years 3
5 Male 50-54 years 1
我正在尝试使用tidyr重塑数据(迄今为止失败),以便Number
列的每个值都在其自己的行上。我正在寻找的输出应该类似于以下内容:
Gender Age
1 Female 55-59 years
2 Female 55-59 years
3 Female 55-59 years
4 Female 55-59 years
5 Female 55-59 years
6 Female 65+ years
7 Female 65+ years
8 Female 65+ years
9 Female 65+ years
10 Female 65+ years
11 Female 65+ years
12 Female 65+ years
13 Female 65+ years
14 Female 65+ years
15 Female 65+ years
16 Male 25-29 years
17 Male 25-29 years
18 Male 25-29 years
19 Male 25-29 years
20 Male 40-44 years
21 Male 40-44 years
22 Male 40-44 years
23 Male 50-54 years
我尝试使用聚集/传播功能的各种组合,而不是远远接近成功。我很确定这在tidyr中是可能的!
我知道有很多其他软件包/功能可以用来实现相同的结果,但我非常渴望得到一个tidyr解决方案,所以我可以将它包含在更大的dplyr / tidyr管道中。
非常感谢任何援助方面的帮助。
dat <- structure(list(Gender = structure(c(3L, 3L, 1L, 2L, 1L), .Label = c(" Male",
" Male", "Female"), class = "factor"), Age = structure(c(5L,
1L, 2L, 3L, 4L), .Label = c("65+ years", "25-29 years", "40-44 years",
"50-54 years", "55-59 years"), class = "factor"), Number = c(5L,
10L, 4L, 3L, 1L)), .Names = c("Gender", "Age", "Number"), class = "data.frame", row.names = c(NA,
-5L))
答案 0 :(得分:5)
这也不是使用tidyr,但我认为这很自然:
dat %>% slice(rep(row_number(), Number)) %>% select(-Number)
Gender Age
1 Female 55-59 years
2 Female 55-59 years
3 Female 55-59 years
4 Female 55-59 years
5 Female 55-59 years
6 Female 65+ years
7 Female 65+ years
8 Female 65+ years
9 Female 65+ years
10 Female 65+ years
11 Female 65+ years
12 Female 65+ years
13 Female 65+ years
14 Female 65+ years
15 Female 65+ years
16 Male 25-29 years
17 Male 25-29 years
18 Male 25-29 years
19 Male 25-29 years
20 Male 40-44 years
21 Male 40-44 years
22 Male 40-44 years
23 Male 50-54 years
正如@bramtayl建议的那样,人们可以(可以说)用
来提高可读性dat %>% slice(row_number() %>% rep(Number)) %>% select(-Number)
答案 1 :(得分:4)
不是 tidyr ,但非常快速有效:
dat2 <- dat[rep(1:nrow(dat), dat[["Number"]]), 1:2]
rownames(dat2) <- NULL
## Gender Age
## 1 Female 55-59 years
## 2 Female 55-59 years
## 3 Female 55-59 years
## 4 Female 55-59 years
## 5 Female 55-59 years
## 6 Female 65+ years
## 7 Female 65+ years
## 8 Female 65+ years
## 9 Female 65+ years
## 10 Female 65+ years
## 11 Female 65+ years
## 12 Female 65+ years
## 13 Female 65+ years
## 14 Female 65+ years
## 15 Female 65+ years
## 16 Male 25-29 years
## 17 Male 25-29 years
## 18 Male 25-29 years
## 19 Male 25-29 years
## 20 Male 40-44 years
## 21 Male 40-44 years
## 22 Male 40-44 years
## 23 Male 50-54 years
答案 2 :(得分:2)
我们可以使用tidyr/dplyr
执行此操作。将值更改为序列list
后,将“数字”转换为unnest
列,并使用select
从输出中删除“数字”列。
library(dplyr)
library(tidyr)
dat1 <- dat %>%
mutate(Number= lapply(Number, seq)) %>%
unnest(Number) %>%
select(-Number)
请注意,输出将是tbl_df
,当我们使用dplyr
函数执行其他操作时,这将非常有用。
str(dat1)
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 23 obs. of 2 variables:
# $ Gender: Factor w/ 3 levels " Male"," Male",..: 3 3 3 3 3 3 3 3 3 3 ...
# $ Age : Factor w/ 5 levels "65+ years","25-29 years",..: 5 5 5 5 5 1 1 1 1 1 ...
dat1 %>%
as.data.frame()
# Gender Age
#1 Female 55-59 years
#2 Female 55-59 years
#3 Female 55-59 years
#4 Female 55-59 years
#5 Female 55-59 years
#6 Female 65+ years
#7 Female 65+ years
#8 Female 65+ years
#9 Female 65+ years
#10 Female 65+ years
#11 Female 65+ years
#12 Female 65+ years
#13 Female 65+ years
#14 Female 65+ years
#15 Female 65+ years
#16 Male 25-29 years
#17 Male 25-29 years
#18 Male 25-29 years
#19 Male 25-29 years
#20 Male 40-44 years
#21 Male 40-44 years
#22 Male 40-44 years
#23 Male 50-54 years