R:将行转换为列-在pivot_wider中标识values_from

relj7zay  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(118)

我有以下数据集
| 宠物店|第一天|第二天|第三天|
| --------------|--------------|--------------|--------------|
| A|制动爪|鱼|猫|
| A|鱼|鱼|猫|
| B|龟|猫|龟|
| A|制动爪|猫|龟|
| B|猫|兔子|制动爪|
| C|兔子|制动爪|兔子|
| C|制动爪|猫|鱼|

df = structure(list(Pet.Shop = c("A", "A", "B", "A", "B", "C", "C"
               ), Day1 = c("dog", "fish", "turtle", "dog", "cat", "rabbit", 
               "dog"), Day2 = c("fish", "fish", "cat", "cat", "rabbit", "dog", 
               "cat"), Day3 = c("cat", "cat", "turtle", "turtle", "dog", "rabbit", 
               "fish")), class = "data.frame", row.names = c(NA, -7L))

我尝试使用pivot_wider获得以下输出
| 宠物店|制动爪|猫|鱼|龟|兔子|
| --------------|--------------|--------------|--------------|--------------|--------------|
| A|1个|1个|1个|0|0|
| A|0|1个|二|0|0|
| B|0|1个|0|二|0|
| A|1个|1个|0|1个|0|
| B|1个|1个|0|0|1个|
| C|1个|0|0|0|二|
| C|1个|1个|1个|0|0|
我的代码如下所示,但我不确定values_from应该表示什么。

new_df <- df %>%
  mutate(row = row_number()) %>%
  pivot_wider(names_from = Item, values_from = XXX) %>%
  select(-row)

感谢任何帮助。除了pivot_wider之外,还欢迎其他建议,谢谢!

h9vpoimq

h9vpoimq1#

我们可以重塑为'长',然后做宽转换

library(tidyr)
library(dplyr)
 df %>% 
  mutate(row = row_number()) %>%
  pivot_longer(cols = starts_with("Day"), names_to = NULL) %>% 
  pivot_wider(names_from = value, values_from = value,
     values_fn = length, values_fill = 0) %>% 
  select(-row)
  • 输出
# A tibble: 7 × 6
  Pet.Shop   dog  fish   cat turtle rabbit
  <chr>    <int> <int> <int>  <int>  <int>
1 A            1     1     1      0      0
2 A            0     2     1      0      0
3 B            0     0     1      2      0
4 A            1     0     1      1      0
5 B            1     0     1      0      1
6 C            1     0     0      0      2
7 C            1     1     1      0      0
gmxoilav

gmxoilav2#

对于那些每次都很难理解values_fn = length的人(比如我)。下面是@akrun的相同代码,只是没有values_fn = length
@akrun的代码中有一些有趣的特性:

  1. names_to = NULL,所以我们不必在最后删除名称
    1.将names_fromvalues_from分配给一列value
    1.创建一个唯一的标识符row,以便原始 Dataframe 中的每一行在整形后都能被唯一地标识。对于分组状态count(Pet.Shop, row, value)中的计数也很重要
  2. values_fill = 0以用0填充NA。
library(tidyr)
library(dplyr)
  
df %>% 
  mutate(row = row_number()) %>% 
  pivot_longer(cols = starts_with("Day"), names_to = NULL) %>% 
  count(Pet.Shop, row, value) %>% 
  pivot_wider(names_from = value, values_from = n, values_fill = 0) %>% 
  select(-row)
Pet.Shop   cat   dog  fish turtle rabbit
  <chr>    <int> <int> <int>  <int>  <int>
1 A            1     1     1      0      0
2 A            1     0     2      0      0
3 A            1     1     0      1      0
4 B            1     0     0      2      0
5 B            1     1     0      0      1
6 C            0     1     0      0      2
7 C            1     1     1      0      0

相关问题