[英]Summarize and/or Transpose similar rows into columns
我有一个这种格式的大data.frame:
Location Crop Acres
Plot 1 Wheat 6
Plot 1 Canola 10
Plot 1 Barley 50
Plot 2 Canola 100
Plot 2 Wheat 25
每个位置可能有许多作物,有些可能只有 1。不知何故,我想总结并将作物和英亩转移到一个位置,以便新的 data.frame 看起来像
Location Crop1 Acres1 Crop2 Acres2 Crop3 Acres3
Plot 1 Wheat 6 Canola 10 Barley 50
Plot 2 Canola 100 Wheat 25 NA NA
显然 Crop 和 Acres 列不可能相同,这就是为什么会有 Crop1、Acres1、Crop2、Acres2 等。
我已经尝试过 pivot 表,但这并没有给我我需要的结果,或者我可能没有使用正确的代码。
用tidyverse
这样的事情怎么样:
library(dplyr)
library(tidyr)
data %>%
# first ensure you do not have some dupes
group_by(Location, Crop) %>%
summarise(Acres = sum(Acres)) %>%
# here you add a column that give the "position" by group
group_by(Location) %>%
mutate(n_ = 1:n()) %>%
# lastly you pivot to have data from long to wide format
pivot_wider( names_from = c(n_),
values_from = c(Crop,Acres))
# A tibble: 2 x 7
# Groups: Location [2]
Location Crop_1 Crop_2 Crop_3 Acres_1 Acres_2 Acres_3
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
1 Plot 1 Barley Canola Wheat 50 10 6
2 Plot 2 Canola Wheat NA 100 25 NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.