[英]Reshape long data to multiple wide columns
I have data in a long format that I need to selectively move to wide format.我有长格式的数据,我需要有选择地转换为宽格式。
Here is an example of what I have and what I need这是我拥有的和需要的示例
Rating <- c("Overall","Overall_rank","Total","Total_rank")
value <- c(6,1,5,2)
example <- data.frame(Rating,value)
Creates data that looks like this:创建如下所示的数据:
Rating value
Overall 6
Overall_rank 1
Total 5
Total_rank 2
However, I want my data to look like:但是,我希望我的数据看起来像:
I tried pivot_wider
, but cannot seem to get it.我尝试
pivot_wider
,但似乎无法得到它。
Does this work for your real situation?这对您的实际情况有用吗?
I think the confusion is stemming from calling column 1 "Rating," when really the "rating" values (as I understand it) are contained in rows 1 and 3.我认为混淆源于将第 1 列称为“评级”,而实际上“评级”值(据我所知)包含在第 1 行和第 3 行中。
example %>%
separate(Rating, sep = "_", into = c("Category", "type")) %>%
mutate(type = replace(type, is.na(type), "rating")) %>%
pivot_wider(names_from = type, values_from = value)
Category rating rank
<chr> <dbl> <dbl>
1 Overall 6 1
2 Total 5 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.