我有一个要加宽的长数据帧。长数据帧包括重复项,我希望在宽格式中将它们保留为唯一列,但宽格式输出将重复项组合在一个列列表中。我已经尝试了一些关于pivot_wider((的东西,并认为unnest((可能是我需要的。下面是我所能找到的最接近我想要的东西,但它并不完全存在。
在下面的例子中,我希望宽数据帧包括变量"0"的值;"惊喜";对于每个";框"--有尽可能多的列,就有独特和非独特的惊喜。从概念上讲,我追求的是盒子里的独特和非独特的惊喜。
用这种方式扩大长数据范围可能吗?
library(tidyverse)
# some data; long format
long_box <- c("A", "A", "A", "B", "B", "B", "C", "C")
surprise <- c("apple", "orange", "orange", "apple", "banana", "insects", "apple", "insects")
# the data frame I have
tibble(long_box, surprise)
#> # A tibble: 8 x 2
#> long_box surprise
#> <chr> <chr>
#> 1 A apple
#> 2 A orange
#> 3 A orange
#> 4 B apple
#> 5 B banana
#> 6 B insects
#> 7 C apple
#> 8 C insects
# same data, wide format
wide_box <- c("A", "B", "C")
a <- c(rep("apple",3))
b <- c("orange", "banana", "insects")
c <- c("orange", "insects", NA)
# the data frame format I want
tibble(wide_box, a, b, c) %>%
rename(suprise_1 = a,
suprise_2 = b,
suprise_3 = c)
#> # A tibble: 3 x 4
#> wide_box suprise_1 suprise_2 suprise_3
#> <chr> <chr> <chr> <chr>
#> 1 A apple orange orange
#> 2 B apple banana insects
#> 3 C apple insects <NA>
# this is what I've tried to get from long to wide
tibble(long_box, surprise) %>%
pivot_wider(id_cols = long_box,
names_from = surprise,
values_from = surprise)
#> Warning: Values are not uniquely identified; output will contain list-cols.
#> * Use `values_fn = list` to suppress this warning.
#> * Use `values_fn = length` to identify where the duplicates arise
#> * Use `values_fn = {summary_fun}` to summarise duplicates
#> # A tibble: 3 x 5
#> long_box apple orange banana insects
#> <chr> <list> <list> <list> <list>
#> 1 A <chr [1]> <chr [2]> <NULL> <NULL>
#> 2 B <chr [1]> <NULL> <chr [1]> <chr [1]>
#> 3 C <chr [1]> <NULL> <NULL> <chr [1]>
创建于2021-07-15由reprex包(v2.0.0(
创建一个序列列,它应该可以使用
library(dplyr)
library(tidyr)
library(data.table)
library(stringr)
tibble(long_box, surprise) %>%
mutate(nm1= str_c('suprise_', rowid(long_box))) %>%
pivot_wider(names_from = nm1, values_from = surprise)
-输出
# A tibble: 3 x 4
long_box suprise_1 suprise_2 suprise_3
<chr> <chr> <chr> <chr>
1 A apple orange orange
2 B apple banana insects
3 C apple insects <NA>