我正在尝试从长到宽重塑数据集。以下代码有效,但我很好奇是否有办法不提供值列并仍然使用pivot_wider. 在下面的示例中,我必须创建一个临时列“val”才能使用pivot_wider,但是有没有办法不用它呢?
a <- data.frame(name = c("sam", "rob", "tom"),
type = c("a", "b", "c"))
a
name type
1 sam a
2 rob b
3 tom c
Run Code Online (Sandbox Code Playgroud)
我想将其转换如下。
name a b c
1 sam 1 0 0
2 rob 0 1 0
3 tom 0 0 1
Run Code Online (Sandbox Code Playgroud)
这可以通过以下代码完成,但我可以在不创建“val”列的情况下完成(并且仍然使用 tidyverse 语言)吗?
a <- data.frame(name = c("sam", "rob", "tom"),
type = c("a", "b", "c"),
val = rep(1, 3)) %>%
pivot_wider(names_from = type, values_from = val, values_fill = list(val = 0))
Run Code Online (Sandbox Code Playgroud)
您可以使用values_fn参数分配 1 和values_fill分配 0:
library(tidyr)
pivot_wider(a, names_from = type, values_from = type, values_fn = list(type = ~1), values_fill = list(type = 0))
# A tibble: 3 x 4
name a b c
<fct> <dbl> <dbl> <dbl>
1 sam 1 0 0
2 rob 0 1 0
3 tom 0 0 1
Run Code Online (Sandbox Code Playgroud)
我们可以mutate使用一列 1 并在pivot_wider
library(dplyr)
library(tidyr)
a %>%
mutate(n = 1) %>%
pivot_wider(names_from = type, values_from = n, values_fill = list(n = 0))
# A tibble: 3 x 4
# name a b c
# <fct> <dbl> <dbl> <dbl>
#1 sam 1 0 0
#2 rob 0 1 0
#3 tom 0 0 1
Run Code Online (Sandbox Code Playgroud)
在base R,它会更容易..
table(a)
Run Code Online (Sandbox Code Playgroud)
进入更老的学校,reshape2::dcast或蓬勃发展的data.table::dcast,让你通过指定一个聚合函数来做到这一点:
reshape2::dcast(a, name ~ type, fun.aggregate = length)
# name a b c
# 1 rob 0 1 0
# 2 sam 1 0 0
# 3 tom 0 0 1
data.table::dcast(setDT(a), name ~ type, fun.aggregate = length)
# name a b c
# 1: rob 0 1 0
# 2: sam 1 0 0
# 3: tom 0 0 1
Run Code Online (Sandbox Code Playgroud)