[英]Reshaping data using tidyr
I am working with a dataframe data
which is similar in structure to the one below. 我正在使用一种数据帧data
,其结构与下面的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
I am attempting to reshape the data (unsuccessfully thus far) using tidyr so that each value of the Number
column is featured on its own line. 我正在尝试使用tidyr重塑数据(到目前为止尚未成功),以使Number
列的每个值都以其自己的行为特征。 The output I am seeking should resemble the following: 我正在寻找的输出应类似于以下内容:
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
I have tried to use various combinations of the gather/spread functions without coming even remotely close to success. 我尝试过使用收集/扩展功能的各种组合,而几乎没有取得成功的机会。 I'm fairly sure this is possible in tidyr! 我很确定这在提迪尔是可能的!
I know there are a number of other packages/functions that I could use to achieve the same result, but I'm quite keen to get a tidyr solution so I can include it in a larger dplyr/tidyr pipe. 我知道我可以使用许多其他程序包/功能来达到相同的结果,但是我非常渴望获得tidyr解决方案,因此可以将其包含在较大的dplyr / tidyr管道中。
Any help of assistance would be very much appreciated. 任何帮助的帮助将不胜感激。
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))
This is also not using tidyr, but I think it's natural: 这也不使用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
As @bramtayl suggested, one can (arguably) improve readability with 正如@bramtayl建议的那样,可以(可以说)通过以下方式提高可读性
dat %>% slice(row_number() %>% rep(Number)) %>% select(-Number)
Not tidyr but pretty fast and efficient: 不是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
We could do this using tidyr/dplyr
. 我们可以使用tidyr/dplyr
进行此操作。 Convert the 'Number' to a list
column after changing the values to sequence, unnest
and remove the 'Number' column from the output with select
. 将值更改为序列后,将“ Number”转换为list
列,取消unnest
并使用select
从输出中删除“ Number”列。
library(dplyr)
library(tidyr)
dat1 <- dat %>%
mutate(Number= lapply(Number, seq)) %>%
unnest(Number) %>%
select(-Number)
Note that the output will be a tbl_df
which would be useful when we are performing other operations using the dplyr
functions. 请注意,输出将是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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.